-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathArchiveUtil.java
More file actions
114 lines (95 loc) · 2.83 KB
/
Copy pathArchiveUtil.java
File metadata and controls
114 lines (95 loc) · 2.83 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* © Copyright IBM Corporation 2016.
* © Copyright HCL Technologies Ltd. 2017.
* LICENSE: Apache License, Version 2.0 https://www.apache.org/licenses/LICENSE-2.0
*/
package com.hcl.appscan.sdk.utils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import com.hcl.appscan.sdk.Messages;
public class ArchiveUtil {
private static final double TOO_BIG = 4e9; // 4GB
private static final int BUFFER_SIZE = 4096;
public static int MAX_PATH_LENGTH = 4096;
static {
if (SystemUtil.isWindows()) {
MAX_PATH_LENGTH = 256;
} else if (SystemUtil.isMac()) {
MAX_PATH_LENGTH = 1024;
} else {
MAX_PATH_LENGTH = 4096;
}
}
/**
* Unzip an archive.
*
* @param source An input file.
* @param dest The destination directory to unzip to.
* @throws IOException If an error occurs during the unzip operation.
*/
public static void unzip(File source, File destDir) throws IOException {
FileInputStream input = new FileInputStream(source);
ZipInputStream zip = new ZipInputStream(new BufferedInputStream(input));
destDir.mkdirs();
ZipEntry entry = null;
long bytesWritten = 0;
try {
while ((entry = zip.getNextEntry()) != null) {
String path = entry.getName();
File newFile = new File(destDir, path);
if (entry.isDirectory()) {
newFile.mkdirs();
}
else {
File parent = newFile.getParentFile();
parent.mkdirs();
if (parent.isDirectory())
bytesWritten += write(zip, newFile);
}
if (bytesWritten >= TOO_BIG)
throw new IOException(Messages.getMessage("err.too.big")); //$NON-NLS-1$
//Set 755 permissions
newFile.setExecutable(true, false);
newFile.setReadable(true, false);
newFile.setWritable(true);
}
}
finally {
if(zip != null)
zip.close();
}
}
/**
* Read from the input stream and write to the new file.
* The caller is responsible for closing the input stream when all reads from it are done.
*
* @param input The input stream.
* @param file The output file.
* @return The number of bytes written.
* @throws IOException if an error occurs during the write.
*/
private static long write(InputStream input, File file) throws IOException {
BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(file));
long bytesWritten = 0;
int len = 0;
byte[] buffer = new byte[BUFFER_SIZE];
try {
while ((len = input.read(buffer)) != -1) {
output.write(buffer, 0, len);
bytesWritten += len;
}
return bytesWritten;
}
finally {
if(output != null)
output.close();
}
}
}