-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZipDecompressor.java
52 lines (43 loc) · 1.65 KB
/
ZipDecompressor.java
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
public class ZipDecompressor {
static void decompress(String inputFile, String outputDir) throws IOException {
final File outDir = new File(outputDir);
try (ZipInputStream zin = new ZipInputStream(new FileInputStream(inputFile))) {
ZipEntry entry;
String name, dir;
String fileName = null;
while ((entry = zin.getNextEntry()) != null) {
name = entry.getName();
if (fileName == null) {
fileName = name.replace(File.separator, "");
}
name = name.replace("/", File.separator);
name = name.replace("\\", File.separator);
if (entry.isDirectory()) {
mkdirs(outDir, name);
continue;
}
dir = dirPart(name);
if (dir != null)
mkdirs(outDir, dir);
extractFile(zin, outDir, name);
}
}
}
private static void mkdirs(File outDir, String path) {
File d = new File(outDir, path);
if (!d.exists())
d.mkdirs();
}
private static String dirPart(String name) {
File file = new File(name);
return file.getParent();
}
private static void extractFile(ZipInputStream in, File outDir, String name) throws IOException {
byte[] buffer = new byte[4096];
int count;
try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(outDir, name)))) {
while ((count = in.read(buffer)) != -1)
out.write(buffer, 0, count);
}
}
}