-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnzip.java
80 lines (78 loc) · 2.29 KB
/
Unzip.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
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
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.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class Unzip {
final static int BUFFER = 2048;
/**
* Unzip a zip archive file into a directory by the same
* name as the original zip file (minus the extension);
* @param f - File to unarchive
* @return Array of file names that were unpacked
*/
public static ArrayList<String> unzip(File f) throws IOException
{
ArrayList<String> unpackedFiles = new ArrayList<String>();
String subdir = f.getName();
int dotOffset = subdir.lastIndexOf('.');
if (dotOffset != -1)
{
subdir = subdir.substring(0, dotOffset);
}
File subdirFile = new File(f.getParent() + File.separator + subdir);
if (!subdirFile.isDirectory() && !subdirFile.mkdir())
{
throw new IOException("Could not create directory: " + subdirFile.getAbsolutePath());
}
/*
* From:
* http://java.sun.com/developer/technicalArticles/Programming/compression/
*/
BufferedOutputStream dest = null;
FileInputStream fis = new
FileInputStream(f);
ZipInputStream zis = new
ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
if (entry.isDirectory())
{
String newDirName = subdir + File.separator + entry.getName();
File newDir = new File(f.getParent() + File.separator + newDirName);
if (!newDir.exists())
{
newDir.mkdirs();
}
}
else
{
//System.out.println("Extracting: " +entry);
int count;
byte data[] = new byte[BUFFER];
// write the files to the disk
String newFileName = subdir + File.separator + entry.getName();
if (!entry.getName().startsWith("."))
{
unpackedFiles.add(newFileName);
}
File newFile = new File(f.getParent() + File.separator + newFileName);
newFile.getParentFile().mkdirs();
FileOutputStream fos = new FileOutputStream(newFile);
dest = new
BufferedOutputStream(fos, BUFFER);
while ((count = zis.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
}
}
zis.close();
return unpackedFiles;
}
}