java压缩文件中文名乱码问题
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
改为
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream; ant包里提供ZipOutputStream类的setEncoding("gbk")方法。 zos.setEncoding("gbk");
ant包下载(点击进入页面,另开一个新页面)
下载地址如下:
https://mvnrepository.com/artifact/org.apache.ant/ant/1.7.1 (点击进入页面,本页面被覆盖)
package file; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream; public class Zip { public static void main(String[] args) {
//文件所在路径
String path = "E:\\test";
File dir = new File(path);
if(!dir.isDirectory()) {
System.out.println(path + "--路径不存在!");
}
//所有文件
File[] files = new File(path).listFiles();
if(files == null || files.length < 1) {
System.out.println(path + "--路径下无文件!");
}
//压缩文件名
String zipname = "hello.zip";
File zipFile = new File(path + File.separator + zipname); if(zipFiles(files, zipFile) == 0) {
System.out.println("压缩文件ok!");
}
} public static int zipFiles(File[] files, File zipFile) {
if (zipFile.exists()) {
System.out.println(zipFile + "--压缩文件已存在!");
return -1;
}
BufferedInputStream bis = null;
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
byte[] bufs = new byte[1024*10];
for(int i=0;i<files.length;i++){
//创建ZIP实体,并添加进压缩包
ZipEntry zipEntry = new ZipEntry(files[i].getName());
zos.putNextEntry(zipEntry);
//乱码问题
zos.setEncoding("gbk");
bis = new BufferedInputStream(new FileInputStream(files[i]), 1024*10);
int read = 0;
while((read=bis.read(bufs, 0, 1024*10)) != -1){
zos.write(bufs,0,read);
}
}
return 0;
}catch (IOException e) {
e.printStackTrace();
System.out.println("压缩文件过程BUG了!");
return -1;
} finally {
try {
if (zos != null) {
zos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (bis != null) {
bis.close();
}
} catch (Exception e) {
e.printStackTrace();
}
} }
}
java压缩文件中文名乱码问题的更多相关文章
- java压缩文件或文件夹并导出
java压缩文件或文件夹并导出 tozipUtil: package com.zhl.push.Utils; import java.io.File; import java.io.FileInput ...
- java压缩文件解压:调用WinRAR5命令强于自己写代码实现
最近,手上维护着一个几年前的系统,技术是用的JSP+Strust2,系统提供了rar和zip两种压缩格式的解压功能,后台是用java实现的 1.解压rar格式,采用的是java-unrar-0.3.j ...
- Java压缩文件
压缩文件 package com.iss.cpf.windmanger.userprivilegeexport.bizlogic; import java.io.BufferedInputStream ...
- linux下解压缩文件中文乱码问题的解决
在windows上压缩的文件,是以系统默认编码中文来压缩文件.由于zip文件中没有声明其编码,所以linux上的unzip一般以默认编码解压,中文文件名会出现乱码. 虽然2005年就有人把这报告为bu ...
- java 压缩文件
package folder; import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundExcept ...
- 关于TomCat上传文件中文名乱码的问题
最近在学习TomCat文件上传这一部分,由于文件上传必须要三个条件: 1.表单提交方式必须为Post 2.表单中需要有<input type="file">元素,还需要 ...
- 简单测试Demo:如何用Java压缩文件夹和文件
一.直接贴出测试代码 package com.jason.zip; import java.io.File; import java.io.FileInputStream; import java.i ...
- Java 压缩文件夹工具类(包含解压)
依赖jar <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons ...
- 一文教您如何通过 Java 压缩文件,打包一个 tar.gz Filebeat 采集器包
欢迎关注笔者的公众号: 小哈学Java, 专注于推送 Java 领域优质干货文章!! 个人网站: https://www.exception.site/essay/create-tar-gz-by-j ...
随机推荐
- 文件和Stream
I/O和文件 输入/输出(I/O)就是在内存和外部设备之间复制数据的过程.输入(input)就是从I/O设备复制数据到内存,输出(output)就是从内存复制数据到I/O设备. 一个文件可以理解成一串 ...
- “fatal error: hdf5.h: 没有那个文件或目录”解决方法
問題一: Installing Caffe without CUDA: fatal error: cublas_v2.h No such file: 在Makefile.config中修改,將CPU_ ...
- 使用C#语言,将DataTable 转换成域模型
DataTable dt = SqlHelper.Query(strQuery); ) * size).Take(pagesize); List<Model> listData = new ...
- The 5 types of programmers
from: http://stevenbenner.com/2010/07/the-5-types-of-programmers/ps: 评论也很精彩 In my code journeys and ...
- webSocket通信
针对webSocket通信总结: 1.webSocket通信原理图: 2.webSocket通信实例 参考地址1:https://www.cnblogs.com/cjm123/p/9674506.ht ...
- Oracle 数据自动备份 通过EXP备份
先写个批处理文件(.bat),具体如下:@echo off@echo ================================================@echo windows环境下 ...
- 【转】How-to: Enable User Authentication and Authorization in Apache HBase
With the default Apache HBase configuration, everyone is allowed to read from and write to all table ...
- ColorPicker 颜色选择器
用于颜色选择,支持多种格式. 基础用法 使用 v-model 与 Vue 实例中的一个变量进行双向绑定,绑定的变量需要是字符串类型. <div class="block"&g ...
- php URL处理函数
parse_url() basename() pathinfo() dirname() 用法 parse_url() 是一计算机函数,功能是解析一个 URL 并返回一个关联数组,包含 ...
- ControlTemplate in WPF —— Button
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...