解决Java原生压缩组件不支持中文文件名乱码的问题
作者:楚骧 发布时间:2021-07-29 22:39:38
标签:java,压缩,文件
最近发现Java原生的Zip压缩组件在压缩过程中,不支持文件名的中文编码,会在压缩过程中把中文文件名变成乱码。Apache的ant包中的压缩组件修复了这个问题,如果你在使用压缩功能时需要支持中文文件名,建议你直接使用Apache的压缩组件来实现这个功能。
具体使用方法:
1.在你的pom文件中增加对Apache的ant工具包的dependency:
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.9.3</version>
</dependency>
并在头部引用用到的类:
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
2.压缩的方法实现,大家注意,本组件支持设置编码(setEncoding("GBK")方法),解决了中文编码的问题:
public static void compress(String srcPath , String dstPath) throws IOException{
File srcFile = new File(srcPath);
File dstFile = new File(dstPath);
if (!srcFile.exists()) {
throw new FileNotFoundException(srcPath + "does not exists");
}
FileOutputStream out = null;
ZipOutputStream zipOut = null;
try {
out = new FileOutputStream(dstFile);
zipOut = new ZipOutputStream(new BufferedOutputStream(out));
zipOut.setEncoding("GBK");
String baseDir = "";
compress(srcFile, zipOut, baseDir);
}
catch (Throwable ex){
throw new RuntimeException(ex);
}
finally {
if(null != zipOut){
zipOut.close();
out = null;
}
if(null != out){
out.close();
}
}
}
private static void compress(File file, ZipOutputStream zipOut, String baseDir) throws IOException{
if (file.isDirectory()) {
compressDirectory(file, zipOut, baseDir);
} else {
compressFile(file, zipOut, baseDir);
}
}
/** 压缩一个目录 */
private static void compressDirectory(File dir, ZipOutputStream zipOut, String baseDir) throws IOException{
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
compress(files[i], zipOut, baseDir + dir.getName() + "/");
}
}
/** 压缩一个文件 */
private static void compressFile(File file, ZipOutputStream zipOut, String baseDir) throws IOException{
if (!file.exists()){
return;
}
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(file));
ZipEntry entry = new ZipEntry(baseDir + file.getName());
zipOut.putNextEntry(entry);
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
zipOut.write(data, 0, count);
}
}finally {
if(null != bis){
bis.close();
}
}
}
3.解压缩的实现:
public static void decompress(String zipFile , String dstPath)throws IOException{
File pathFile = new File(dstPath);
if(!pathFile.exists()){
pathFile.mkdirs();
}
ZipFile zip = new ZipFile(zipFile);
for(Enumeration entries = zip.getEntries();entries.hasMoreElements();){
ZipEntry entry = (ZipEntry)entries.nextElement();
String zipEntryName = entry.getName();
InputStream in = null;
OutputStream out = null;
try{
in = zip.getInputStream(entry);
String outPath = (dstPath+"/"+zipEntryName).replaceAll("\\*", "/");;
//判断路径是否存在,不存在则创建文件路径
File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
if(!file.exists()){
file.mkdirs();
}
//判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
if(new File(outPath).isDirectory()){
continue;
}
out = new FileOutputStream(outPath);
byte[] buf1 = new byte[1024];
int len;
while((len=in.read(buf1))>0){
out.write(buf1,0,len);
}
}
finally {
if(null != in){
in.close();
}
if(null != out){
out.close();
}
}
}
}
以上代码经过测试,可以直接使用。
来源:http://www.jianshu.com/p/36f87ab9a55b


猜你喜欢
- 在工作上,我最近对一个现有的Java项目代码进行了清理。完成之后,我发现了一些反复出现的不规范代码。所以,我把它们整理成了一个列表出来分享给
- Kotlin的对象表达式与Java中的匿名内部类的主要区别:匿名内部类只能指定一个父类型,但对象表达式可以指定0~N个肤类型。一、对象表达式
- 一. 简介 俩个数据库db1,db2, db1数据库的map
- 1.定义字符串字符串常见的构造方式如下:String s1 = "with";String s2 = new Strin
- 对于超大数字的运算,用long long int仍然不能解决,这时候就需要考虑通过模拟运算和数组存储来实现高精度运算。本文讨论借助C++的s
- 要使用Dictionary集合,需要导入C#泛型命名空间 System.Collections.Generic(程序集:mscor
- 本文实例为大家分享了Android屏幕适配工具类的具体代码,供大家参考,具体内容如下DimenToolgithub地址Android 屏幕适
- 本文实例讲述了Android开发之ListView列表刷新和加载更多实现方法。分享给大家供大家参考。具体如下:上下拉实现刷新和加载更多的Li
- 表达式目录树表达式目录树:语法树,或者说是一种数据结构1.表达式目录树Expression:System.Linq.Expressions;
- 要实现摇一摇的功能,类似于微信的摇一摇方法1:通过分析加速计数据来判断是否进行了摇一摇操作(比较复杂)方法2:iOS自带的Shake监控AP
- 目录面试题1:如何判断对象是否存活1.引用计数算法2.可达性分析算法面试题2:哪些对象可以作为GC Roots?面试题3:你了解的对象引用方
- 前言Windows 11下所有控件已经默认采用圆角,其效果更好、相对有着更好的优化,只是这是默认的行为,无法进一步自定义。注意两点:Pain
- RocketMQ发送消息我们在使用RocketMQ发送消息时,一般都会使用DefaultMQProducer,类型的代码如下:Default
- 碎片,它的出现是为了更好展示UI的设计,让程序更加得到充分的展示。Fragment的出现,如微信的额主界面包含多个Fragment,使得微信
- 效果图如下:默认第一次加载选择原始队列:级联效果图:关键代码给下拉列表选中事件监听绑定Id :int pos = firsthand_dlb
- Android Studio是采用gradle来构建项目的,gradle是基于groovy语言的,如果只是用它构建普通Android项目的话
- File类File类事java.io包中唯一代表磁盘文件本身的对象。File类定义了一些与平台无关的方法来操作文件,可以通过调用File类中
- 做个网站的安卓客户端,用户安装到自己手机上,如果我出了新版本怎么办呢?要有版本更新功能。 本来版本检测最好可以自动进行。但如果每次开启程序,
- 项目中有需要多次统计 某些集合中 的某个属性值,所以考虑封装一个方法,让其其定义实现计算方式。 话不多说,看代码:1、封装的自定义集合工具类
- 😜shape属性详解<?xml version="1.0" encoding="utf-8"?