根据路径地址压缩文件/文件夹-zip,并下载zip(二)

27 篇文章 1 订阅
8 篇文章 0 订阅
这篇博客介绍了如何使用Java实现文件夹的压缩功能,包括创建ZipOutputStream和BufferedOutputStream,递归压缩文件夹及其内容。同时,提供了下载压缩文件的方法,通过设置响应头实现文件下载。整个过程包括压缩和下载的详细步骤。
摘要由CSDN通过智能技术生成

该功能和需求及逻辑在上篇一中已经说明和实现,这是第二种压缩方法,下载同上,只是代码量相对少些,也做下笔记吧。

@GetMapping("/zip")
    public void zip(String srcDir, HttpServletRequest request, HttpServletResponse response)throws Exception {
        System.out.println("压缩中...");
        // 本地资源路径
        String localPath = E:/app/uploadPath;
        String zipFileName;      // 目的地Zip文件
        String sourceFileName;   //源文件(带压缩的文件或文件夹)

                //创建zip输出流
                ZipOutputStream out = new ZipOutputStream( new FileOutputStream(localPath+"/test2.zip"));

                 //创建缓冲输出流
                 BufferedOutputStream bos = new BufferedOutputStream(out);

                 File sourceFile = new File("E:\\app\\uploadPath\\avatar\\target\\2021");

                 //调用函数
                 compress(out,bos,sourceFile,sourceFile.getName());

                 bos.close();
                 out.close();
                 System.out.println("压缩完成");

        String contentType = "application/octet-stream";
//下载
      download(request, response, localPath+"/test2.zip", "test2.zip");
    }

    public void compress(ZipOutputStream out,BufferedOutputStream bos,File sourceFile,String base) throws Exception{
                 //如果路径为目录(文件夹)
                 if(sourceFile.isDirectory())
                     {

                         //取出文件夹中的文件(或子文件夹)
                         File[] flist = sourceFile.listFiles();

                         if(flist.length==0)//如果文件夹为空,则只需在目的地zip文件中写入一个目录进入点
                             {
                                System.out.println(base+"/");
                                 out.putNextEntry(  new ZipEntry(base+"/") );
                             }
                         else//如果文件夹不为空,则递归调用compress,文件夹中的每一个文件(或文件夹)进行压缩
                         {
                                 for(int i=0;i<flist.length;i++)
                                     {
                                         compress(out,bos,flist[i],base+"/"+flist[i].getName());
                                     }
                             }
                     }
                 else//如果不是目录(文件夹),即为文件,则先写入目录进入点,之后将文件写入zip文件中
                 {
                         out.putNextEntry( new ZipEntry(base) );
                         FileInputStream fos = new FileInputStream(sourceFile);
                         BufferedInputStream bis = new BufferedInputStream(fos);

                        int tag;
                         System.out.println(base);
                         //将源文件写入到zip文件中
                         while((tag=bis.read())!=-1)
                             {
                                 out.write(tag);
                             }
                         bis.close();
                         fos.close();

                     }
             }

    /**
     * 下载
     */
    public static void download(HttpServletRequest request,HttpServletResponse response, String storeName, String contentType,String realName) throws Exception {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        String downLoadPath =  storeName;

        long fileLength = new File(downLoadPath).length();

        response.setContentType(contentType);
        response.setHeader("Content-disposition", "attachment; filename="
                + new String(realName.getBytes("utf-8"), "ISO8859-1"));
        response.setHeader("Content-Length", String.valueOf(fileLength));

        bis = new BufferedInputStream(new FileInputStream(downLoadPath));
        bos = new BufferedOutputStream(response.getOutputStream());
        byte[] buff = new byte[2048];
        int bytesRead;
        while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
            bos.write(buff, 0, bytesRead);
        }
        bis.close();
        bos.close();
    }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值