java压缩多个文件到zip中,并返回给浏览器

        今天遇到个需求,需要生成excel文件,生成文件后压缩到zip包中,然后响应给浏览器,下面是核心代码,以便后面再用到:

        

/**
     * 下载压缩包,响应给浏览器
     * @param fileList  文件集合
     * @param zipFileName  下载zip的文件名
     * @param request
     * @param response
     */
    public void downloadZip(List fileList,String zipFileName, HttpServletRequest request,HttpServletResponse response){
        byte[] buf = new byte[1024];
        // 获取输出流
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        FileInputStream in = null;
        ZipOutputStream out = null;
        try {
            // 重置
            response.reset();
            String fileName = "";
            String agent = request.getHeader("user-agent");
            if (agent.contains("FireFox")) {
                fileName = new String(zipFileName.getBytes("UTF-8"), "iso-8859-1");
            } else {
                fileName = URLEncoder.encode(zipFileName, "UTF-8");
            }

            // 不同类型的文件对应不同的MIME类型
            response.setContentType("application/x-msdownload");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".zip");

            // ZipOutputStream类:完成文件或文件夹的压缩
            out = new ZipOutputStream(bos);
            for (int i = 0; i < fileList.size(); i++) {
                in = new FileInputStream(fileList.get(i));
                // 给列表中的文件单独命名
                out.putNextEntry(new ZipEntry(fileList.get(i).getName()));
                int len = -1;
                while ((len = in.read(buf)) != -1) {
                    out.write(buf, 0, len);
                }
            }
            out.close();
            bos.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try{
                if (in != null){
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

        另外附上写在某个位置(比如本地或者服务器上)的公共方法:

 //调用示例  zipFiles(fileList.toArray(new File[fileList.size()]),new File("D:\\/my.zip"));
    /**
     * 生成压缩文件
     * @param srcFiles  要压缩的文件
     * @param zipFile   压缩包全路径
     */
    public void zipFiles(File[] srcFiles, File zipFile) {
        // 判断压缩后的文件存在不,不存在则创建
        if (!zipFile.exists()) {
            try {
                zipFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 创建 FileOutputStream 对象
        FileOutputStream fileOutputStream = null;
        // 创建 ZipOutputStream
        ZipOutputStream zipOutputStream = null;
        // 创建 FileInputStream 对象
        FileInputStream fileInputStream = null;

        try {
            // 实例化 FileOutputStream 对象
            fileOutputStream = new FileOutputStream(zipFile);
            // 实例化 ZipOutputStream 对象
            zipOutputStream = new ZipOutputStream(fileOutputStream);
            // 创建 ZipEntry 对象
            ZipEntry zipEntry = null;
            // 遍历源文件数组
            for (int i = 0; i < srcFiles.length; i++) {
                // 将源文件数组中的当前文件读入 FileInputStream 流中
                fileInputStream = new FileInputStream(srcFiles[i]);
                // 实例化 ZipEntry 对象,源文件数组中的当前文件
                zipEntry = new ZipEntry(srcFiles[i].getName());
                zipOutputStream.putNextEntry(zipEntry);
                // 该变量记录每次真正读的字节个数
                int len;
                // 定义每次读取的字节数组
                byte[] buffer = new byte[1024];
                while ((len = fileInputStream.read(buffer)) > 0) {
                    zipOutputStream.write(buffer, 0, len);
                }
            }
            zipOutputStream.closeEntry();
            zipOutputStream.close();
            fileInputStream.close();
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

你可能感兴趣的:(Java,java,多个文件压缩zip,ZipOutputStream,ZipEntry,文件压缩导出)