-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathGXCompressor.java
More file actions
807 lines (775 loc) · 28.3 KB
/
GXCompressor.java
File metadata and controls
807 lines (775 loc) · 28.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
package com.genexus.compression;
import com.genexus.CommonUtil;
import com.genexus.GXBaseCollection;
import com.genexus.SdtMessages_Message;
import com.genexus.StructSdtMessages_Message;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.apache.logging.log4j.Logger;
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import java.io.*;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.jar.JarOutputStream;
import java.util.zip.*;
import static org.apache.commons.io.FilenameUtils.getExtension;
public class GXCompressor {
private static final Logger log = org.apache.logging.log4j.LogManager.getLogger(GXCompressor.class);
private static final int BUFFER_SIZE = 8192;
private static final String GENERIC_ERROR = "An error occurred during the compression/decompression process: ";
private static final String NO_FILES_ADDED = "No files have been added for compression.";
private static final String FILE_NOT_EXISTS = "File does not exist: ";
private static final String UNSUPPORTED_FORMAT = " is an unsupported format. Supported formats are zip, 7z, tar, gz and jar.";
private static final String EMPTY_FILE = "The selected file is empty: ";
private static final String PURGED_ARCHIVE = "After performing security checks, no valid files where left to compress";
private static final String DIRECTORY_ATTACK = "Potential directory traversal attack detected: ";
private static final String MAX_FILESIZE_EXCEEDED = "The file(s) selected for (de)compression exceed the maximum permitted file size of ";
private static final String TOO_MANY_FILES = "Too many files have been added for (de)compression. Maximum allowed is ";
private static final String ZIP_SLIP_DETECTED = "Zip slip or path traversal attack detected in archive: ";
private static final String BIG_SINGLE_FILE = "Individual file exceeds maximum allowed size: ";
private static final String PROCESSING_ERROR = "Error checking archive safety for file: ";
private static final String ARCHIVE_SIZE_ESTIMATION_ERROR = "";
private static void storageMessages(String error, GXBaseCollection<SdtMessages_Message> messages) {
try {
StructSdtMessages_Message struct = new StructSdtMessages_Message();
struct.setDescription(error);
struct.setType((byte) 1);
SdtMessages_Message msg = new SdtMessages_Message(struct);
messages.add(msg);
} catch (Exception e) {
log.error("Failed to store the following error message: {}", error, e);
}
}
/**
* Compresses specified files into an archive at the given path based on configuration parameters.
*
* @param files List of file paths to compress
* @param path Target path for the compressed archive
* @param configuration Configuration parameters for compression
* @param messages Collection to store output messages
* @return Boolean indicating success or failure of compression operation
*/
public static Boolean compress(ArrayList<String> files, String path, CompressionConfiguration configuration, GXBaseCollection<SdtMessages_Message>[] messages) {
if (files.isEmpty()) {
log.error(NO_FILES_ADDED);
storageMessages(NO_FILES_ADDED, messages[0]);
return false;
}
List<File> validFiles = new ArrayList<>();
long totalSize = 0;
for (String filePath : files) {
File file = new File(filePath);
try {
String normalizedPath = file.getCanonicalPath();
if (!file.exists()) {
log.error("{}{}", FILE_NOT_EXISTS, filePath);
storageMessages(FILE_NOT_EXISTS + filePath, messages[0]);
continue;
}
File absFile = file.getAbsoluteFile();
if (!normalizedPath.startsWith(absFile.getParentFile().getCanonicalPath())) {
log.error(DIRECTORY_ATTACK + "{}", filePath);
storageMessages(DIRECTORY_ATTACK + filePath, messages[0]);
return false;
}
long fileSize = file.length();
if (configuration.maxIndividualFileSize > -1 && fileSize > configuration.maxIndividualFileSize) {
log.error(BIG_SINGLE_FILE + filePath);
storageMessages(BIG_SINGLE_FILE + filePath, messages[0]);
continue;
}
totalSize += fileSize;
validFiles.add(file);
} catch (IOException e) {
log.error("Error normalizing path for file: {}", filePath, e);
storageMessages("Error normalizing path for file: " + filePath, messages[0]);
return false;
}
}
if (validFiles.isEmpty()) {
log.error(PURGED_ARCHIVE);
storageMessages(PURGED_ARCHIVE, messages[0]);
return false;
}
if (configuration.maxCombinedFileSize > -1 && totalSize > configuration.maxCombinedFileSize) {
log.error(MAX_FILESIZE_EXCEEDED + configuration.maxCombinedFileSize);
storageMessages(MAX_FILESIZE_EXCEEDED + configuration.maxCombinedFileSize, messages[0]);
return false;
}
if (configuration.maxFileCount > -1 && validFiles.size() > configuration.maxFileCount) {
log.error(TOO_MANY_FILES + configuration.maxFileCount);
storageMessages(TOO_MANY_FILES + configuration.maxFileCount, messages[0]);
return false;
}
try {
File targetFile = new File(path);
File targetDir = targetFile.getParentFile();
if (path.contains("/../") || path.contains("../") || path.contains("/..")) {
log.error(DIRECTORY_ATTACK + path);
storageMessages(DIRECTORY_ATTACK + path, messages[0]);
return false;
}
if (configuration.targetDirectory != null && !configuration.targetDirectory.isEmpty()) {
File configTargetDir = new File(configuration.targetDirectory);
String normalizedTargetPath = targetDir.getCanonicalPath();
String normalizedConfigPath = configTargetDir.getCanonicalPath();
if (!normalizedTargetPath.startsWith(normalizedConfigPath)) {
log.error(DIRECTORY_ATTACK + path);
storageMessages(DIRECTORY_ATTACK + path, messages[0]);
return false;
}
}
} catch (IOException e) {
log.error("Error validating target path: {}", path, e);
storageMessages("Error validating target path: " + path, messages[0]);
return false;
}
File[] toCompress = validFiles.toArray(new File[0]);
String format = CommonUtil.getFileType(path).toLowerCase();
try {
switch (format.toLowerCase()) {
case "zip":
compressToZip(toCompress, path);
break;
case "7z":
compressToSevenZ(toCompress, path);
break;
case "tar":
compressToTar(toCompress, path);
break;
case "gz":
compressToGzip(toCompress, path);
break;
case "jar":
compressToJar(toCompress, path);
break;
default:
log.error(format + UNSUPPORTED_FORMAT);
storageMessages(format + UNSUPPORTED_FORMAT, messages[0]);
return false;
}
return true;
} catch (Exception e) {
log.error(GENERIC_ERROR, e);
storageMessages(e.getMessage(), messages[0]);
return false;
}
}
/**
* Compresses files interactively, add files to a collection until the GXCompressor.compress method is executed
*
* @param path Target path for the compressed archive
* @param configuration Configuration parameters for decompression
* @param messages Collection to store output messages
* @return Boolean indicating success or failure of decompression operation
*/
public static Compression newCompression(String path, CompressionConfiguration configuration, GXBaseCollection<SdtMessages_Message>[] messages) {
return new Compression(path, configuration, messages);
}
/**
* Decompresses an archive file to the specified path based on configuration parameters.
*
* @param file Path to the archive file to decompress
* @param path Target path for the decompressed files
* @param configuration Configuration parameters for decompression
* @param messages Collection to store output messages
* @return Boolean indicating success or failure of decompression operation
*/
public static Boolean decompress(String file, String path, CompressionConfiguration configuration, GXBaseCollection<SdtMessages_Message>[] messages) {
File archiveFile = new File(file);
if (!archiveFile.exists()) {
log.error("{}{}", FILE_NOT_EXISTS, archiveFile.getAbsolutePath());
storageMessages(FILE_NOT_EXISTS + archiveFile.getAbsolutePath(), messages[0]);
return false;
}
if (archiveFile.length() == 0L) {
log.error("{}{}", EMPTY_FILE, file);
storageMessages(EMPTY_FILE + file, messages[0]);
return false;
}
int fileCount;
try {
fileCount = CompressionUtils.countArchiveEntries(archiveFile);
if (fileCount <= 0) {
log.error("{}{}", EMPTY_FILE, file);
storageMessages(EMPTY_FILE + file, messages[0]);
return false;
}
} catch (Exception e) {
log.error(PROCESSING_ERROR + file, e);
storageMessages(PROCESSING_ERROR + file, messages[0]);
return false;
}
try {
File targetDir = new File(path);
if (path.contains("/../") || path.contains("../") || path.contains("/..")) {
log.error(DIRECTORY_ATTACK + path);
storageMessages(DIRECTORY_ATTACK + path, messages[0]);
return false;
}
if (configuration.targetDirectory != null && !configuration.targetDirectory.isEmpty()) {
File configTargetDir = new File(configuration.targetDirectory);
String normalizedTargetPath = targetDir.getCanonicalPath();
String normalizedConfigPath = configTargetDir.getCanonicalPath();
if (!normalizedTargetPath.startsWith(normalizedConfigPath)) {
log.error(DIRECTORY_ATTACK + path);
storageMessages(DIRECTORY_ATTACK + path, messages[0]);
return false;
}
}
} catch (IOException e) {
log.error("Error validating target path: {}", path, e);
storageMessages("Error validating target path: " + path, messages[0]);
return false;
}
try {
if (!CompressionUtils.isArchiveSafe(archiveFile, path)) {
log.error(ZIP_SLIP_DETECTED + file);
storageMessages(ZIP_SLIP_DETECTED + file, messages[0]);
return false;
}
} catch (Exception e) {
log.error(PROCESSING_ERROR + file, e);
storageMessages(PROCESSING_ERROR + file, messages[0]);
return false;
}
try {
if (configuration.maxIndividualFileSize > -1) {
long maxFileSize = CompressionUtils.getMaxFileSize(archiveFile);
if (maxFileSize > configuration.maxIndividualFileSize) {
log.error(BIG_SINGLE_FILE + maxFileSize + " bytes");
storageMessages(BIG_SINGLE_FILE + maxFileSize + " bytes", messages[0]);
return false;
}
}
if (configuration.maxCombinedFileSize > -1) {
long totalSizeEstimate = CompressionUtils.estimateDecompressedSize(archiveFile);
if (totalSizeEstimate > configuration.maxCombinedFileSize) {
log.error(MAX_FILESIZE_EXCEEDED + configuration.maxCombinedFileSize);
storageMessages(MAX_FILESIZE_EXCEEDED + configuration.maxCombinedFileSize, messages[0]);
return false;
}
}
} catch (Exception e) {
log.error(ARCHIVE_SIZE_ESTIMATION_ERROR + "{}", file, e);
storageMessages("Error estimating archive size: " + file, messages[0]);
return false;
}
if (configuration.maxFileCount > -1 && fileCount > configuration.maxFileCount) {
log.error(TOO_MANY_FILES + configuration.maxFileCount);
storageMessages(TOO_MANY_FILES + configuration.maxFileCount, messages[0]);
return false;
}
String extension = getExtension(archiveFile.getName());
try {
switch (extension.toLowerCase()) {
case "zip":
decompressZip(archiveFile, path);
break;
case "7z":
decompress7z(archiveFile, path);
break;
case "tar":
decompressTar(archiveFile, path);
break;
case "gz":
decompressGzip(archiveFile, path);
break;
case "jar":
decompressJar(archiveFile, path);
break;
default:
log.error(extension + UNSUPPORTED_FORMAT);
storageMessages(extension + UNSUPPORTED_FORMAT, messages[0]);
return false;
}
return true;
} catch (Exception e) {
log.error(GENERIC_ERROR, e);
storageMessages(e.getMessage(), messages[0]);
return false;
}
}
private static void compressToZip(File[] files, String outputPath) throws IOException {
try (FileOutputStream fos = new FileOutputStream(outputPath);
ZipOutputStream zipOut = new ZipOutputStream(fos)) {
for (File fileToZip : files) {
zipFile(fileToZip, fileToZip.getName(), zipOut);
}
}
}
private static void zipFile(File fileToZip, String fileName, ZipOutputStream zipOut) throws IOException {
if (fileToZip.isHidden()) {
return;
}
if (fileToZip.isDirectory()) {
if (!fileName.endsWith("/")) {
fileName += "/";
}
zipOut.putNextEntry(new ZipEntry(fileName));
zipOut.closeEntry();
File[] children = fileToZip.listFiles();
if (children != null) {
for (File childFile : children) {
zipFile(childFile, fileName + childFile.getName(), zipOut);
}
}
} else {
try (FileInputStream fis = new FileInputStream(fileToZip)) {
zipOut.putNextEntry(new ZipEntry(fileName));
byte[] bytes = new byte[BUFFER_SIZE];
int length;
while ((length = fis.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
}
}
}
}
private static void compressToSevenZ(File[] files, String outputPath) throws IOException {
if (files == null || outputPath == null) {
throw new IllegalArgumentException("Files and outputPath must not be null");
}
File outputFile = new File(outputPath);
if (outputFile.exists()) {
throw new IOException("Output file already exists");
}
try (SevenZOutputFile sevenZOutput = new SevenZOutputFile(outputFile)) {
for (File file : files) {
if (file == null || !file.exists()) {
continue;
}
addFileToSevenZ(sevenZOutput, file, file.getName());
}
}
}
private static void addFileToSevenZ(SevenZOutputFile sevenZOutput, File file, String entryName) throws IOException {
if (file.isDirectory()) {
File[] children = file.listFiles();
if (children != null) {
for (File child : children) {
addFileToSevenZ(sevenZOutput, child, entryName + "/" + child.getName());
}
}
} else {
SevenZArchiveEntry entry = sevenZOutput.createArchiveEntry(file, entryName);
sevenZOutput.putArchiveEntry(entry);
try (FileInputStream fis = new FileInputStream(file)) {
byte[] buffer = new byte[BUFFER_SIZE];
int len;
while ((len = fis.read(buffer)) > 0) {
sevenZOutput.write(buffer, 0, len);
}
}
sevenZOutput.closeArchiveEntry();
}
}
private static void compressToTar(File[] files, String outputPath) throws IOException {
if (outputPath == null || outputPath.isEmpty()) {
throw new IllegalArgumentException("The output path must not be null or empty");
}
File outputFile = new File(outputPath);
if (outputFile.exists()) {
throw new IOException("Output file already exists");
}
try (FileOutputStream fos = new FileOutputStream(outputFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
TarArchiveOutputStream tarOut = new TarArchiveOutputStream(bos)) {
for (File file : files) {
if (file == null || !file.exists()) {
continue;
}
addFileToTar(tarOut, file, file.getName());
}
}
}
private static void addFileToTar(TarArchiveOutputStream tarOut, File file, String entryName) throws IOException {
if (file.isDirectory()) {
File[] children = file.listFiles();
if (children != null) {
for (File child : children) {
addFileToTar(tarOut, child, entryName + "/" + child.getName());
}
}
} else {
TarArchiveEntry entry = new TarArchiveEntry(file, entryName);
entry.setSize(file.length());
tarOut.putArchiveEntry(entry);
try (FileInputStream fis = new FileInputStream(file)) {
byte[] buffer = new byte[BUFFER_SIZE];
int len;
while ((len = fis.read(buffer)) != -1) {
tarOut.write(buffer, 0, len);
}
}
tarOut.closeArchiveEntry();
}
}
private static void compressToGzip(File[] files, String outputPath) throws IOException {
if (files == null || files.length == 0) {
throw new IllegalArgumentException("No files to compress");
}
if (outputPath == null || outputPath.isEmpty()) {
throw new IllegalArgumentException("Output path is null or empty");
}
File outputFile = new File(outputPath);
if (outputFile.exists() && !outputFile.canWrite()) {
throw new IOException("Cannot write to output file");
}
File parentDir = outputFile.getParentFile();
if (parentDir != null && !parentDir.exists() && !parentDir.mkdirs()) {
throw new IOException("Failed to create output directory");
}
boolean singleFile = files.length == 1 && files[0].isFile();
File tempFile = File.createTempFile("compress_", ".tmp", parentDir);
if (singleFile) {
try (
FileInputStream fis = new FileInputStream(files[0]);
FileOutputStream fos = new FileOutputStream(tempFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
GzipCompressorOutputStream gcos = new GzipCompressorOutputStream(bos)
) {
byte[] buffer = new byte[BUFFER_SIZE];
int len;
while ((len = fis.read(buffer)) != -1) {
gcos.write(buffer, 0, len);
}
}
} else {
try (
FileOutputStream fos = new FileOutputStream(tempFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
GzipCompressorOutputStream gcos = new GzipCompressorOutputStream(bos);
TarArchiveOutputStream taos = new TarArchiveOutputStream(gcos)
) {
taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
Stack<File> fileStack = new Stack<>();
Stack<String> pathStack = new Stack<>();
for (File f : files) {
if (f != null) {
fileStack.push(f);
pathStack.push("");
}
}
while (!fileStack.isEmpty()) {
File currentFile = fileStack.pop();
String path = pathStack.pop();
String entryName = path + currentFile.getName();
if (currentFile.isDirectory()) {
File[] children = currentFile.listFiles();
if (children != null && children.length > 0) {
for (File child : children) {
fileStack.push(child);
pathStack.push(entryName + "/");
}
}
} else {
TarArchiveEntry entry = new TarArchiveEntry(currentFile, entryName);
taos.putArchiveEntry(entry);
try (FileInputStream fis = new FileInputStream(currentFile)) {
byte[] buffer = new byte[BUFFER_SIZE];
int len;
while ((len = fis.read(buffer)) != -1) {
taos.write(buffer, 0, len);
}
}
taos.closeArchiveEntry();
}
}
}
}
if (!tempFile.exists()) {
throw new IOException("Failed to create the archive");
}
String finalName = outputPath;
if (singleFile) {
if (!finalName.toLowerCase().endsWith(".gz")) {
finalName += ".gz";
}
} else {
if (finalName.toLowerCase().endsWith(".tar.gz")) {
// do nothing
} else if (finalName.toLowerCase().endsWith(".gz")) {
finalName = finalName.substring(0, finalName.length() - 3) + ".tar.gz";
} else {
finalName += ".tar.gz";
}
}
File finalFile = new File(finalName);
if (finalFile.exists() && !finalFile.delete()) {
throw new IOException("Failed to delete existing file with desired name");
}
if (!tempFile.renameTo(finalFile)) {
throw new IOException("Failed to rename archive to desired name");
}
}
private static void compressToJar(File[] files, String outputPath) throws IOException {
if (outputPath == null || outputPath.isEmpty()) {
throw new IllegalArgumentException("Output path is null or empty");
}
File outputFile = new File(outputPath);
if (outputFile.exists()) {
throw new IOException("Output file already exists");
}
try (JarOutputStream jos = new JarOutputStream(Files.newOutputStream(outputFile.toPath()))) {
byte[] buffer = new byte[BUFFER_SIZE];
for (File file : files) {
if (file == null || !file.exists()) {
continue;
}
String basePath = file.isDirectory() ? file.getCanonicalPath() : file.getParentFile().getCanonicalPath();
Stack<File> stack = new Stack<>();
stack.push(file);
while (!stack.isEmpty()) {
File currentFile = stack.pop();
String entryName = currentFile.getCanonicalPath().substring(basePath.length() + 1).replace("\\", "/");
if (currentFile.isDirectory()) {
File[] subFiles = currentFile.listFiles();
if (subFiles != null) {
for (File subFile : subFiles) {
stack.push(subFile);
}
}
if (!entryName.isEmpty()) {
if (!entryName.endsWith("/")) {
entryName += "/";
}
jos.putNextEntry(new JarEntry(entryName));
jos.closeEntry();
}
} else {
jos.putNextEntry(new JarEntry(entryName));
try {
try (FileInputStream fis = new FileInputStream(currentFile)) {
int len;
while ((len = fis.read(buffer)) > 0) {
jos.write(buffer, 0, len);
}
}
} finally {
jos.closeEntry();
}
}
}
}
}
}
private static void decompressZip(File archive, String directory) throws IOException {
byte[] buffer = new byte[BUFFER_SIZE];
try (ZipInputStream zis = new ZipInputStream(Files.newInputStream(archive.toPath()))) {
ZipEntry zipEntry;
while ((zipEntry = zis.getNextEntry()) != null) {
File newFile = new File(directory, zipEntry.getName());
if (HasZipSlipVulnerability(newFile, directory)) {
throw new IOException("Bad tar entry: " + zipEntry.getName());
}
if (zipEntry.isDirectory()) {
if (!newFile.isDirectory() && !newFile.mkdirs()) {
throw new IOException("Failed to create directory " + newFile);
}
} else {
File parent = newFile.getParentFile();
if (!parent.isDirectory() && !parent.mkdirs()) {
throw new IOException("Failed to create directory " + parent);
}
try (FileOutputStream fos = new FileOutputStream(newFile)) {
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
}
}
}
}
}
private static void decompress7z(File archive, String directory) throws IOException {
byte[] buffer = new byte[BUFFER_SIZE];
try (SevenZFile sevenZFile = new SevenZFile(archive)) {
SevenZArchiveEntry entry;
while ((entry = sevenZFile.getNextEntry()) != null) {
File newFile = new File(directory, entry.getName());
if (HasZipSlipVulnerability(newFile, directory)) {
throw new IOException("Bad tar entry: " + entry.getName());
}
if (entry.isDirectory()) {
if (!newFile.isDirectory() && !newFile.mkdirs()) {
throw new IOException("Failed to create directory " + newFile);
}
} else {
File parent = newFile.getParentFile();
if (!parent.isDirectory() && !parent.mkdirs()) {
throw new IOException("Failed to create directory " + parent);
}
try (OutputStream out = Files.newOutputStream(newFile.toPath())) {
int bytesRead;
while ((bytesRead = sevenZFile.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}
}
}
}
}
private static void decompressTar(File archive, String directory) throws IOException {
byte[] buffer = new byte[BUFFER_SIZE];
try (TarArchiveInputStream tis = new TarArchiveInputStream(Files.newInputStream(archive.toPath()))) {
TarArchiveEntry entry;
while ((entry = tis.getNextEntry()) != null) {
File newFile = new File(directory, entry.getName());
if (HasZipSlipVulnerability(newFile, directory)) {
throw new IOException("Bad tar entry: " + entry.getName());
}
if (entry.isDirectory()) {
if (!newFile.isDirectory() && !newFile.mkdirs()) {
throw new IOException("Failed to create directory " + newFile);
}
} else {
File parent = newFile.getParentFile();
if (!parent.isDirectory() && !parent.mkdirs()) {
throw new IOException("Failed to create directory " + parent);
}
try (OutputStream out = Files.newOutputStream(newFile.toPath())) {
int len;
while ((len = tis.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
}
}
}
}
}
private static void decompressGzip(File archive, String directory) throws IOException {
if (!archive.exists() || !archive.isFile()) {
throw new IllegalArgumentException("The archive file does not exist or is not a file.");
}
File targetDir = new File(directory);
if (!targetDir.exists()) {
if (!targetDir.mkdirs()) {
String error = "Failed to create target directory: " + directory;
log.error(error);
throw new IOException(error);
}
} else if (!targetDir.isDirectory()) {
throw new IllegalArgumentException("The specified path exists but is not a directory.");
}
File tempFile = File.createTempFile("decompressed_", ".tmp");
try (
FileInputStream fis = new FileInputStream(archive);
GZIPInputStream gzipInputStream = new GZIPInputStream(fis);
FileOutputStream fos = new FileOutputStream(tempFile)
) {
byte[] buffer = new byte[BUFFER_SIZE];
int len;
while ((len = gzipInputStream.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
}
boolean isTar = false;
try (FileInputStream tempFis = new FileInputStream(tempFile);
TarArchiveInputStream testTar = new TarArchiveInputStream(tempFis)) {
TarArchiveEntry testEntry = testTar.getNextEntry();
if (testEntry != null) {
isTar = true;
}
} catch (IOException ignored) {}
if (isTar) {
try (FileInputStream tarFis = new FileInputStream(tempFile);
TarArchiveInputStream tarInput = new TarArchiveInputStream(tarFis)) {
TarArchiveEntry entry;
while ((entry = tarInput.getNextEntry()) != null) {
File outFile = new File(targetDir, entry.getName());
if (entry.isDirectory()) {
if (!outFile.exists() && !outFile.mkdirs()) {
String error = "Failed to create directory: " + outFile;
log.error(error);
throw new IOException(error);
}
} else {
File parent = outFile.getParentFile();
if (!parent.exists() && !parent.mkdirs()) {
String error = "Failed to create parent directory: " + parent;
log.error(error);
throw new IOException(error);
}
try (FileOutputStream os = new FileOutputStream(outFile)) {
byte[] buffer = new byte[BUFFER_SIZE];
int count;
while ((count = tarInput.read(buffer)) != -1) {
os.write(buffer, 0, count);
}
}
}
}
}
} else {
String name = archive.getName();
if (name.toLowerCase().endsWith(".gz")) {
name = name.substring(0, name.length() - 3);
}
File singleOutFile = new File(targetDir, name);
if (!tempFile.renameTo(singleOutFile)) {
try (
FileInputStream in = new FileInputStream(tempFile);
FileOutputStream out = new FileOutputStream(singleOutFile)
) {
byte[] buffer = new byte[BUFFER_SIZE];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
}
}
}
if (!tempFile.delete()) {
tempFile.deleteOnExit();
}
}
private static void decompressJar(File archive, String directory) throws IOException {
if (!archive.exists() || !archive.isFile()) {
throw new IOException("Invalid archive file.");
}
File targetDir = new File(directory);
if (!targetDir.exists()) {
if (!targetDir.mkdirs()) {
throw new IOException("Failed to create target directory.");
}
}
try (JarInputStream jarInputStream = new JarInputStream(Files.newInputStream(archive.toPath()))) {
JarEntry entry;
while ((entry = jarInputStream.getNextJarEntry()) != null) {
File outputFile = new File(targetDir, entry.getName());
if (entry.isDirectory()) {
if (!outputFile.exists() && !outputFile.mkdirs()) {
throw new IOException("Failed to create directory: " + outputFile.getAbsolutePath());
}
} else {
File parent = outputFile.getParentFile();
if (!parent.exists() && !parent.mkdirs()) {
throw new IOException("Failed to create parent directory: " + parent.getAbsolutePath());
}
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead;
while ((bytesRead = jarInputStream.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
}
}
}
}
}
// Check for Zip Slip vulnerability: ensure extracted file remains within target directory
// Use Path.normalize() and Path.startsWith()
private static boolean HasZipSlipVulnerability(File file, String directory) {
java.nio.file.Path destDirPath = new File(directory).toPath().toAbsolutePath().normalize();
java.nio.file.Path newFilePath = file.toPath().toAbsolutePath().normalize();
return !newFilePath.startsWith(destDirPath);
}
}