• 【转】Java压缩和解压文件工具类ZipUtil


    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过。如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/mao2080/
      1 package com.utility.zip;
      2  
      3 import java.io.BufferedInputStream;
      4 import java.io.BufferedOutputStream;
      5 import java.io.File;
      6 import java.io.FileInputStream;
      7 import java.io.FileOutputStream;
      8 import java.io.IOException;
      9 import java.util.zip.ZipEntry;
     10 import java.util.zip.ZipInputStream;
     11 import java.util.zip.ZipOutputStream;
     12  
     13 import com.utility.io.IOUtil;
     14  
     15 /**
     16  * 通过Java的Zip输入输出流实现压缩和解压文件
     17  * 
     18  * @author liujiduo
     19  * 
     20  */
     21 public final class ZipUtil {
     22  
     23     private ZipUtil() {
     24         // empty
     25     }
     26  
     27     /**
     28      * 压缩文件
     29      * 
     30      * @param filePath
     31      *            待压缩的文件路径
     32      * @return 压缩后的文件
     33      */
     34     public static File zip(String filePath) {
     35         File target = null;
     36         File source = new File(filePath);
     37         if (source.exists()) {
     38             // 压缩文件名=源文件名.zip
     39             String zipName = source.getName() + ".zip";
     40             target = new File(source.getParent(), zipName);
     41             if (target.exists()) {
     42                 target.delete(); // 删除旧的文件
     43             }
     44             FileOutputStream fos = null;
     45             ZipOutputStream zos = null;
     46             try {
     47                 fos = new FileOutputStream(target);
     48                 zos = new ZipOutputStream(new BufferedOutputStream(fos));
     49                 // 添加对应的文件Entry
     50                 addEntry("/", source, zos);
     51             } catch (IOException e) {
     52                 throw new RuntimeException(e);
     53             } finally {
     54                 IOUtil.closeQuietly(zos, fos);
     55             }
     56         }
     57         return target;
     58     }
     59  
     60     /**
     61      * 扫描添加文件Entry
     62      * 
     63      * @param base
     64      *            基路径
     65      * 
     66      * @param source
     67      *            源文件
     68      * @param zos
     69      *            Zip文件输出流
     70      * @throws IOException
     71      */
     72     private static void addEntry(String base, File source, ZipOutputStream zos)
     73             throws IOException {
     74         // 按目录分级,形如:/aaa/bbb.txt
     75         String entry = base + source.getName();
     76         if (source.isDirectory()) {
     77             for (File file : source.listFiles()) {
     78                 // 递归列出目录下的所有文件,添加文件Entry
     79                 addEntry(entry + "/", file, zos);
     80             }
     81         } else {
     82             FileInputStream fis = null;
     83             BufferedInputStream bis = null;
     84             try {
     85                 byte[] buffer = new byte[1024 * 10];
     86                 fis = new FileInputStream(source);
     87                 bis = new BufferedInputStream(fis, buffer.length);
     88                 int read = 0;
     89                 zos.putNextEntry(new ZipEntry(entry));
     90                 while ((read = bis.read(buffer, 0, buffer.length)) != -1) {
     91                     zos.write(buffer, 0, read);
     92                 }
     93                 zos.closeEntry();
     94             } finally {
     95                 IOUtil.closeQuietly(bis, fis);
     96             }
     97         }
     98     }
     99  
    100     /**
    101      * 解压文件
    102      * 
    103      * @param filePath
    104      *            压缩文件路径
    105      */
    106     public static void unzip(String filePath) {
    107         File source = new File(filePath);
    108         if (source.exists()) {
    109             ZipInputStream zis = null;
    110             BufferedOutputStream bos = null;
    111             try {
    112                 zis = new ZipInputStream(new FileInputStream(source));
    113                 ZipEntry entry = null;
    114                 while ((entry = zis.getNextEntry()) != null
    115                         && !entry.isDirectory()) {
    116                     File target = new File(source.getParent(), entry.getName());
    117                     if (!target.getParentFile().exists()) {
    118                         // 创建文件父目录
    119                         target.getParentFile().mkdirs();
    120                     }
    121                     // 写入文件
    122                     bos = new BufferedOutputStream(new FileOutputStream(target));
    123                     int read = 0;
    124                     byte[] buffer = new byte[1024 * 10];
    125                     while ((read = zis.read(buffer, 0, buffer.length)) != -1) {
    126                         bos.write(buffer, 0, read);
    127                     }
    128                     bos.flush();
    129                 }
    130                 zis.closeEntry();
    131             } catch (IOException e) {
    132                 throw new RuntimeException(e);
    133             } finally {
    134                 IOUtil.closeQuietly(zis, bos);
    135             }
    136         }
    137     }
    138  
    139     public static void main(String[] args) {
    140         String targetPath = "E:\Win7壁纸";
    141         File file = ZipUtil.zip(targetPath);
    142         System.out.println(file);
    143         ZipUtil.unzip("F:\Win7壁纸.zip");
    144     }
    145 }
     1 package com.utility.io;
     2  
     3 import java.io.Closeable;
     4 import java.io.IOException;
     5  
     6 /**
     7  * IO流工具类
     8  * 
     9  * @author liujiduo
    10  * 
    11  */
    12 public class IOUtil {
    13     /**
    14      * 关闭一个或多个流对象
    15      * 
    16      * @param closeables
    17      *            可关闭的流对象列表
    18      * @throws IOException
    19      */
    20     public static void close(Closeable... closeables) throws IOException {
    21         if (closeables != null) {
    22             for (Closeable closeable : closeables) {
    23                 if (closeable != null) {
    24                     closeable.close();
    25                 }
    26             }
    27         }
    28     }
    29  
    30     /**
    31      * 关闭一个或多个流对象
    32      * 
    33      * @param closeables
    34      *            可关闭的流对象列表
    35      */
    36     public static void closeQuietly(Closeable... closeables) {
    37         try {
    38             close(closeables);
    39         } catch (IOException e) {
    40             // do nothing
    41         }
    42     }
    43  
    44 }

    参考网站

    https://www.oschina.net/code/snippet_1021818_48130

  • 相关阅读:
    移植thinkPHP的dump()函数
    PHP生成linux命令行进度条
    没有ORM库的时候,通过PDO连接MySQL的方法
    mysql json字符串 解析成对应字段
    linux上安装并启动nginx
    linux上启动redis
    mui的input搜索框里的清除按钮的点击监听事件
    miniui 修改input样式及弹出框按钮文字
    js 删除数组元素的方法
    miniui反选
  • 原文地址:https://www.cnblogs.com/mao2080/p/7435268.html
Copyright © 2020-2023  润新知