【轉】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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM