-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFileUtils.java
More file actions
253 lines (205 loc) · 7.9 KB
/
FileUtils.java
File metadata and controls
253 lines (205 loc) · 7.9 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package io.github.fabriccompatibiltylayers.modremappingapi.impl.utils;
import org.jetbrains.annotations.ApiStatus;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
@ApiStatus.Internal
public class FileUtils {
@ApiStatus.Internal
public static boolean exist(Collection<Path> paths) {
for (Path path : paths) {
if (!Files.exists(path)) return false;
}
return true;
}
@ApiStatus.Internal
public static void delete(Collection<Path> paths) {
for (Path path : paths) {
try {
Files.deleteIfExists(path);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@ApiStatus.Internal
public static void downloadFile(String url, Path target) throws IOException {
try (BufferedInputStream inputStream = new BufferedInputStream(new URL(url).openStream())) {
try (BufferedOutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(target))) {
byte[] buffer = new byte[2048];
// Increments file size
int length;
int downloaded = 0;
// Looping until server finishes
while ((length = inputStream.read(buffer)) != -1) {
// Writing data
outputStream.write(buffer, 0, length);
downloaded += length;
// Constants.MAIN_LOGGER.debug("Download Status: " + (downloaded * 100) / (contentLength * 1.0) + "%");
}
outputStream.close();
inputStream.close();
}
}
}
@ApiStatus.Internal
public static void copyZipFile(Path original, Path target) throws IOException {
target.toFile().delete();
ZipInputStream zin = new ZipInputStream(Files.newInputStream(original));
ZipOutputStream zout = new ZipOutputStream(Files.newOutputStream(target));
ZipEntry entry = zin.getNextEntry();
byte[] buf = new byte[1024];
while (entry != null) {
String zipEntryName = entry.getName();
zout.putNextEntry(new ZipEntry(zipEntryName));
// Transfer bytes from the ZIP file to the output file
int len;
while ((len = zin.read(buf)) > 0) {
zout.write(buf, 0, len);
}
entry = zin.getNextEntry();
}
// Close the streams
zin.close();
// Compress the files
// Complete the ZIP file
zout.close();
}
@ApiStatus.Internal
public static List<String> listZipContent(Path path) throws IOException {
List<String> files = new ArrayList<>();
try (ZipInputStream zin = new ZipInputStream(Files.newInputStream(path))) {
while (true) {
ZipEntry entry = zin.getNextEntry();
if (entry == null) {
break;
}
String name = entry.getName();
if (!entry.isDirectory()) {
files.add(name.replace("\\", "/"));
}
}
}
return files;
}
@ApiStatus.Internal
public static List<String> listDirectoryContent(File[] files) {
List<String> list = new ArrayList<>();
for (File file : files) {
if (file.isDirectory()) {
String name = file.getName();
for (String fileName : listDirectoryContent(file.listFiles())) {
list.add(name + "/" + fileName);
}
} else if (file.isFile()) {
list.add(file.getName());
}
}
return list;
}
@ApiStatus.Internal
public static List<String> listPathContent(Path path) throws IOException {
File file = path.toFile();
if (file.isDirectory()) {
return listDirectoryContent(file.listFiles());
} else if (file.isFile()) {
return listZipContent(path);
}
return new ArrayList<>();
}
@ApiStatus.Internal
public static void emptyDir(Path dir) {
try {
Files.walkFileTree(dir, new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@ApiStatus.Internal
public static void zipFolder(Path input, Path output) throws IOException {
try (ZipOutputStream zout = new ZipOutputStream(Files.newOutputStream(output))) {
File fileToZip = input.toFile();
zipFile(fileToZip, fileToZip.getName(), zout, true);
}
}
private static void zipFile(File fileToZip, String fileName, ZipOutputStream zipOut, boolean root) throws IOException {
if (fileToZip.isHidden()) {
return;
}
if (fileToZip.isDirectory()) {
if (!root) {
if (fileName.endsWith("/")) {
zipOut.putNextEntry(new ZipEntry(fileName));
zipOut.closeEntry();
} else {
zipOut.putNextEntry(new ZipEntry(fileName + "/"));
zipOut.closeEntry();
}
}
File[] children = fileToZip.listFiles();
for (File childFile : children) {
zipFile(childFile, (root ? "" : fileName + "/") + childFile.getName(), zipOut, false);
}
return;
}
ZipEntry zipEntry = new ZipEntry(fileName);
zipOut.putNextEntry(zipEntry);
try (FileInputStream fis = new FileInputStream(fileToZip)) {
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
}
}
zipOut.closeEntry();
}
/* Define ZIP File System Properies in HashMap */
private static final Map<String, String> ZIP_PROPERTIES = new HashMap<>();
static {
/* We want to read an existing ZIP File, so we set this to False */
ZIP_PROPERTIES.put("create", "false");
/* Specify the encoding as UTF-8 */
ZIP_PROPERTIES.put("encoding", "UTF-8");
}
@ApiStatus.Internal
public static FileSystem getJarFileSystem(Path path) throws URISyntaxException, IOException {
return FileSystems.newFileSystem(new URI("jar:" + path.toUri()), ZIP_PROPERTIES);
}
@ApiStatus.Internal
public static void removeEntriesFromZip(Path zipPath, List<String> entries) throws IOException, URISyntaxException {
try (FileSystem zipfs = getJarFileSystem(zipPath)) {
for (String entryName : entries) {
Path entryPath = zipfs.getPath(entryName);
if (Files.exists(entryPath)) {
Files.delete(entryPath);
}
}
}
}
}