-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathZipUtil.java
More file actions
92 lines (77 loc) · 3.33 KB
/
Copy pathZipUtil.java
File metadata and controls
92 lines (77 loc) · 3.33 KB
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
package top.thinkin.lightd.kit;
import java.io.*;
import java.nio.file.Paths;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class ZipUtil {
public static void compressDirectoryToZipFile(final String rootDir, final String sourceDir,
final ZipOutputStream zos) throws IOException {
final String dir = Paths.get(rootDir, sourceDir).toString();
final File[] files = requireNonNull(new File(dir).listFiles(), "files");
for (final File file : files) {
if (file.isDirectory()) {
compressDirectoryToZipFile(rootDir, Paths.get(sourceDir, file.getName()).toString(), zos);
} else {
zos.putNextEntry(new ZipEntry(Paths.get(sourceDir, file.getName()).toString()));
try (final FileInputStream in = new FileInputStream(Paths.get(rootDir, sourceDir, file.getName())
.toString())) {
copy(in, zos);
}
}
}
}
public static int copy(InputStream input, OutputStream output) throws IOException {
long count = copyLarge(input, output);
return count > 2147483647L ? -1 : (int) count;
}
public static long copyLarge(InputStream input, OutputStream output) throws IOException {
return copyLarge(input, output, new byte[4096]);
}
public static long copyLarge(InputStream input, OutputStream output, byte[] buffer) throws IOException {
long count = 0L;
int n;
for (boolean var5 = false; -1 != (n = input.read(buffer)); count += (long) n) {
output.write(buffer, 0, n);
}
return count;
}
public static <T> T requireNonNull(T obj, String message) {
if (obj == null) {
throw new NullPointerException(message);
}
return obj;
}
public static void unzipFile(final String sourceFile, final String outputDir) throws IOException {
try (final ZipInputStream zis = new ZipInputStream(new FileInputStream(sourceFile))) {
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
final String fileName = zipEntry.getName();
final File entryFile = new File(outputDir, fileName);
if (!entryFile.toPath().normalize().startsWith(outputDir)) {
throw new IOException("Bad zip entry");
}
forceMkdir(entryFile.getParentFile());
try (final FileOutputStream fos = new FileOutputStream(entryFile)) {
copy(zis, fos);
}
zipEntry = zis.getNextEntry();
}
zis.closeEntry();
}
}
public static void forceMkdir(File directory) throws IOException {
String message;
if (directory.exists()) {
if (!directory.isDirectory()) {
message = "File " + directory + " exists and is " + "not a directory. Unable to create directory.";
throw new IOException(message);
}
} else if (!directory.mkdirs() && !directory.isDirectory()) {
message = "Unable to create directory " + directory;
throw new IOException(message);
}
}
private ZipUtil() {
}
}