解决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
0
投稿
猜你喜欢
- 问题发现今天发生了一件事,令我非常郁闷,就是我在使用一个SDK时,当我调用他的方法时,提示我方法中的参数var1, var2如下:// 方法
- 本文实例讲述了C#简单实现防止多个程序运行的方法。分享给大家供大家参考,具体如下:/// <summary>/// 应用程序的主
- Java内置GUI Frame类Frame概述* 事件处理 * 事件: 用户的一个操作* 事件源: * 作的组件*
- 本文实例讲述了C#通过指针读取文件的方法。分享给大家供大家参考。具体如下:// readfile.cs// 编译时使用:/unsafe//
- 泛型约束的意思就是说:类的泛型,只能是where字句后面所写的接口或类。这么说好像也有点不大明白,举个例子。我有一个接口,如下:
- 本文实例讲述了Android编程使用android-support-design实现MD风格对话框功能。分享给大家供大家参考,具体如下:首先
- 目录I. 环境配置1. 项目配置2. 数据库表II. 传参类型确定1. 参数类型为整形2. 指定jdbcType3. 传参类型为String
- 背景:有时候string类型的数据取出来是个很标准的key、value形式,通过Gson的可以直接转成map使用方式:Gson gson =
- 1.前言初始化就是给变量一个初始值。 初始化的目的是为了让变量有值,防止使用时出现异常。在构造函数中,有一项重要功能就是对成员变量进行初始化
- 前言在前面的2个章节中,一一哥 带大家实现了在Spring Security中添加图形验证码校验功能,其实Spring Security的功
- 1 前言项目中,目前主流的当然是微服务项目。为了应对高并发,以及保证自己的服务比较稳定,通常会把服务按照模块,或者具体的业务划分为多个独立的
- 本文主要研究的是关于Java中重载,重写,多态,静态绑定、动态绑定的相关内容,具体如下。重载,英文名是overload,是指在一个类中定义了
- 这篇文章主要介绍了Java内存模型可见性问题相关解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友
- Java Map.values()方法获取Map集合中的所有键值对象Java 集合类中的 Map.values() 方法用来获取
- 1.MyBatisX插件在使用mybatis或者mybatis-plus时,我们可以安装IDEA的MyBatis的插件 - MyBatisX
- 一 数据库操作框架的历程1.1 JDBCJDBC(Java Data Base Connection,java数据库连接)是一种用于执行SQ
- 引言备忘录模式经常可以遇到,譬如下面这些场景:浏览器回退:浏览器一般有浏览记录,当我们在一个网页上点击几次链接之后,可在左上角点击左箭头回退
- http无状态协议的解决:HTTP 是一种无状态协议。 无状态协议不要求服务器在多次请求期间保留有关每个用户的信息或状态。但是某些 Web
- 在程序中对文件进行压缩解压缩是很重要的功能,不仅能减小文件的体积,还能对文件起到保护作用。如果是生成用户可以下载的文件,还可以极大的减少网络
- 一、setting.xml文件的位置今天我们来谈谈Maven setting文件配置的禅定之道。不知道大家有没有听说过禅宗?嗯,没错,就是那