-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunzip-files.java
More file actions
78 lines (64 loc) · 2.33 KB
/
unzip-files.java
File metadata and controls
78 lines (64 loc) · 2.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
package de.MarkusTieger;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Random;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class App {
private static int count = 0;
public static void main(String[] args) throws IOException {
File file = new File("challenge.zip");
unzip(file, "temp.zip");
}
public static void unzip(File target, String tmp) throws IOException {
count++;
System.out.println(count);
File temp = new File(tmp);
if(temp.exists()) Files.delete(temp.toPath());
FileInputStream in = new FileInputStream(target);
ZipInputStream zis = new ZipInputStream(in);
ZipEntry ze = zis.getNextEntry();
if(ze == null) throw new IOException("NULL");
if(ze.getName().toLowerCase().endsWith(".zip")) {
if(!temp.exists()) temp.createNewFile();
FileOutputStream o = new FileOutputStream(temp);
int len;
byte[] buffer = new byte[1024];
while((len = zis.read(buffer)) > 0){
o.write(buffer, 0, len);
o.flush();
}
zis.closeEntry();
zis.close();
o.close();
unzip(temp, temp.getName().equalsIgnoreCase("temp.zip") ? "tmp.zip" : "temp.zip");
} else {
if(!temp.exists()) temp.createNewFile();
FileOutputStream o = new FileOutputStream(temp);
int len;
byte[] buffer = new byte[1024];
while((len = zis.read(buffer)) > 0){
o.write(buffer, 0, len);
o.flush();
}
zis.closeEntry();
o.close();
temp.renameTo(new File("data" + new Random().nextInt()));
while((ze = zis.getNextEntry()) != null){
if(!temp.exists()) temp.createNewFile();
o = new FileOutputStream(temp);
while((len = zis.read(buffer)) > 0){
o.write(buffer, 0, len);
o.flush();
}
zis.closeEntry();
o.close();
temp.renameTo(new File("data" + new Random().nextInt()));
}
zis.close();
}
}
}