zoukankan      html  css  js  c++  java
  • Java实现将文件或者文件夹压缩成zip

    转自:https://www.cnblogs.com/zeng1994/p/7862288.html 有改动

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

            这个工具类的功能为:
        • (1)可以压缩文件,也可以压缩文件夹
        • (2)同时支持压缩多级文件夹,工具内部做了递归处理
        • (3)碰到空的文件夹,也可以压缩
        • (4)可以选择是否保留原来的目录结构,如果不保留,所有文件跑压缩包根目录去了,且空文件夹直接舍弃。注意:如果不保留文件原来目录结构,在碰到文件名相同的文件时,会压缩失败。
        • (5)代码中提供了2个压缩文件的方法,一个的输入参数为文件夹路径,一个为文件列表,可根据实际需求选择方法。
          
            下面直接上代码

    一、代码

    ZipUtils

      1 import java.io.File;
      2 import java.io.FileInputStream;
      3 import java.io.FileOutputStream;
      4 import java.io.IOException;
      5 import java.io.OutputStream;
      6 import java.util.ArrayList;
      7 import java.util.List;
      8 import java.util.zip.ZipEntry;
      9 import java.util.zip.ZipOutputStream;
     10 /**
     11  * @author Nemo
     12  * @version 1.0
     13  * @date 2019/11/5
     14  */
     15 public class ZipUtils {
     16     private static final int  BUFFER_SIZE = 2 * 1024;
     17     /**
     18      * 压缩成ZIP 方法1
     19      * @param sourceFile 压缩文件夹路径
     20      * @param out    压缩文件输出流
     21      * @param KeepDirStructure  是否保留原来的目录结构,true:保留目录结构;
     22      *                          false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
     23      * @throws RuntimeException 压缩失败会抛出运行时异常
     24      */
     25     public static void toZip(File sourceFile, OutputStream out, boolean KeepDirStructure)
     26             throws RuntimeException{
     27         ZipOutputStream zos = null ;
     28         try {
     29             zos = new ZipOutputStream(out);
     30             compress(sourceFile,zos,sourceFile.getName(),KeepDirStructure);
     31         } catch (Exception e) {
     32             throw new RuntimeException("zip error from ZipUtils",e);
     33         }finally{
     34             if(zos != null){
     35                 try {
     36                     zos.close();
     37                 } catch (IOException e) {
     38                     e.printStackTrace();
     39                 }
     40             }
     41         }
     42     }
     43     /**
     44      * 压缩成ZIP 方法2
     45      * @param srcFiles 需要压缩的文件列表
     46      * @param out           压缩文件输出流
     47      * @throws RuntimeException 压缩失败会抛出运行时异常
     48      */
     49     public static void toZip(List<File> srcFiles , OutputStream out)throws RuntimeException {
     50         long start = System.currentTimeMillis();
     51         ZipOutputStream zos = null ;
     52         try {
     53             zos = new ZipOutputStream(out);
     54             for (File srcFile : srcFiles) {
     55                 byte[] buf = new byte[BUFFER_SIZE];
     56                 zos.putNextEntry(new ZipEntry(srcFile.getName()));
     57                 int len;
     58                 FileInputStream in = new FileInputStream(srcFile);
     59                 while ((len = in.read(buf)) != -1){
     60                     zos.write(buf, 0, len);
     61                 }
     62                 zos.closeEntry();
     63                 in.close();
     64             }
     65             long end = System.currentTimeMillis();
     66             System.out.println("压缩完成,耗时:" + (end - start) +" ms");
     67         } catch (Exception e) {
     68             throw new RuntimeException("zip error from ZipUtils",e);
     69         }finally{
     70             if(zos != null){
     71                 try {
     72                     zos.close();
     73                 } catch (IOException e) {
     74                     e.printStackTrace();
     75                 }
     76             }
     77         }
     78     }
     79     /**
     80      * 递归压缩方法
     81      * @param sourceFile 源文件
     82      * @param zos        zip输出流
     83      * @param name       压缩后的名称
     84      * @param KeepDirStructure  是否保留原来的目录结构,true:保留目录结构;
     85      *                          false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
     86      * @throws Exception
     87      */
     88     private static void compress(File sourceFile, ZipOutputStream zos, String name,
     89                                  boolean KeepDirStructure) throws Exception{
     90         byte[] buf = new byte[BUFFER_SIZE];
     91         if(sourceFile.isFile()){
     92             // 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
     93             zos.putNextEntry(new ZipEntry(name));
     94             // copy文件到zip输出流中
     95             int len;
     96             FileInputStream in = new FileInputStream(sourceFile);
     97             while ((len = in.read(buf)) != -1){
     98                 zos.write(buf, 0, len);
     99             }
    100             // Complete the entry
    101             zos.closeEntry();
    102             in.close();
    103         } else {
    104             File[] listFiles = sourceFile.listFiles();
    105             if(listFiles == null || listFiles.length == 0){
    106                 // 需要保留原来的文件结构时,需要对空文件夹进行处理
    107                 if(KeepDirStructure){
    108                     // 空文件夹的处理
    109                     zos.putNextEntry(new ZipEntry(name + "/"));
    110                     // 没有文件,不需要文件的copy
    111                     zos.closeEntry();
    112                 }
    113             }else {
    114                 for (File file : listFiles) {
    115                     // 判断是否需要保留原来的文件结构
    116                     if (KeepDirStructure) {
    117                         // 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
    118                         // 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
    119                         compress(file, zos, name + "/" + file.getName(),KeepDirStructure);
    120                     } else {
    121                         compress(file, zos, file.getName(),KeepDirStructure);
    122                     }
    123                 }
    124             }
    125         }
    126     }
    127     public static void main(String[] args) throws Exception {
    128         /** 测试压缩方法1  */
    129         FileOutputStream fos1 = new FileOutputStream(new File("c:/mytest01.zip"));
    130         ZipUtils.toZip(new File("D:/log"), fos1,true);
    131         /** 测试压缩方法2  */
    132         List<File> fileList = new ArrayList<>();
    133         fileList.add(new File("D:/Java/jdk1.7.0_45_64bit/bin/jar.exe"));
    134         fileList.add(new File("D:/Java/jdk1.7.0_45_64bit/bin/java.exe"));
    135         FileOutputStream fos2 = new FileOutputStream(new File("c:/mytest02.zip"));
    136         ZipUtils.toZip(fileList, fos2);
    137     }
    138 }

    二、注意事项

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

            (1)支持选择是否保留原来的文件目录结构,如果不保留,那么空文件夹直接不用处理。
            (1)碰到空文件夹时,如果需要保留目录结构,则直接添加个ZipEntry就可以了,不过就是这个entry的名字后面需要带上一斜杠(/)表示这个是目录。
            (2)递归时,不需要把zip输出流关闭,zip输出流的关闭应该是在调用完递归方法后面关闭
            (3)递归时,如果是个文件夹且需要保留目录结构,那么在调用方法压缩他的子文件时,需要把文件夹的名字加一斜杠给添加到子文件名字前面,这样压缩后才有多级目录
     

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

       代码中的步骤为:
        1. 获取到存在数据库中的图片的url
        2. 创建要压缩的文件夹
        3. 根据获取到的图片的url,把图片按照想要的文件夹目录进行下载
        4. 把要压缩的文件夹路径、压缩文件输出流传入到ZipUtils.toZip方法,对文件夹进行压缩
        5. 删除压缩前准备的中间文件
      其中下载文件的方法在https://www.cnblogs.com/nemowang1996/p/11603848.html
      因为接口是GET请求,所以直接拼接接口路由+参数,用浏览器打开就能弹出下载。
      1 import org.apache.commons.io.FileUtils;
      2 import java.io.*;
      3 
      4 /**
      5      * 图片打包下载
      6      * @author: wangzhouchao
      7      */
      8     @ApiImplicitParams({
      9             @ApiImplicitParam(name = "id", value = "申请人id", required = true, dataType = "Long", paramType = "query"),
     10     })
     11     @ApiOperation(value = "图片打包下载", notes = "图片打包下载")
     12     @RequestMapping(value = "/downloadPictureList", method = RequestMethod.GET)
     13     public void downloadPictureList(TProposerDataVO tProposerDataVO) {
     14 
     15         long readyStart = System.currentTimeMillis();
     16 
     17         // *************  1. 获取到存在数据库中的图片的url  *************
     18         PictureDownloadVO picturesById = tOrderService.getPicturesByProposerDataId(tProposerDataVO.getId());
     19 
     20         // 获取当前类的所在项目路径
     21         File file = null;
     22         try {
     23             file = new File(ResourceUtils.getURL("classpath:").getPath());
     24         } catch (FileNotFoundException e) {
     25             throw new RuntimeException("获取根目录失败,无法获取文件目录!");
     26         }
     27         if(!file.exists()) {
     28             file = new File("");
     29         }
     30         String absolutePath = file.getAbsolutePath();
     31 
     32 
     33         // 要打包的文件夹列表
     34         String order_number = picturesById.getOrder_number();
     35         String country_name = picturesById.getCountry_name();
     36         String visa_type = picturesById.getVisa_type();
     37         String dirName = order_number + country_name + visa_type;
     38 
     39         // *************  2. 创建要压缩的文件夹  *************
     40         // 根据订单号+国家名称+签证类型创建文件夹
     41         File dirOfOrder = new File(absolutePath, dirName);
     42         if(!dirOfOrder.exists()) {
     43             dirOfOrder.mkdirs();
     44         }
     45 
     46         ZipOutputStream zos = null;
     47         OutputStream out = null;
     48 
     49         long readyEnd = System.currentTimeMillis();
     50         System.out.println("准备完成,耗时:" + (readyEnd - readyStart) + " ms");
     51         try {
     52 
     53             long downStart = System.currentTimeMillis();
     54 
     55 
     56             System.out.println("开始下载");
     57 
     58             TProposerDataVO vo = picturesById.getProposerDataVO();
     59 
     60             // *************  3. 根据获取到的图片的url,把图片按照想要的文件夹目录进行下载  *************
     61             // 根据申请人姓名创建文件夹
     62             File proposerFile = new File(dirOfOrder, vo.getReal_name());
     63             if (!proposerFile.exists()) {
     64                 proposerFile.mkdirs();
     65             }
     66             // 下载申请人照片
     67             if (StringUtil.checkNotNull(vo.getPhoto_url())) {
     68                 System.out.println("开始下载申请人照片");
     69                 WordExportUtil.downloadHttpUrl(DOMAIN + vo.getPhoto_url(), proposerFile.toString(), File.separator + "photo.jpg");
     70             }
     71             // 下载申请人护照首页
     72             if (StringUtil.checkNotNull(vo.getPassport_home_page_url())) {
     73                 System.out.println("开始下载申请人护照照片");
     74                 WordExportUtil.downloadHttpUrl(DOMAIN + vo.getPassport_home_page_url(), proposerFile.toString(), File.separator + "passport.jpg");
     75             }
     76             // 下载申请人户口本照片
     77             if (StringUtil.checkNotNull(vo.getResidence_booklet_url())) {
     78                 System.out.println("开始下载申请人户口本照片");
     79                 String[] booklets = vo.getResidence_booklet_url().split(",");
     80                 // 创建户口本照片文件夹
     81                 File bookletsFile = new File(proposerFile, "hukouben");
     82                 if (!bookletsFile.exists()) {
     83                     bookletsFile.mkdirs();
     84                 }
     85                 for (int k = 0; k < booklets.length; k++) {
     86                     WordExportUtil.downloadHttpUrl(DOMAIN + booklets[k], bookletsFile.toString(), File.separator + "residenceBooklet" + k + ".jpg");
     87                 }
     88             }
     89             // 下载申请人身份证照片
     90             if (StringUtil.checkNotNull(vo.getId_card_status()) && vo.getId_card_status() == 0) {
     91                 System.out.println("开始下载申请人身份证照片");
     92                 // 创建身份证照片文件夹
     93                 File idCards = new File(proposerFile, "idCards");
     94                 if (!idCards.exists()) {
     95                     idCards.mkdirs();
     96                 }
     97                 if (StringUtil.checkNotNull(vo.getId_card_positive_url())) {
     98                     WordExportUtil.downloadHttpUrl(DOMAIN + vo.getId_card_positive_url(), idCards.toString(), File.separator + "idCardPostive.jpg");
     99                 }
    100                 if (StringUtil.checkNotNull(vo.getId_card_reverse_url())) {
    101                     WordExportUtil.downloadHttpUrl(DOMAIN + vo.getId_card_reverse_url(), idCards.toString(), File.separator + "idCardReverse.jpg");
    102                 }
    103             }
    104             // 下载申请人婚姻证明照片
    105             if (StringUtil.checkNotNull(vo.getMar_div_card_url())) {
    106                 System.out.println("开始下载申请人婚姻证明照片");
    107                 WordExportUtil.downloadHttpUrl(DOMAIN + vo.getMar_div_card_url(), proposerFile.toString(), File.separator + "marriage.jpg");
    108             }
    109             // 下载申请人辅助资产照片
    110             if (StringUtil.checkNotNull(vo.getAuxiliary_assets_url())) {
    111                 System.out.println("开始下载申请人辅助资产照片");
    112                 String[] auxiliarys = vo.getAuxiliary_assets_url().split(",");
    113                 // 创建辅助资产照片文件夹
    114                 File auxiliarysFile = new File(proposerFile, "fuzhuzichan");
    115                 if (!auxiliarysFile.exists()) {
    116                     auxiliarysFile.mkdirs();
    117                 }
    118                 for (int k = 0; k < auxiliarys.length; k++) {
    119                     WordExportUtil.downloadHttpUrl(DOMAIN + auxiliarys[k], auxiliarysFile.toString(), File.separator + "auxiliary" + k + ".jpg");
    120                 }
    121             }
    122             // 下载申请人居住证照片
    123             if (StringUtil.checkNotNull(vo.getResidence_permit_url())) {
    124                 System.out.println("开始下载申请人居住证照片");
    125                 String[] residences = vo.getResidence_permit_url().split(",");
    126                 // 创建居住证照片文件夹
    127                 File residencesFile = new File(proposerFile, "juzhuzheng");
    128                 if (!residencesFile.exists()) {
    129                     residencesFile.mkdirs();
    130                 }
    131                 for (int k = 0; k < residences.length; k++) {
    132                     WordExportUtil.downloadHttpUrl(DOMAIN + residences[k], residencesFile.toString(), File.separator + "residence" + k + ".jpg");
    133                 }
    134             }
    135             // 下载申请人其余补充资料照片
    136             if (StringUtil.checkNotNull(vo.getOther_data_url())) {
    137                 System.out.println("开始下载申请人其余补充资料照片");
    138                 String[] others = vo.getOther_data_url().split(",");
    139                 // 创建其余补充资料照片文件夹
    140                 File othersFile = new File(proposerFile, "qitabuchongziliao");
    141                 if (!othersFile.exists()) {
    142                     othersFile.mkdirs();
    143                 }
    144                 for (int k = 0; k < others.length; k++) {
    145                     WordExportUtil.downloadHttpUrl(DOMAIN + others[k], othersFile.toString(), File.separator + "other" + k + ".jpg");
    146                 }
    147             }
    148             // 下载申请人证明资料照片
    149             if (StringUtil.checkNotNull(vo.getProve_url())) {
    150                 System.out.println("开始下载申请人证明资料照片");
    151                 String[] prove_urls = vo.getProve_url().split(",");
    152                 // 创建证明资料照片文件夹
    153                 File proveFile = new File(proposerFile, "zhengmingziliao");
    154                 if (!proveFile.exists()) {
    155                     proveFile.mkdirs();
    156                 }
    157                 for (int k = 0; k < prove_urls.length; k++) {
    158                     WordExportUtil.downloadHttpUrl(DOMAIN + prove_urls[k], proveFile.toString(), File.separator + "prove" + k + ".jpg");
    159                 }
    160             }
    161 
    162             long downEnd = System.currentTimeMillis();
    163             System.out.println("下载完成,耗时:" + (downEnd - downStart) + " ms");
    164             long zipStart = System.currentTimeMillis();
    165 
    166             response.setContentType("application/x-zip-compressed");
    167             response.setHeader("Content-disposition", "attachment;filename=" + StringUtil.getUUID() + ".zip");
    168             out = response.getOutputStream();
    169             zos = new ZipOutputStream(out);
    170 
    171             // *************  4. 把要压缩的文件夹路径、压缩文件输出流传入到ZipUtils.toZip方法,对文件夹进行压缩  *************
    172             // 对文件夹进行压缩,保留原文件夹路径
    173             ZipUtils.toZip(dirOfOrder, out, true);
    174             long zipEnd = System.currentTimeMillis();
    175             System.out.println("压缩完成,耗时:" + (zipEnd - zipStart) + " ms");
    176 
    177             out.flush();
    178         } catch (IOException e) {
    179             e.printStackTrace();
    180         } catch (Exception e) {
    181             throw new RuntimeException("zip error from ZipUtils", e);
    182         } finally {
    183             if (zos != null) {
    184                 try {
    185                     zos.close();
    186                 } catch (IOException e) {
    187                     e.printStackTrace();
    188                 }
    189             }
    190             if (out != null) {
    191                 try {
    192                     zos.close();
    193                     out.close();
    194                 } catch (IOException e) {
    195                     e.printStackTrace();
    196                 }
    197             }
    198         }
    199 
    200         // *************  5. 删除压缩前准备的中间文件  *************
    201         if (dirOfOrder != null) {
    202             try {
    203                 FileUtils.deleteDirectory(dirOfOrder);
    204                 System.out.println("中间文件已删除");
    205             } catch (IOException e) {
    206                 e.printStackTrace();
    207                 System.out.println("中间文件删除失败");
    208             }
    209         }
    210     }
     
  • 相关阅读:
    [转]使用RDLC报表
    WPF 导出Excel 导出图片
    细说WPF自定义路由事件
    [转]WPF更换主题
    推荐5款超实用的.NET性能分析工具
    Hibernate框架
    C#的protected internal
    C#的类修饰符和成员修饰符
    C#的foreach
    C#接口
  • 原文地址:https://www.cnblogs.com/nemowang1996/p/11798595.html
Copyright © 2011-2022 走看看