java将多文件打zip包输出到浏览器

package util;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class FileUtils {
    public static void zipFiles(List<String> fileList, String localPath,String zipFileName, HttpServletResponse response){
        OutputStream out = null;
        String zipPathName = localPath+"/"+zipFileName;
        try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(zipPathName)))){
            File zipFile = new File(zipPathName);
            if (!zipFile.exists()){
                zipFile.mkdirs();
            }
            for (String filePathName:fileList){
                File file = new File(filePathName);
                if (file.exists()){
                    ZipEntry zipEntry = new ZipEntry(file.getName());
                    zos.putNextEntry(zipEntry);
                    byte[] buffer = new byte[2048];
                    compressSingleFile(file,zos,buffer);
                }
            }
            //响应到浏览器
            response.reset();
            response.setContentType("application/x-msdownload");
            response.setHeader("Content-disposition","attachment;filename="+zipFileName);
            out = response.getOutputStream();
            byte[] bytes = readFileToBytes(new File(zipPathName));
            out.write(bytes);
            out.flush();
            out.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    public static byte[] readFileToBytes(File file) throws IOException {
        FileInputStream fis = null;
        if (file.exists()){
            try {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[2048];
                fis = new FileInputStream(file);
                BufferedInputStream bis = new BufferedInputStream(fis);
                int len;
                while ((len= bis.read(buffer,0, buffer.length))!=-1){
                    baos.write(buffer,0,len);
                }
                bis.close();
                fis.close();
                return baos.toByteArray();
            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
                if (fis!=null){
                    fis.close();
                }
            }
        }else {
            return null;
        }
    }

    /**
     * 压缩单文件
     * @param file
     * @param zos
     * @param buffer
     */
    public static void compressSingleFile(File file,ZipOutputStream zos,byte[] buffer){
        int len;
        try(FileInputStream fis = new FileInputStream(file)){
            while ((len=fis.read(buffer))>0){
                zos.write(buffer,0,len);
                zos.flush();
            }
            zos.closeEntry();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

参考:java 生成zip文件并浏览器导出 - 爱码网 (likecs.com)icon-default.png?t=M85Bhttps://www.likecs.com/show-840847.html(47条消息) java实现多文件压缩_yuhuofei2021的博客-CSDN博客_java 压缩多个文件icon-default.png?t=M85Bhttps://blog.csdn.net/Crezfikbd/article/details/126268756以上版本为未优化版本会在本地留存压缩文件

优化后的版本为:

public static void zipFiles(List<String> fileList, String zipFileName, HttpServletResponse response) throws IOException {
        byte[] buf = new byte[1024];
        BufferedOutputStream bos = null;
        ZipOutputStream out = null;
        try {
            bos = new BufferedOutputStream(response.getOutputStream());
            //响应到浏览器
            response.reset();
            response.setContentType("application/x-msdownload");
            response.setHeader("Content-disposition","attachment;filename="+zipFileName);
            out = new ZipOutputStream(bos);
            for (String filePathName:fileList){
                File file = new File(filePathName);
                FileInputStream in = new FileInputStream(file);
                out.putNextEntry(new ZipEntry(file.getName()));
                int len = -1;
                while ((len=in.read(buf))!=-1){
                    out.write(buf,0,len);
                }
                in.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (out!=null)
                out.close();
            if (bos!=null)
                bos.close();
        }
    }

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值