导zip4j_1.3.2.jar包,出力压缩加密csv文件

导包

ZIP4J---ZIP文件压缩与解压缩学习

 

入口

 

public static void writeCsvAsZip(HttpServletResponse response, List<CsvEntity> entities, String fileName, String password)
    {
        String csv = ".csv";
        String zip = ".zip";
        String url = "";
        try {
            File csvFile = CsvCreator.out(entities);
            url = csvFile.getPath();
            url = url.substring(0, url.length() - 3);
            fileChannelCopy(csvFile.getPath(), url + csv);

            makeZip(url + zip, new File(url + csv), password);
            StringBuilder uri = new StringBuilder();
            uri.append(url);
            uri.append(zip);

            downloadFile(response, uri, fileName);

        } catch (IOException e) {
            AxaNaviLogHelper.error("CSV出力失败", e);
            throw new SystemException(ERROR, ErrorCode.CSV_OUT_ERROR, "CSV出力失败。", e);
        } catch (ZipException e) {
            AxaNaviLogHelper.error("CSV出力失败。", e);
            throw new SystemException(ERROR, ErrorCode.CSV_OUT_ERROR, "CSV出力失败。", e);
        } finally {
             deleteFile(url + csv);
             deleteFile(url + zip);
        }
    }

 文件复制

 

public static void fileChannelCopy(String ChangeBeforeName, String ChangeAfterName) {
        File s = new File(ChangeBeforeName);
        File t = new File(ChangeAfterName);

        FileInputStream fi = null;
        FileOutputStream fo = null;
        FileChannel in = null;
        FileChannel out = null;
        try {
            fi = new FileInputStream(s);
            fo = new FileOutputStream(t);
            in = fi.getChannel();
            out = fo.getChannel();
            in.transferTo(0, in.size(), out);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fi.close();
                in.close();
                fo.close();
                out.close();
            } catch (IOException e) {
                AxaNaviLogHelper.error("CSV失败。", e);
                throw new SystemException(ERROR, ErrorCode.CSV_OUT_ERROR, "CSV失败。", e);
            }
        }
    }

 压缩加密

引用zip4j_1.3.2.jar包方法

public static void makeZip(String zipFileName, File inputFile, String password) throws ZipException {
            ZipFile zipFile = new ZipFile(zipFileName);

            ArrayList<File> filesToAdd = new ArrayList<File>();
            filesToAdd.add(inputFile);
            ZipParameters parameters = new ZipParameters();
            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); 
            if (!StringUtils.isEmpty(password)) {
                parameters.setEncryptFiles(true);
                parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
                parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
                parameters.setPassword(password);
            }

            zipFile.addFiles(filesToAdd, parameters);
    }

 下载

public static void downloadFile(HttpServletResponse response, StringBuilder uri, String fileName) throws IOException {
        StringBuffer filename = new StringBuffer();
        filename.append(uri);
        File file = new File(filename.toString());
        StringBuffer sb = new StringBuffer();
        sb.append("attachment;  filename=").append(fileName);
        response.setHeader("Expires", "0");
        response.setHeader("Cache-Control",
                "must-revalidate, post-check=0, pre-check=0");
        response.setHeader("Pragma", "public");
        response.setContentType("application/x-msdownload;charset=UTF-8");
        response.setHeader("Content-Disposition", new String(sb.toString()
                .getBytes(), "Windows-31J"));

        FileInputStream inputStream = new FileInputStream(file);
        OutputStream outputStream = response.getOutputStream();
        byte[] buffer = new byte[1024];
        int i = -1;
        while ((i = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, i);
        }
        outputStream.flush();
        outputStream.close();
        inputStream.close();
    }

 删除临时文件

public static void deleteFile(String sPath) {
        File file = new File(sPath);
        if (file.isFile() && file.exists()) {
            file.delete();
        }
    }

 

 

 

具体参考

http://www.open-open.com/lib/view/open1378556210553.html

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值