-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileExtraction.java
49 lines (41 loc) · 1.4 KB
/
FileExtraction.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
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.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class FileExtraction {
/**
* Max extraction buffer...4 MB
*/
private static final int BUFFER_SIZE = 4096;
public static void unzip(String fileLocation, String destinationPath) throws IOException {
final File file = new File(fileLocation);
final BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));
final ZipInputStream zin = new ZipInputStream(input);
ZipEntry e;
while ((e = zin.getNextEntry()) != null) {
if (e.isDirectory()) {
(new File(destinationPath + e.getName())).mkdir();
} else {
if (e.getName().equals(fileLocation)) {
extract(zin, fileLocation);
break;
}
extract(zin, destinationPath + e.getName());
}
}
zin.close();
}
private static void extract(ZipInputStream zipIn, String filePath) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
byte[] bytesIn = new byte[BUFFER_SIZE];
int read = 0;
while ((read = zipIn.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
bos.close();
}
}