對zip壓縮包的解壓是比較常見的應用場景,java代碼的實現也很簡單。廢話不多說,直接上代碼吧
一、代碼
/**
* zip解壓
* @param srcFile zip源文件
* @param destDirPath 解壓后的目標文件夾
* @throws RuntimeException 解壓失敗會拋出運行時異常
*/
public static void unZip(File srcFile, String destDirPath) throws RuntimeException {
long start = System.currentTimeMillis();
// 判斷源文件是否存在
if (!srcFile.exists()) {
throw new RuntimeException(srcFile.getPath() + "所指文件不存在");
}
// 開始解壓
ZipFile zipFile = null;
try {
zipFile = new ZipFile(srcFile);
Enumeration<?> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
System.out.println("解壓" + entry.getName());
// 如果是文件夾,就創建個文件夾
if (entry.isDirectory()) {
String dirPath = destDirPath + "/" + entry.getName();
File dir = new File(dirPath);
dir.mkdirs();
} else {
// 如果是文件,就先創建一個文件,然后用io流把內容copy過去
File targetFile = new File(destDirPath + "/" + entry.getName());
// 保證這個文件的父文件夾必須要存在
if(!targetFile.getParentFile().exists()){
targetFile.getParentFile().mkdirs();
}
targetFile.createNewFile();
// 將壓縮文件內容寫入到這個文件中
InputStream is = zipFile.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(targetFile);
int len;
byte[] buf = new byte[BUFFER_SIZE];
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
// 關流順序,先打開的后關閉
fos.close();
is.close();
}
}
long end = System.currentTimeMillis();
System.out.println("解壓完成,耗時:" + (end - start) +" ms");
} catch (Exception e) {
throw new RuntimeException("unzip error from ZipUtils", e);
} finally {
if(zipFile != null){
try {
zipFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
1
/**
2
* zip解壓
3
* @param srcFile zip源文件
4
* @param destDirPath 解壓后的目標文件夾
5
* @throws RuntimeException 解壓失敗會拋出運行時異常
6
*/
7
public static void unZip(File srcFile, String destDirPath) throws RuntimeException {
8
long start = System.currentTimeMillis();
9
// 判斷源文件是否存在
10
if (!srcFile.exists()) {
11
throw new RuntimeException(srcFile.getPath() + "所指文件不存在");
12
}
13
// 開始解壓
14
ZipFile zipFile = null;
15
try {
16
zipFile = new ZipFile(srcFile);
17
Enumeration<?> entries = zipFile.entries();
18
while (entries.hasMoreElements()) {
19
ZipEntry entry = (ZipEntry) entries.nextElement();
20
System.out.println("解壓" + entry.getName());
21
// 如果是文件夾,就創建個文件夾
22
if (entry.isDirectory()) {
23
String dirPath = destDirPath + "/" + entry.getName();
24
File dir = new File(dirPath);
25
dir.mkdirs();
26
} else {
27
// 如果是文件,就先創建一個文件,然后用io流把內容copy過去
28
File targetFile = new File(destDirPath + "/" + entry.getName());
29
// 保證這個文件的父文件夾必須要存在
30
if(!targetFile.getParentFile().exists()){
31
targetFile.getParentFile().mkdirs();
32
}
33
targetFile.createNewFile();
34
// 將壓縮文件內容寫入到這個文件中
35
InputStream is = zipFile.getInputStream(entry);
36
FileOutputStream fos = new FileOutputStream(targetFile);
37
int len;
38
byte[] buf = new byte[BUFFER_SIZE];
39
while ((len = is.read(buf)) != -1) {
40
fos.write(buf, 0, len);
41
}
42
// 關流順序,先打開的后關閉
43
fos.close();
44
is.close();
45
}
46
}
47
long end = System.currentTimeMillis();
48
System.out.println("解壓完成,耗時:" + (end - start) +" ms");
49
} catch (Exception e) {
50
throw new RuntimeException("unzip error from ZipUtils", e);
51
} finally {
52
if(zipFile != null){
53
try {
54
zipFile.close();
55
} catch (IOException e) {
56
e.printStackTrace();
57
}
58
}
59
}
60
}
二、小結
解壓的代碼並不復雜,不過有個關鍵點是:創建文件時,需要保證該文件所在的文件夾必須存在。
如果對Java實現zip壓縮感興趣,可看我上一篇博客:
http://www.cnblogs.com/zeng1994/p/7862288.html