将有多个文件的文件夹压缩成压缩包返回给前端

场景:

最近有涉及到将有多个文件的文件夹压缩成文件流返回给前端,给出使用的工具类

 涉及到一些文件头处理的问题:这个非常有用

 // 显示响应头,将文件名传给前端
 httpResponse.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
 // 解决前端访问跨域问题
 httpResponse.setHeader("Access-Control-Allow-Origin", "*");
 httpResponse.setContentType("application/pdf");
 // 需要注意的是,attachment表示的是附件,不加这个就没有附件下载的标识
 httpResponse.addHeader("Content-disposition", "attachment; filename="+fileNameUnEncode+".zip");

 有多个文件的文件夹打包压缩成文件流返回给前端

/**
     * 创建ZIP文件
     * @param sourcePath 目标文件或文件夹路径
     * @param filePathAndName 生成的zip文件存在路径(包括文件名)
     */
    @SneakyThrows
    public RtMsg createZip(String sourcePath, String filePathAndName, HttpServletResponse httpResponse) {
        ZipOutputStream zos = null;
        OutputStream os = null;
        try {
            //如果是浏览器请求下载zip
            os = httpResponse.getOutputStream();
            zos = new ZipOutputStream(os);
            //更改文件编码,将压缩包命名为中文
            String fileNameUnEncode = URLEncoder.encode("批量下载文件","UTF-8");
            httpResponse.reset();
            //显示响应头,将文件名传给前端
            httpResponse.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            // 解决前端访问跨域问题
            httpResponse.setHeader("Access-Control-Allow-Origin", "*");
            httpResponse.setContentType("application/pdf");
            httpResponse.addHeader("Content-disposition", "attachment; filename="+fileNameUnEncode+".zip");
            writeZip(new File(sourcePath), "", zos);

        } catch (FileNotFoundException e) {
            logger.error("创建ZIP文件失败",e);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (zos != null) {
                    zos.close();
                }
                if (null != os) {
                    os.close();
                }
            } catch (IOException e) {
                logger.error("创建ZIP文件失败",e);
            }
        }
        return null;
    }

    /**
     * 压缩zip,循环压缩子目录文件
     */
    private static void writeZip(File file, String parentPath, ZipOutputStream zos) {
        if(file.exists()){
            //处理文件夹
            if(file.isDirectory()){
                parentPath+=file.getName()+ File.separator;
                File [] files=file.listFiles();
                if(files.length != 0)
                {
                    for(File f:files){
                        writeZip(f, parentPath, zos);
                    }
                }
                else
                {       //空目录则创建当前目录
                    try {
                        zos.putNextEntry(new ZipEntry(parentPath));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }else{
                FileInputStream fis=null;
                try {
                    fis=new FileInputStream(file);
                    //创建压缩文件
                    ZipEntry ze = new ZipEntry(parentPath + file.getName());
                    //添加压缩文件
                    zos.putNextEntry(ze);
                    byte [] content=new byte[1024];
                    int len;
                    while((len=fis.read(content))!=-1){
                        zos.write(content,0,len);
                        zos.flush();
                    }

                } catch (FileNotFoundException e) {
                    logger.error("创建ZIP文件失败",e);
                } catch (IOException e) {
                    logger.error("创建ZIP文件失败",e);
                }finally{
                    try {
                        if(fis!=null){
                            fis.close();
                        }
                    }catch(IOException e){
                        logger.error("创建ZIP文件失败",e);
                    }
                }
            }
        }
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值