(java)解决文件是中文名打包成zip文件名称乱码的问题。

1、引人

import org.apache.tools.zip.ZipEntry; 

import org.apache.tools.zip.ZipOutputStream;类

2、方法

/*

billNos:要打包的文件列表

suffix:后缀名

return 打包文件路径

*/

private String packZip(List<String> billNos, String suffix) throws Exception{
byte[] buffer = new byte[1024];
String dir = FileUtils.rootPath + "report/";//文件所在目录
File file;
String strZipName = "sample.zip";//名称
String path = dir + strZipName;//文件路径
File file1 = new File(path);

//判断打包文件是否存在,如果存在就删除
if(file1.exists()) {  
file1.delete();
        }
file1.createNewFile();//新建打包文件
String fileName;
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(path));
out.setEncoding("utf-8");//设置编码格式
FileInputStream fis;
int len;
for(String s:billNos){
fileName = dir + s+ suffix;//要打包的文件名称
file = new File(fileName);
fis = new FileInputStream(file);  
out.putNextEntry(new ZipEntry(file.getName()));
while((len = fis.read(buffer))>0){  
out.write(buffer,0,len);   
}
out.closeEntry();  
       fis.close();  
}
 
out.close();//关闭
return "report/" + strZipName;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值