-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUnzipper_JavaTutorial.java
More file actions
58 lines (54 loc) · 2.01 KB
/
Copy pathUnzipper_JavaTutorial.java
File metadata and controls
58 lines (54 loc) · 2.01 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
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
*
* @author Vincent
*/
public class ZipInputStream_JavaTutorial {
public static void main(String[] args) {
Unzipper uz = new Unzipper("C:/Users/" + System.getProperty("user.name") + "/desktop/YourZipFileHere", "C:/Users/" + System.getProperty("user.name") + "/desktop");
}
private static class Unzipper {
public Unzipper(String in, String out) {
if (out == null) {
out = "";
} else {
out += File.separator;
}
File outputDirectory = new File(out);
if (outputDirectory.exists()) {
outputDirectory.delete();
}
outputDirectory.mkdir();
try {
ZipInputStream zip = new ZipInputStream(new FileInputStream(in));
ZipEntry contents = null;
int len;
byte[] buffer = new byte[1024];
while ((contents = zip.getNextEntry()) != null) {
if (!contents.isDirectory()) {
System.out.println("-" + contents.getName());
File file = new File(out + contents.getName());
if (!new File(file.getParent()).exists()) {
new File(file.getParent()).mkdirs();
}
FileOutputStream outStream = new FileOutputStream(file);
while ((len = zip.read(buffer)) > 0) {
outStream.write(buffer, 0, len);
}
outStream.close();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}