/
Directories.java
162 lines (145 loc) · 4.62 KB
/
Directories.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package net.ME1312.Galaxi.Library;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.util.LinkedList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public final class Directories {
private Directories() {}
/**
* Copy a Directory
*
* @param from Source
* @param to Destination
*/
public static void copy(File from, File to) {
if (from.isDirectory() && !Files.isSymbolicLink(from.toPath())) {
if (!to.exists()) {
to.mkdirs();
}
String files[] = from.list();
for (String file : files) {
File srcFile = new File(from, file);
File destFile = new File(to, file);
copy(srcFile, destFile);
}
} else {
try {
if (!to.exists()) Files.copy(from.toPath(), to.toPath(), LinkOption.NOFOLLOW_LINKS);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Search a Directory
*
* @param folder Location
* @return List of all found file paths
*/
public static List<String> search(File folder) {
return search(folder, folder);
}
private static List<String> search(File origin, File file) {
List<String> list = new LinkedList<String>();
if (file.isFile()) {
if (origin == file) {
list.add(file.getName());
} else {
list.add(file.getAbsolutePath().substring(origin.getAbsolutePath().length()+1));
}
}
if (file.isDirectory()) for (File next : file.listFiles()) {
list.addAll(search(origin, next));
}
return list;
}
/**
* Zip a Directory
*
* @param folder Location
* @param zip Zip Data Stream
*/
public static void zip(File folder, OutputStream zip) {
File dir = (folder.isFile())?folder.getParentFile():folder;
byte[] buffer = new byte[4096];
try {
ZipOutputStream zos = new ZipOutputStream(zip);
for (String next : search(folder)) {
zos.putNextEntry(new ZipEntry(next.replace(File.separatorChar, '/')));
FileInputStream in = new FileInputStream(dir.getAbsolutePath() + File.separator + next);
int len;
while ((len = in.read(buffer)) != -1) {
zos.write(buffer, 0, len);
}
in.close();
}
zos.closeEntry();
zos.close();
} catch(IOException ex) {
ex.printStackTrace();
}
}
/**
* Unzip a Directory
*
* @param zip Zip Data Stream
* @param folder Parent Location
*/
public static void unzip(InputStream zip, File folder) {
byte[] buffer = new byte[4096];
try{
ZipInputStream zis = new ZipInputStream(zip);
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null) {
File newFile = new File(folder + File.separator + ze.getName().replace('/', File.separatorChar));
if (newFile.exists()) {
if (newFile.isDirectory()) {
delete(newFile);
} else {
newFile.delete();
}
}
if (ze.isDirectory()) {
newFile.mkdirs();
continue;
} else if (!newFile.getParentFile().exists()) {
newFile.getParentFile().mkdirs();
}
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.close();
}
zis.closeEntry();
zis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* Delete Directory
*
* @param folder Location
*/
public static void delete(File folder) {
File[] files = folder.listFiles();
if(files!=null) {
for(File f : files) {
if(f.isDirectory() && !Files.isSymbolicLink(f.toPath())) {
delete(f);
} else try {
Files.delete(f.toPath());
} catch (IOException e) {
e.printStackTrace();
}
}
}
folder.delete();
}
}