JAVA编码(33)—— java文件压缩

    /**
     * 压缩文件路径
     */
    private final static String zipFilePath = "D://reporinfodata.zip"; 

  location list容器中装的事文件的详细路劲
/** * 将查询的文件进行压缩打包 * @param response */ private void getReportZip(HttpServletResponse response) { ZipOutputStream reportZip = null; try { reportZip = new ZipOutputStream(new FileOutputStream(zipFilePath)); for (int i = 0; i < location.size(); i++) { writeAFile(new File(location.get(i)), reportZip); } reportZip.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 将文件添加到压缩包中 */ public static void writeAFile(File file, ZipOutputStream zos) { try { FileInputStream fis = new FileInputStream(file); ZipEntry ze = new ZipEntry(file.getName()); zos.putNextEntry(ze); zos.setEncoding("utf-8");//设置压缩文件的编码 byte[] content = new byte[1024]; int len; while ((len = fis.read(content)) != -1) { zos.write(content, 0, len); } zos.flush(); fis.close(); } catch (Exception e) { e.printStackTrace(); } }

 

posted @ 2015-10-28 14:53  xu_shuyi  阅读(132)  评论(0)    收藏  举报