解决思路:首先判断需要解压的文件是否存在或路径是否正确,接着判断路径是否存在,若不存在则创建路径,然后判断压缩文件是否合法,最后设置文件名称编码为“GBK”即可。免费在线教学视频分享:java教学视频示例代码:package com.yun
解决思路:
首先判断需要解压的文件是否存在或路径是否正确,接着判断路径是否存在,若不存在则创建路径,然后判断压缩文件是否合法,最后设置文件名称编码为“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 {
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;
}
}
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();
}
}
}
--结束END--
本文标题: java解压zip包出现乱码
本文链接: https://www.lsjlt.com/news/1771.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-03
2024-04-03
2024-04-01
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0