Java实现把文件及文件夹压缩成zip

作者:zeng1994 时间:2022-03-30 05:28:45 

最近碰到个需要下载zip压缩包的需求,于是我在网上找了下别人写好的zip工具类。但找了好多篇博客,总是发现有bug。因此就自己来写了个工具类。
这个工具类的功能为:

(1)可以压缩文件,也可以压缩文件夹

(2)同时支持压缩多级文件夹,工具内部做了递归处理

(3)碰到空的文件夹,也可以压缩

(4)可以选择是否保留原来的目录结构,如果不保留,所有文件跑压缩包根目录去了,且空文件夹直接舍弃。注意:如果不保留文件原来目录结构,在碰到文件名相同的文件时,会压缩失败。

(5)代码中提供了2个压缩文件的方法,一个的输入参数为文件夹路径,一个为文件列表,可根据实际需求选择方法。

下面直接上代码

一、代码

ZipUtils


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @author Nemo
* @version 1.0
* @date 2019/11/5
*/
public class ZipUtils {
 private static final int BUFFER_SIZE = 2 * 1024;
 /**
  * 压缩成ZIP 方法1
  * @param sourceFile 压缩文件夹路径
  * @param out  压缩文件输出流
  * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;
  *             false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
  * @throws RuntimeException 压缩失败会抛出运行时异常
  */
 public static void toZip(File sourceFile, OutputStream out, boolean KeepDirStructure)
     throws RuntimeException{
   ZipOutputStream zos = null ;
   try {
     zos = new ZipOutputStream(out);
     compress(sourceFile,zos,sourceFile.getName(),KeepDirStructure);
   } catch (Exception e) {
     throw new RuntimeException("zip error from ZipUtils",e);
   }finally{
     if(zos != null){
       try {
         zos.close();
       } catch (IOException e) {
         e.printStackTrace();
       }
     }
   }
 }
 /**
  * 压缩成ZIP 方法2
  * @param srcFiles 需要压缩的文件列表
  * @param out      压缩文件输出流
  * @throws RuntimeException 压缩失败会抛出运行时异常
  */
 public static void toZip(List<File> srcFiles , OutputStream out)throws RuntimeException {
   long start = System.currentTimeMillis();
   ZipOutputStream zos = null ;
   try {
     zos = new ZipOutputStream(out);
     for (File srcFile : srcFiles) {
       byte[] buf = new byte[BUFFER_SIZE];
       zos.putNextEntry(new ZipEntry(srcFile.getName()));
       int len;
       FileInputStream in = new FileInputStream(srcFile);
       while ((len = in.read(buf)) != -1){
         zos.write(buf, 0, len);
       }
       zos.closeEntry();
       in.close();
     }
     long end = System.currentTimeMillis();
     System.out.println("压缩完成,耗时:" + (end - start) +" ms");
   } catch (Exception e) {
     throw new RuntimeException("zip error from ZipUtils",e);
   }finally{
     if(zos != null){
       try {
         zos.close();
       } catch (IOException e) {
         e.printStackTrace();
       }
     }
   }
 }
 /**
  * 递归压缩方法
  * @param sourceFile 源文件
  * @param zos    zip输出流
  * @param name    压缩后的名称
  * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;
  *             false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
  * @throws Exception
  */
 private static void compress(File sourceFile, ZipOutputStream zos, String name,
                boolean KeepDirStructure) throws Exception{
   byte[] buf = new byte[BUFFER_SIZE];
   if(sourceFile.isFile()){
     // 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
     zos.putNextEntry(new ZipEntry(name));
     // copy文件到zip输出流中
     int len;
     FileInputStream in = new FileInputStream(sourceFile);
     while ((len = in.read(buf)) != -1){
       zos.write(buf, 0, len);
     }
     // Complete the entry
     zos.closeEntry();
     in.close();
   } else {
     File[] listFiles = sourceFile.listFiles();
     if(listFiles == null || listFiles.length == 0){
       // 需要保留原来的文件结构时,需要对空文件夹进行处理
       if(KeepDirStructure){
         // 空文件夹的处理
         zos.putNextEntry(new ZipEntry(name + "/"));
         // 没有文件,不需要文件的copy
         zos.closeEntry();
       }
     }else {
       for (File file : listFiles) {
         // 判断是否需要保留原来的文件结构
         if (KeepDirStructure) {
           // 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
           // 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
           compress(file, zos, name + "/" + file.getName(),KeepDirStructure);
         } else {
           compress(file, zos, file.getName(),KeepDirStructure);
         }
       }
     }
   }
 }
 public static void main(String[] args) throws Exception {
   /** 测试压缩方法1 */
   FileOutputStream fos1 = new FileOutputStream(new File("c:/mytest01.zip"));
   ZipUtils.toZip(new File("D:/log"), fos1,true);
   /** 测试压缩方法2 */
   List<File> fileList = new ArrayList<>();
   fileList.add(new File("D:/Java/jdk1.7.0_45_64bit/bin/jar.exe"));
   fileList.add(new File("D:/Java/jdk1.7.0_45_64bit/bin/java.exe"));
   FileOutputStream fos2 = new FileOutputStream(new File("c:/mytest02.zip"));
   ZipUtils.toZip(fileList, fos2);
 }
}

二、注意事项

写该工具类时,有些注意事项说一下:

(1)支持选择是否保留原来的文件目录结构,如果不保留,那么空文件夹直接不用处理。

(1)碰到空文件夹时,如果需要保留目录结构,则直接添加个ZipEntry就可以了,不过就是这个entry的名字后面需要带上一斜杠(/)表示这个是目录。

(2)递归时,不需要把zip输出流关闭,zip输出流的关闭应该是在调用完递归方法后面关闭

(3)递归时,如果是个文件夹且需要保留目录结构,那么在调用方法压缩他的子文件时,需要把文件夹的名字加一斜杠给添加到子文件名字前面,这样压缩后才有多级目录。

三、如何在javaWeb项目中使用该工具类

这个工具类在web项目中的使用场景就是多文件下载,我就简单说个下载多个excel表格的案例吧。

代码中的步骤为:

(1)创建一个临时文件夹

(2)将要下载的文件生成至该临时文件夹内

(3)当所有文件生成完后,获取HttpServletResponse获取设置下载的header

(4)调用工具类的方法,传入上面生成的临时文件夹路径及response获取的输出流;这样就下载出来zip包了

(5)递归删除掉上面生成的临时文件夹和文件

下面为一个示例代码的代码片段,不是完整代码,简单看一下代码中的步骤


import org.apache.commons.io.FileUtils;
import java.io.*;

/**
  * 图片打包下载
  * @author: wangzhouchao
  */
 @ApiImplicitParams({
     @ApiImplicitParam(name = "id", value = "申请人id", required = true, dataType = "Long", paramType = "query"),
 })
 @ApiOperation(value = "图片打包下载", notes = "图片打包下载")
 @RequestMapping(value = "/downloadPictureList", method = RequestMethod.GET)
 public void downloadPictureList(TProposerDataVO tProposerDataVO) {

long readyStart = System.currentTimeMillis();

// ************* 1. 获取到存在数据库中的图片的url *************
   PictureDownloadVO picturesById = tOrderService.getPicturesByProposerDataId(tProposerDataVO.getId());

// 获取当前类的所在项目路径
   File file = null;
   try {
     file = new File(ResourceUtils.getURL("classpath:").getPath());
   } catch (FileNotFoundException e) {
     throw new RuntimeException("获取根目录失败,无法获取文件目录!");
   }
   if(!file.exists()) {
     file = new File("");
   }
   String absolutePath = file.getAbsolutePath();

// 要打包的文件夹列表
   String order_number = picturesById.getOrder_number();
   String country_name = picturesById.getCountry_name();
   String visa_type = picturesById.getVisa_type();
   String dirName = order_number + country_name + visa_type;

// ************* 2. 创建要压缩的文件夹 *************
   // 根据订单号+国家名称+签证类型创建文件夹
   File dirOfOrder = new File(absolutePath, dirName);
   if(!dirOfOrder.exists()) {
     dirOfOrder.mkdirs();
   }

ZipOutputStream zos = null;
   OutputStream out = null;

long readyEnd = System.currentTimeMillis();
   System.out.println("准备完成,耗时:" + (readyEnd - readyStart) + " ms");
   try {

long downStart = System.currentTimeMillis();

System.out.println("开始下载");

TProposerDataVO vo = picturesById.getProposerDataVO();

// ************* 3. 根据获取到的图片的url,把图片按照想要的文件夹目录进行下载 *************
     // 根据申请人姓名创建文件夹
     File proposerFile = new File(dirOfOrder, vo.getReal_name());
     if (!proposerFile.exists()) {
       proposerFile.mkdirs();
     }
     // 下载申请人照片
     if (StringUtil.checkNotNull(vo.getPhoto_url())) {
       System.out.println("开始下载申请人照片");
       WordExportUtil.downloadHttpUrl(DOMAIN + vo.getPhoto_url(), proposerFile.toString(), File.separator + "photo.jpg");
     }
     // 下载申请人护照首页
     if (StringUtil.checkNotNull(vo.getPassport_home_page_url())) {
       System.out.println("开始下载申请人护照照片");
       WordExportUtil.downloadHttpUrl(DOMAIN + vo.getPassport_home_page_url(), proposerFile.toString(), File.separator + "passport.jpg");
     }
     // 下载申请人户口本照片
     if (StringUtil.checkNotNull(vo.getResidence_booklet_url())) {
       System.out.println("开始下载申请人户口本照片");
       String[] booklets = vo.getResidence_booklet_url().split(",");
       // 创建户口本照片文件夹
       File bookletsFile = new File(proposerFile, "hukouben");
       if (!bookletsFile.exists()) {
         bookletsFile.mkdirs();
       }
       for (int k = 0; k < booklets.length; k++) {
         WordExportUtil.downloadHttpUrl(DOMAIN + booklets[k], bookletsFile.toString(), File.separator + "residenceBooklet" + k + ".jpg");
       }
     }
     // 下载申请人身份证照片
     if (StringUtil.checkNotNull(vo.getId_card_status()) && vo.getId_card_status() == 0) {
       System.out.println("开始下载申请人身份证照片");
       // 创建身份证照片文件夹
       File idCards = new File(proposerFile, "idCards");
       if (!idCards.exists()) {
         idCards.mkdirs();
       }
       if (StringUtil.checkNotNull(vo.getId_card_positive_url())) {
         WordExportUtil.downloadHttpUrl(DOMAIN + vo.getId_card_positive_url(), idCards.toString(), File.separator + "idCardPostive.jpg");
       }
       if (StringUtil.checkNotNull(vo.getId_card_reverse_url())) {
         WordExportUtil.downloadHttpUrl(DOMAIN + vo.getId_card_reverse_url(), idCards.toString(), File.separator + "idCardReverse.jpg");
       }
     }
     // 下载申请人婚姻证明照片
     if (StringUtil.checkNotNull(vo.getMar_div_card_url())) {
       System.out.println("开始下载申请人婚姻证明照片");
       WordExportUtil.downloadHttpUrl(DOMAIN + vo.getMar_div_card_url(), proposerFile.toString(), File.separator + "marriage.jpg");
     }
     // 下载申请人辅助资产照片
     if (StringUtil.checkNotNull(vo.getAuxiliary_assets_url())) {
       System.out.println("开始下载申请人辅助资产照片");
       String[] auxiliarys = vo.getAuxiliary_assets_url().split(",");
       // 创建辅助资产照片文件夹
       File auxiliarysFile = new File(proposerFile, "fuzhuzichan");
       if (!auxiliarysFile.exists()) {
         auxiliarysFile.mkdirs();
       }
       for (int k = 0; k < auxiliarys.length; k++) {
         WordExportUtil.downloadHttpUrl(DOMAIN + auxiliarys[k], auxiliarysFile.toString(), File.separator + "auxiliary" + k + ".jpg");
       }
     }
     // 下载申请人居住证照片
     if (StringUtil.checkNotNull(vo.getResidence_permit_url())) {
       System.out.println("开始下载申请人居住证照片");
       String[] residences = vo.getResidence_permit_url().split(",");
       // 创建居住证照片文件夹
       File residencesFile = new File(proposerFile, "juzhuzheng");
       if (!residencesFile.exists()) {
         residencesFile.mkdirs();
       }
       for (int k = 0; k < residences.length; k++) {
         WordExportUtil.downloadHttpUrl(DOMAIN + residences[k], residencesFile.toString(), File.separator + "residence" + k + ".jpg");
       }
     }
     // 下载申请人其余补充资料照片
     if (StringUtil.checkNotNull(vo.getOther_data_url())) {
       System.out.println("开始下载申请人其余补充资料照片");
       String[] others = vo.getOther_data_url().split(",");
       // 创建其余补充资料照片文件夹
       File othersFile = new File(proposerFile, "qitabuchongziliao");
       if (!othersFile.exists()) {
         othersFile.mkdirs();
       }
       for (int k = 0; k < others.length; k++) {
         WordExportUtil.downloadHttpUrl(DOMAIN + others[k], othersFile.toString(), File.separator + "other" + k + ".jpg");
       }
     }
     // 下载申请人证明资料照片
     if (StringUtil.checkNotNull(vo.getProve_url())) {
       System.out.println("开始下载申请人证明资料照片");
       String[] prove_urls = vo.getProve_url().split(",");
       // 创建证明资料照片文件夹
       File proveFile = new File(proposerFile, "zhengmingziliao");
       if (!proveFile.exists()) {
         proveFile.mkdirs();
       }
       for (int k = 0; k < prove_urls.length; k++) {
         WordExportUtil.downloadHttpUrl(DOMAIN + prove_urls[k], proveFile.toString(), File.separator + "prove" + k + ".jpg");
       }
     }

long downEnd = System.currentTimeMillis();
     System.out.println("下载完成,耗时:" + (downEnd - downStart) + " ms");
     long zipStart = System.currentTimeMillis();

response.setContentType("application/x-zip-compressed");
     response.setHeader("Content-disposition", "attachment;filename=" + StringUtil.getUUID() + ".zip");
     out = response.getOutputStream();
     zos = new ZipOutputStream(out);

// ************* 4. 把要压缩的文件夹路径、压缩文件输出流传入到ZipUtils.toZip方法,对文件夹进行压缩 *************
     // 对文件夹进行压缩,保留原文件夹路径
     ZipUtils.toZip(dirOfOrder, out, true);
     long zipEnd = System.currentTimeMillis();
     System.out.println("压缩完成,耗时:" + (zipEnd - zipStart) + " ms");

out.flush();
   } catch (IOException e) {
     e.printStackTrace();
   } catch (Exception e) {
     throw new RuntimeException("zip error from ZipUtils", e);
   } finally {
     if (zos != null) {
       try {
         zos.close();
       } catch (IOException e) {
         e.printStackTrace();
       }
     }
     if (out != null) {
       try {
         zos.close();
         out.close();
       } catch (IOException e) {
         e.printStackTrace();
       }
     }
   }

// ************* 5. 删除压缩前准备的中间文件 *************
   if (dirOfOrder != null) {
     try {
       FileUtils.deleteDirectory(dirOfOrder);
       System.out.println("中间文件已删除");
     } catch (IOException e) {
       e.printStackTrace();
       System.out.println("中间文件删除失败");
     }
   }
 }

来源:https://www.cnblogs.com/zeng1994/p/7862288.html

标签:Java,文件,压缩,zip
0
投稿

猜你喜欢

  • Java的内部类总结

    2022-08-13 01:33:41
  • C#保存listbox中数据到文本文件的方法

    2023-02-20 10:37:27
  • IDEA2020.1常用配置说明

    2023-01-09 02:11:50
  • Java使用Random类生成随机数示例

    2022-08-19 04:22:19
  • 详解Java设计模式之备忘录模式的使用

    2023-09-10 09:38:32
  • Spring如何使用注解的方式创建bean

    2022-01-29 03:45:49
  • C# WinForm实现图片浏览器

    2022-04-12 23:28:00
  • springMVC的生命周期详解

    2022-10-29 22:27:40
  • Struts2实现文件上传时显示进度条功能

    2021-10-13 05:22:22
  • C# FileStream实现大文件复制

    2021-06-14 11:00:53
  • Spring的refresh()方法相关异常解析

    2021-12-08 07:39:07
  • 深入理解Java设计模式之抽象工厂模式

    2023-11-28 12:11:34
  • Winform在DataGridView中显示图片

    2023-06-26 21:02:48
  • Android ListView 和ScroolView 出现onmeasure空指针的解决办法

    2021-11-25 18:19:03
  • C#条件拼接Expression<Func<T, bool>>的使用

    2023-07-11 22:29:18
  • C#回收机制之资源回收托管

    2022-03-04 13:18:20
  • SpringMVC实现上传下载文件

    2022-11-10 03:18:14
  • idea中Maven镜像源详细配置步骤记录(对所有项目)

    2022-12-17 14:27:56
  • Unity 使用TexturePacker打包图集的操作方法

    2021-08-30 18:48:17
  • java调用openoffice将office系列文档转换为PDF的示例方法

    2023-11-29 11:42:53
  • asp之家 软件编程 m.aspxhome.com