Garbled characters appear when java decompresses zip package

解决思路:
首先判断需要解压的文件是否存在或路径是否正确,接着判断路径是否存在,若不存在则创建路径,然后判断压缩文件是否合法,最后设置文件名称编码为“GBK”即可。
免费在线教学视频分享:java教学视频
示例代码:
package com.yunfei.fts;
import java.io.File;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
public class ZipUtil {
/**
* todo zip解压缩
* @param source 压缩文件全路径
* @param target 要解压路径
* @param targetName 解压文件夹名称
*/
public static void unzip (String source,String target,String targetName) throws Exception{
try {
File file = new File(source);
if(!file.exists() || file.isDirectory()){
throw new Exception("将要解压文件不存在或路径填写不正确!");
}
file = new File(target+File.separator+targetName);
if(!file.exists()){
file.mkdirs();
System.out.println("路劲不存在,创建路径");
}
ZipFile zipfile = new ZipFile(source);
if (!zipfile.isValidZipFile()) {
throw new Exception("压缩文件不合法,可能被损坏.");
}
zipfile.setFileNameCharset("GBK");
zipfile.extractAll(target+File.separator+targetName);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
/**
* todo 生成zip压缩
* @param source 要压缩文件全路径
* @param target 压缩文件存放路径
* @param targetName 解压文件名称
*/
public static void zip (String source,String target,String targetName) throws Exception{
try {
File file = new File(target);
if(!file.exists()){
file.mkdirs();
System.out.println("解压存储路劲不存在,创建路径");
}
file = new File(source);
if(!file.exists()){
throw new Exception("将要解压文件不存在或路径填写不正确!");
}
ZipFile zipfile = new ZipFile(target+File.separator+targetName);
zipfile.setFileNameCharset("GBK");
ZipParameters params = new ZipParameters();
params.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // 压缩方式
params.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 压缩级别
//zipfile.cr
if(file.isFile()){
zipfile.addFile(file, params);
}else{
zipfile.addFolder(source, params);
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
public static void main(String[] args) {
try {
unzip("d:\\home.zip","e:\\","test");
zip("D:\\home","e:\\","test.zip");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}推荐java相关文章教程:java开发入门
The above is the detailed content of Garbled characters appear when java decompresses zip package. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version
Recommended: Win version, supports code prompts!

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),





