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

阅读更多

导包

导zip4j_1.3.2.jar包,出力压缩加密csv文件_第1张图片

 

入口

 

public static void writeCsvAsZip(HttpServletResponse response, List 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 filesToAdd = new ArrayList();
            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

 

你可能感兴趣的:(zip4j_1.3.2.jar)