Repository navigation
Expand file tree
/
Copy pathFileHelper.java
More file actions
407 lines (364 loc) · 14.3 KB
/
Copy pathFileHelper.java
File metadata and controls
407 lines (364 loc) · 14.3 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
* Copyright 2020-2025 IEXEC BLOCKCHAIN TECH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.iexec.common.utils;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.web3j.crypto.Hash;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Objects;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class FileHelper {
public static String readFile(String filePath) {
try {
byte[] content = Files.readAllBytes(Paths.get(filePath));
return new String(content);
} catch (Exception e) {
log.error("Failed to read file [filePath:{}]", filePath);
}
return "";
}
public static byte[] readFileBytes(String filePath) {
String content = readFile(filePath);
if (!content.isEmpty()) {
return content.getBytes();
}
return null;
}
/*
* TODO
* 1 - Rename FileHelper.readFile --> FileHelper.readStringFile
* 2 - Remove FileHelper.readFileBytes ?
* */
public static byte[] readAllBytes(String filePath) {
try {
return Files.readAllBytes(Paths.get(filePath));
} catch (IOException e) {
log.error("Failed to readAllBytes [filePath:{}]", filePath);
}
return null;
}
public static boolean writeFile(String filePath, byte[] data) {
try {
Files.write(Paths.get(filePath), data);
return true;
} catch (IOException e) {
log.error("Failed to write file [filePath:{}, dataHash:{}]", filePath, Hash.sha3(data));
}
return false;
}
public static File createFileWithContent(String filePath, String data) {
return data != null ? createFileWithContent(filePath, data.getBytes()) : null;
}
public static File createFileWithContent(String filePath, byte[] data) {
String parentDirectoryPath = new File(filePath).getParent();
boolean isParentFolderCreated = createFolder(parentDirectoryPath);
if (!isParentFolderCreated) {
log.error("Failed to create base directory [parentDirectoryPath:{}]", parentDirectoryPath);
return null;
}
try {
Files.write(Paths.get(filePath), data);
log.debug("File created [filePath:{}]", filePath);
return new File(filePath);
} catch (IOException e) {
log.error("Failed to create file [filePath:{}]", filePath);
return null;
}
}
/**
* Download file with custom name in specified directory
*
* @param fileUrl URL of the file
* @param parentFolderPath directory path where the file will be downloaded
* @param outputFilename desired name for future downloaded file
* @return downloaded file location path if successful download
*/
public static String downloadFile(String fileUrl,
String parentFolderPath,
String outputFilename) {
if (StringUtils.isEmpty(fileUrl)) {
log.error("Invalid file url [fileUrl:{}]", fileUrl);
return "";
}
if (StringUtils.isEmpty(parentFolderPath)) {
log.error("Invalid parent folder path [fileUrl:{}, path:{}]",
fileUrl, parentFolderPath);
return "";
}
if (StringUtils.isEmpty(outputFilename)) {
log.error("Invalid output filename [fileUrl:{}, outputFilename:{}]",
fileUrl, outputFilename);
return "";
}
byte[] fileBytes = readFileBytesFromUrl(fileUrl);
if (fileBytes.length == 0) {
log.error("Failed to download file [fileUrl:{}]", fileUrl);
return "";
}
boolean parentFolderAlreadyExisted = exists(parentFolderPath);
if (!parentFolderAlreadyExisted && !createFolder(parentFolderPath)) {
log.error("Failed to create parent folder [fileUrl:{}, parentFolderPath:{}]",
fileUrl, parentFolderPath);
return "";
}
String filePath = parentFolderPath + File.separator + outputFilename;
boolean isWritten = writeFile(filePath, fileBytes);
if (!isWritten) {
log.error("Failed to write downloaded file to disk " +
"[fileUrl:{}, filePath:{}]", fileUrl, filePath);
if (!parentFolderAlreadyExisted) {
deleteFolder(parentFolderPath);
}
return "";
}
log.info("Downloaded data [fileUrl:{}, filePath:{}]", fileUrl, filePath);
return filePath;
}
/**
* Read the content of the remote file located at the provided URL.
*
* @param uri URI of the file
* @return the content of the file in a byte array if success,
* an empty byte array otherwise.
*/
public static byte[] readFileBytesFromUrl(final String uri) {
try (final InputStream inputStream = new URI(uri).toURL().openStream()) {
return inputStream.readAllBytes();
} catch (Exception e) {
log.error("Failed to read file bytes from URI [uri:{}]", uri, e);
return new byte[0];
}
}
/**
* Download file and returns downloaded file location path if successful.
* Downloaded file name is inferred from uri end path
*
* @param fileUri URI of the file
* @param downloadDirectoryPath directory path where the file will be downloaded
* @return downloaded file location path if successful download
*/
public static String downloadFile(String fileUri, String downloadDirectoryPath) {
return downloadFile(fileUri,
downloadDirectoryPath,
Paths.get(fileUri).getFileName().toString());
}
public static boolean exists(String path) {
Objects.requireNonNull(path, "Path must not be null");
return new File(path).exists();
}
public static boolean createFolder(String folderPath) {
File baseDirectory = new File(folderPath);
return baseDirectory.exists() ? true : baseDirectory.mkdirs();
}
public static boolean deleteFile(String filePath) {
try {
Files.delete(Paths.get(filePath));
log.info("File has been deleted [path:{}]", filePath);
return true;
} catch (IOException e) {
log.error("Problem when trying to delete the file [path:{}]", filePath);
}
return false;
}
/**
* Delete a folder recursively with its content.
*
* @param folderPath The folder to delete
* @return Whether the folder still exists after the operation or not
*/
public static boolean deleteFolder(final String folderPath) {
final File folder = new File(folderPath);
if (!folder.exists()) {
log.info("Folder does not exist, nothing to delete [path:{}]", folderPath);
return true;
}
try {
FileUtils.deleteDirectory(folder);
log.info("Folder has been deleted [path:{}]", folderPath);
} catch (IOException e) {
log.error("Problem when trying to delete the folder [path:{}]", folderPath, e);
}
return !folder.exists();
}
public static File zipFolder(String folderPath) {
String parentFolder = Paths.get(folderPath).getParent().toString();
return zipFolder(folderPath, parentFolder);
}
public static File zipFolder(String folderPath, String saveIn) {
String folderName = Paths.get(folderPath).getFileName().toString();
String zipFilePath = Path.of(saveIn, folderName + ".zip").toAbsolutePath().toString();
Path sourceFolderPath = Paths.get(folderPath);
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFilePath))) {
Files.walkFileTree(sourceFolderPath, new SimpleFileVisitor<>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
log.debug("Adding file to zip [file:{}, zip:{}]", file.toAbsolutePath(), zipFilePath);
zos.putNextEntry(new ZipEntry(sourceFolderPath.relativize(file).toString()));
if (!Files.isSymbolicLink(file)) {
Files.copy(file, zos);
}
zos.closeEntry();
return FileVisitResult.CONTINUE;
}
});
log.info("Folder zipped [path:{}]", zipFilePath);
return new File(zipFilePath);
} catch (Exception e) {
log.error("Failed to zip folder [path:{}]", zipFilePath, e);
}
return null;
}
// TODO: Use same lib for zipping
public static boolean unZipFile(String zipFilePath, String destDirPath) {
if (zipFilePath == null || zipFilePath.isEmpty() || !new File(zipFilePath).exists()) {
log.error("Failed to unZipFile (missing zipFile) [zipFilePath:{}, destDirPath:{}]", zipFilePath, destDirPath);
return false;
}
if (destDirPath == null || destDirPath.isEmpty()) {
log.error("Failed to unZipFile (missing destDirPath) [zipFilePath:{}, destDirPath:{}]", zipFilePath, destDirPath);
return false;
}
try {
new ZipFile(zipFilePath).extractAll(destDirPath);
return true;
} catch (ZipException e) {
log.error("Failed to unZipFile (can't extract) [zipFilePath:{}, destDirPath:{}]", zipFilePath, destDirPath);
}
return false;
}
/*
* Will extract into a directory next to zip
*
* before
* └── some-content.zip
*
* after
* ├── some-content.zip
* └── some-content/
* ├── file1
* └── file2
*
* returns extractDirPath
* */
public static String unZipFile(String zipPath){
String extractDirPath = removeZipExtension(zipPath);
if (extractDirPath.isEmpty()){
log.error("unzip failed (removeZipExtension) [zipPath:{}]", zipPath);
return "";
}
boolean isExtracted = FileHelper.unZipFile(zipPath, extractDirPath);
if (!isExtracted){
log.error("unzip failed (unZipFile) [zipPath:{}]", zipPath);
return "";
}
return extractDirPath;
}
public static String removeZipExtension(String zipPath) {
if (zipPath == null || !FilenameUtils.getExtension(zipPath).equals("zip")){
return "";
}
return FilenameUtils.removeExtension(zipPath);
}
public static boolean replaceFile(String toBeReplaced, String replacer) {
try {
Files.delete(Paths.get(toBeReplaced));
return new File(replacer).renameTo(new File(toBeReplaced));
} catch (IOException e) {
log.error("Problem when trying to replace file [toBeReplaced:{}, replacer:{}]",
toBeReplaced, replacer, e);
return false;
}
}
public static boolean moveFile(String fromPath, String toPath) {
boolean isMoved = new File(fromPath).renameTo(new File(toPath));
if (!isMoved) {
log.error("Failed to moveFile file [fromPath:{}, toPath:{}]", fromPath, toPath);
}
return isMoved;
}
public static boolean copyFolder(String source, String target) {
try {
FileUtils.copyDirectory(new File(source), new File(target));
return true;
} catch (IOException e) {
log.error("Error copying folder [source:{}, target:{}]", source, target, e);
return false;
}
}
public static boolean copyFile(String source, String target) {
try {
Files.copy(Path.of(source), Path.of(target));
return true;
} catch (IOException e) {
log.error("Error copying file [source:{}, target:{}]", source, target, e);
return false;
}
}
public static String getFilenameFromUri(String uri) {
return Paths.get(uri).getFileName().toString();
}
public static String printDirectoryTree(File folder) {
if (!folder.isDirectory()) {
throw new IllegalArgumentException("folder is not a Directory");
}
int indent = 0;
StringBuilder sb = new StringBuilder();
printDirectoryTree(folder, indent, sb);
return sb.toString();
}
private static void printDirectoryTree(File folder, int indent,
StringBuilder sb) {
if (!folder.isDirectory()) {
throw new IllegalArgumentException("folder is not a Directory");
}
sb.append(getIndentString(indent));
sb.append("├── ");
sb.append(folder.getName());
sb.append("/");
sb.append("\n");
for (File file : folder.listFiles()) {
if (file.isDirectory()) {
printDirectoryTree(file, indent + 1, sb);
} else {
printFile(file, indent + 1, sb);
}
}
}
private static void printFile(File file, int indent, StringBuilder sb) {
sb.append(getIndentString(indent));
sb.append("├── ");
sb.append(file.getName());
sb.append("\n");
}
private static String getIndentString(int indent) {
return "│ ".repeat(indent);
}
}