-
Notifications
You must be signed in to change notification settings - Fork 7
/
package_downloader.java
executable file
·235 lines (107 loc) · 3.41 KB
/
package_downloader.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import com.cycling74.max.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class package_downloader extends MaxObject
{
public void unzip(String zipfile, String outputfolder, String packagename, String relativepath)
{
String rootFolder = unZipIt(zipfile, outputfolder);
File from = null;
//rename package
if (relativepath.equals("None"))
{
from = new File(outputfolder + File.separator + rootFolder);
}
else
{
from = new File(outputfolder + File.separator + rootFolder + File.separator + relativepath);
}
File to = new File(outputfolder + File.separator + packagename);
//if package was already there, delete it
if (!rootFolder.equals(packagename + "/"))
{
if (to.exists())
{
deleteFolder(to);
}
from.renameTo(to);
}
//if package was in subfolder, delete parent folder
if (!relativepath.equals("None"))
{
File dir_to_delete = new File(outputfolder + File.separator + rootFolder);
deleteFolder(dir_to_delete);
}
//delete zip file
File to_delete = new File(zipfile);
to_delete.delete();
}
public static void deleteFolder(File folder)
{
File[] files = folder.listFiles();
if(files!=null) { //some JVMs return null for empty dirs
for(File f: files) {
if(f.isDirectory()) {
deleteFolder(f);
} else {
f.delete();
}
}
}
folder.delete();
}
public String unZipIt(String zipFile, String outputFolder)
{
byte[] buffer = new byte[1024];
String rootFolder = "None";
try{
//create output directory is not exists
File folder = new File(outputFolder);
if(!folder.exists()){
folder.mkdir();
}
//get the zip file content
ZipInputStream zis =
new ZipInputStream(new FileInputStream(zipFile));
//get the zipped file list entry
ZipEntry ze = zis.getNextEntry();
rootFolder = ze.getName();
while(ze!=null){
String fileName = ze.getName();
File newFile = new File(outputFolder + File.separator + fileName);
//System.out.println("file unzip : "+ newFile.getAbsoluteFile());
//create all non exists folders
//else you will hit FileNotFoundException for compressed folder
if(ze.isDirectory())
{
new File(newFile.getParent()).mkdirs();
}
else
{
FileOutputStream fos = null;
new File(newFile.getParent()).mkdirs();
fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0)
{
fos.write(buffer, 0, len);
}
fos.close();
}
ze = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
System.out.println("Unzipped " + zipFile + " package to " + outputFolder);
outlet(0,"bang");
}catch(IOException ex){
ex.printStackTrace();
}
return rootFolder;
}
}