|
| 1 | +package com.genexus.compression; |
| 2 | + |
| 3 | +import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry; |
| 4 | +import org.apache.commons.compress.archivers.sevenz.SevenZFile; |
| 5 | +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; |
| 6 | +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; |
| 7 | + |
| 8 | +import java.io.File; |
| 9 | +import java.io.FileInputStream; |
| 10 | +import java.io.IOException; |
| 11 | +import java.util.Enumeration; |
| 12 | +import java.util.zip.ZipEntry; |
| 13 | +import java.util.zip.ZipFile; |
| 14 | + |
| 15 | +import static org.apache.commons.io.FileUtils.getFile; |
| 16 | +import static org.apache.commons.io.FilenameUtils.getExtension; |
| 17 | + |
| 18 | +public class CompressionUtils { |
| 19 | + public static int countArchiveEntries(File toCompress) throws IOException { |
| 20 | + String ext = getExtension(toCompress.getName()).toLowerCase(); |
| 21 | + switch (ext) { |
| 22 | + case "zip": |
| 23 | + case "jar": { |
| 24 | + int count = 0; |
| 25 | + try (ZipFile zip = new ZipFile(toCompress)) { |
| 26 | + Enumeration<? extends ZipEntry> entries = zip.entries(); |
| 27 | + while (entries.hasMoreElements()) { |
| 28 | + entries.nextElement(); |
| 29 | + count++; |
| 30 | + } |
| 31 | + } |
| 32 | + return count; |
| 33 | + } |
| 34 | + case "tar": { |
| 35 | + int count = 0; |
| 36 | + try (FileInputStream fis = new FileInputStream(toCompress); |
| 37 | + TarArchiveInputStream tais = new TarArchiveInputStream(fis)) { |
| 38 | + while (tais.getNextEntry() != null) { |
| 39 | + count++; |
| 40 | + } |
| 41 | + } |
| 42 | + return count; |
| 43 | + } |
| 44 | + case "7z": { |
| 45 | + int count = 0; |
| 46 | + try (SevenZFile sevenZFile = getSevenZFile(toCompress.getAbsolutePath())) { |
| 47 | + while (sevenZFile.getNextEntry() != null) { |
| 48 | + count++; |
| 49 | + } |
| 50 | + } |
| 51 | + return count; |
| 52 | + } |
| 53 | + case "gz": |
| 54 | + return 1; |
| 55 | + } |
| 56 | + return 0; |
| 57 | + } |
| 58 | + public static boolean isArchiveSafe(File toCompress, String targetDir) throws IOException { |
| 59 | + String ext = getExtension(toCompress.getName()).toLowerCase(); |
| 60 | + File target = new File(targetDir).getCanonicalFile(); |
| 61 | + switch (ext) { |
| 62 | + case "zip": |
| 63 | + case "jar": |
| 64 | + try (ZipFile zip = new ZipFile(toCompress)) { |
| 65 | + Enumeration<? extends ZipEntry> entries = zip.entries(); |
| 66 | + while (entries.hasMoreElements()) { |
| 67 | + ZipEntry entry = entries.nextElement(); |
| 68 | + File entryFile = new File(target, entry.getName()); |
| 69 | + if (!entryFile.getCanonicalPath().startsWith(target.getCanonicalPath() + File.separator)) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + return true; |
| 75 | + case "tar": |
| 76 | + try (FileInputStream fis = new FileInputStream(toCompress); |
| 77 | + TarArchiveInputStream tais = new TarArchiveInputStream(fis)) { |
| 78 | + TarArchiveEntry entry; |
| 79 | + while ((entry = tais.getNextEntry()) != null) { |
| 80 | + File entryFile = new File(target, entry.getName()); |
| 81 | + if (!entryFile.getCanonicalPath().startsWith(target.getCanonicalPath() + File.separator)) { |
| 82 | + return false; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + return true; |
| 87 | + case "7z": |
| 88 | + try (SevenZFile sevenZFile = getSevenZFile(toCompress.getAbsolutePath())) { |
| 89 | + SevenZArchiveEntry entry; |
| 90 | + while ((entry = sevenZFile.getNextEntry()) != null) { |
| 91 | + File entryFile = new File(target, entry.getName()); |
| 92 | + if (!entryFile.getCanonicalPath().startsWith(target.getCanonicalPath() + File.separator)) { |
| 93 | + return false; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + return true; |
| 98 | + case "gz": |
| 99 | + return true; |
| 100 | + } |
| 101 | + return true; |
| 102 | + } |
| 103 | + |
| 104 | + private static SevenZFile getSevenZFile(final String specialPath) throws IOException { |
| 105 | + return SevenZFile.builder().setFile(getFile(specialPath)).get(); |
| 106 | + } |
| 107 | +} |
0 commit comments