JAVA将多个二进制文件流已zip形式保存(解决了中文文件名乱码问题)

        因为压缩包内的文件需要用到中文命名,如果用jdk自带的zip Jar包没法实现,如果以中文命名会直接不显示,因此采用了

commons.compress.jar。
public static Boolean generateZip(List<Map<String,Object>> bytes) throws Exception {
        //ZipOutputStream out = null;
        Boolean flag = true;
        //压缩文件路径
        String strZipName = "C:/QueryResult.zip";
        File desFile = new File(strZipName);

        if(!desFile.exists()) {
            desFile.getParentFile().mkdirs();
            desFile.createNewFile();

        }
        ZipArchiveOutputStream zous = new ZipArchiveOutputStream(desFile);
        //防止中文乱码
        if(zous instanceof ZipArchiveOutputStream) {

            zous.setEncoding("GBK");

        }
        try {

            zous.setUseZip64(Zip64Mode.AsNeeded);

            //out = new ZipOutputStream(new FileOutputStream(strZipName));
            for (int i=0;i<bytes.size();i++) {
                int xh = i+1;
                String time = (String) bytes.get(i).get("time");
                String name = (String) bytes.get(i).get("name");
                String zjh = (String) bytes.get(i).get("zjh");
               //前面几个参数只是为了命名文件,这个by才是二进制文件,是byte[]的形式。
                byte[] by = (byte[]) bytes.get(i).get("buffer");
                String filename = xh+"-"+time+"-"+name+"-"+zjh+".pdf";
                ArchiveEntry entry = new ZipArchiveEntry(filename);
                zous.putArchiveEntry(entry);
                zous.write(by);
                zous.closeArchiveEntry();
//                out.putNextEntry(new ZipEntry(xh+"-"+time+"-"+name+"-"+zjh+".pdf"));
//                out.write(by);
//                out.flush();
//                out.closeEntry();
            }
        } catch (Exception e) {
            flag = false;
            e.printStackTrace();
        } finally {
//            if (out != null) {
//                out.close();
//            }
            zous.close();
            return flag;
        }
    }

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值