Java中zip和rar解压缩工具

在 java 开发中难免压缩包解压也很常用,今天写个工具类。

依赖的Jar

1
2
3
4
5
6
7
8
9
10
11
12
<!--zip 工具-->
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.9.7</version>
</dependency>
<!--rar 工具-->
<dependency>
<groupId>com.github.junrar</groupId>
<artifactId>junrar</artifactId>
<version>6.0.1</version>
</dependency>

工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import com.github.junrar.Archive;
import com.github.junrar.rarfile.FileHeader;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;

import java.io.*;
import java.util.Enumeration;


/**
* 解压缩工具类
*
* @author dgb8901
*/
public class ZipAndRarTools {

/**
* 解压rar文件
*
* @param sourcePath 解压的rar文件路径
* @param destPath 解压到的文件目录
* @throws Exception
*/
public static void unRar(String sourcePath, String destPath) throws Exception {
File sourceRar = new File(sourcePath);
File destDir = new File(destPath);
Archive archive = null;
FileOutputStream fos = null;
try {
archive = new Archive(new FileInputStream(sourceRar));
FileHeader fh = archive.nextFileHeader();
int count = 0;
File destFileName = null;
while (fh != null) {
++count;
System.out.println((count) + ") " + fh.getFileNameString());
String compressFileName = fh.getFileNameString().trim();
destFileName = new File(destDir.getAbsolutePath() + "/" + compressFileName);
if (fh.isDirectory()) {
if (!destFileName.exists()) {
destFileName.mkdirs();
}
fh = archive.nextFileHeader();
continue;
}
if (!destFileName.getParentFile().exists()) {
destFileName.getParentFile().mkdirs();
}

fos = new FileOutputStream(destFileName);
archive.extractFile(fh, fos);
fos.close();
fos = null;
fh = archive.nextFileHeader();
}
archive.close();
archive = null;
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
if (fos != null) {
fos.close();
fos = null;
}
if (archive != null) {
archive.close();
archive = null;
}
}
}


/**
* 解压Zip文件
*
* @param zipPath 解压缩的文件位置
* @param destPath 解压到某个路径
*/
public static void unZip(String zipPath, String destPath) {
String descFileNames = destPath;
if (!descFileNames.endsWith(File.separator)) {
descFileNames = descFileNames + File.separator;
}
try {
ZipFile zipFile = new ZipFile(zipPath);
ZipEntry entry = null;
String entryName = null;
String descFileDir = null;
byte[] buf = new byte[4096];
int readByte = 0;
Enumeration<ZipEntry> enums = zipFile.getEntries();
while (enums.hasMoreElements()) {
entry = enums.nextElement();
entryName = entry.getName();
descFileDir = descFileNames + entryName;
if (entry.isDirectory()) {
new File(descFileDir).mkdirs();
continue;
} else {
new File(descFileDir).getParentFile().mkdirs();
}
File file = new File(descFileDir);
OutputStream os = new FileOutputStream(file);
InputStream is = zipFile.getInputStream(entry);
while ((readByte = is.read(buf)) != -1) {
os.write(buf, 0, readByte);
}
os.close();
is.close();
}
zipFile.close();
} catch (Exception e) {
e.printStackTrace();
}

}

}

原文作者: dgb8901,yinxing

原文链接: https://www.itwork.club/2020/07/23/zip-rar/

版权声明: 转载请注明出处

为您推荐

体验小程序「跟我记账」

关注公众号「特想学英语」