@Test public void testArchiveDuplicateEntry (@TempDir Path tempDir) throws IOException, ArchiveException, CompressorException { String someFilename = "some-file.txt"; Path someFilePath = tempDir.resolve(someFilename); Files.write(someFilePath, "Hello world\n".getBytes(StandardCharsets.UTF_8)); Path archiveZip = tempDir.resolve("archive.zip"); try (OutputStream fileOutputStream = new BufferedOutputStream(Files.newOutputStream(archiveZip)); ArchiveOutputStream archiveOutputStream = new ArchiveStreamFactory() .createArchiveOutputStream(ZIP.getCommonsCompressName(), fileOutputStream)) { ArchiveEntry entry = archiveOutputStream.createArchiveEntry(someFilePath.toFile(), someFilename); archiveOutputStream.putArchiveEntry(entry); Files.copy(someFilePath, archiveOutputStream); archiveOutputStream.closeArchiveEntry(); Files.write(someFilePath, "Good bye\n".getBytes(StandardCharsets.UTF_8)); entry = archiveOutputStream.createArchiveEntry(someFilePath.toFile(), someFilename); archiveOutputStream.putArchiveEntry(entry); Files.copy(someFilePath, archiveOutputStream); archiveOutputStream.closeArchiveEntry(); archiveOutputStream.finish(); } Path extractionDirectory = tempDir.resolve("extract"); Files.createDirectories(extractionDirectory); ArchiveUtils.extractArchive(archiveZip, extractionDirectory); assertEquals(new String(Files.readAllBytes(tempDir.resolve(someFilename)), StandardCharsets.UTF_8), new String(Files.readAllBytes(extractionDirectory.resolve(someFilename)), StandardCharsets.UTF_8), "Extracted file contents should have matched original"); }
public static void extractArchive (Path archivePath, Path extractToDirectory) throws IOException, CompressorException, ArchiveException { try (InputStream jdkInputStream = new BufferedInputStream(Files.newInputStream(archivePath))) { String compressorType = null; try { compressorType = CompressorStreamFactory.detect(jdkInputStream); } catch (CompressorException exception) { LOG.debug("Didn't detect any compression for archive " + archivePath + ": " + exception.getMessage()); } InputStream decompressedJdkInputStream = jdkInputStream; if (compressorType != null) { decompressedJdkInputStream = new BufferedInputStream( CompressorStreamFactory.getSingleton().createCompressorInputStream(compressorType, jdkInputStream)); } switch (ArchiveStreamFactory.detect(decompressedJdkInputStream)) { case ArchiveStreamFactory.ZIP: if (compressorType != null) { LOG.error("Cannot extract Zip archives that are wrapped in additional compression"); } else { extractZipArchive(archivePath, extractToDirectory); } break; case ArchiveStreamFactory.JAR: extractJarArchive(decompressedJdkInputStream, extractToDirectory); break; case ArchiveStreamFactory.TAR: extractTarArchive(decompressedJdkInputStream, extractToDirectory); break; default: LOG.error("No special handling for archive type " + archivePath + ". Permissions and links will not be properly handled."); extractGenericArchive(decompressedJdkInputStream, extractToDirectory); break; } } }
ArchiveUtils { public static void extractArchive (Path archivePath, Path extractToDirectory) throws IOException, CompressorException, ArchiveException { try (InputStream jdkInputStream = new BufferedInputStream(Files.newInputStream(archivePath))) { String compressorType = null; try { compressorType = CompressorStreamFactory.detect(jdkInputStream); } catch (CompressorException exception) { LOG.debug("Didn't detect any compression for archive " + archivePath + ": " + exception.getMessage()); } InputStream decompressedJdkInputStream = jdkInputStream; if (compressorType != null) { decompressedJdkInputStream = new BufferedInputStream( CompressorStreamFactory.getSingleton().createCompressorInputStream(compressorType, jdkInputStream)); } switch (ArchiveStreamFactory.detect(decompressedJdkInputStream)) { case ArchiveStreamFactory.ZIP: if (compressorType != null) { LOG.error("Cannot extract Zip archives that are wrapped in additional compression"); } else { extractZipArchive(archivePath, extractToDirectory); } break; case ArchiveStreamFactory.JAR: extractJarArchive(decompressedJdkInputStream, extractToDirectory); break; case ArchiveStreamFactory.TAR: extractTarArchive(decompressedJdkInputStream, extractToDirectory); break; default: LOG.error("No special handling for archive type " + archivePath + ". Permissions and links will not be properly handled."); extractGenericArchive(decompressedJdkInputStream, extractToDirectory); break; } } } }
ArchiveUtils { public static void extractArchive (Path archivePath, Path extractToDirectory) throws IOException, CompressorException, ArchiveException { try (InputStream jdkInputStream = new BufferedInputStream(Files.newInputStream(archivePath))) { String compressorType = null; try { compressorType = CompressorStreamFactory.detect(jdkInputStream); } catch (CompressorException exception) { LOG.debug("Didn't detect any compression for archive " + archivePath + ": " + exception.getMessage()); } InputStream decompressedJdkInputStream = jdkInputStream; if (compressorType != null) { decompressedJdkInputStream = new BufferedInputStream( CompressorStreamFactory.getSingleton().createCompressorInputStream(compressorType, jdkInputStream)); } switch (ArchiveStreamFactory.detect(decompressedJdkInputStream)) { case ArchiveStreamFactory.ZIP: if (compressorType != null) { LOG.error("Cannot extract Zip archives that are wrapped in additional compression"); } else { extractZipArchive(archivePath, extractToDirectory); } break; case ArchiveStreamFactory.JAR: extractJarArchive(decompressedJdkInputStream, extractToDirectory); break; case ArchiveStreamFactory.TAR: extractTarArchive(decompressedJdkInputStream, extractToDirectory); break; default: LOG.error("No special handling for archive type " + archivePath + ". Permissions and links will not be properly handled."); extractGenericArchive(decompressedJdkInputStream, extractToDirectory); break; } } } private  ArchiveUtils(); }
ArchiveUtils { public static void extractArchive (Path archivePath, Path extractToDirectory) throws IOException, CompressorException, ArchiveException { try (InputStream jdkInputStream = new BufferedInputStream(Files.newInputStream(archivePath))) { String compressorType = null; try { compressorType = CompressorStreamFactory.detect(jdkInputStream); } catch (CompressorException exception) { LOG.debug("Didn't detect any compression for archive " + archivePath + ": " + exception.getMessage()); } InputStream decompressedJdkInputStream = jdkInputStream; if (compressorType != null) { decompressedJdkInputStream = new BufferedInputStream( CompressorStreamFactory.getSingleton().createCompressorInputStream(compressorType, jdkInputStream)); } switch (ArchiveStreamFactory.detect(decompressedJdkInputStream)) { case ArchiveStreamFactory.ZIP: if (compressorType != null) { LOG.error("Cannot extract Zip archives that are wrapped in additional compression"); } else { extractZipArchive(archivePath, extractToDirectory); } break; case ArchiveStreamFactory.JAR: extractJarArchive(decompressedJdkInputStream, extractToDirectory); break; case ArchiveStreamFactory.TAR: extractTarArchive(decompressedJdkInputStream, extractToDirectory); break; default: LOG.error("No special handling for archive type " + archivePath + ". Permissions and links will not be properly handled."); extractGenericArchive(decompressedJdkInputStream, extractToDirectory); break; } } } private  ArchiveUtils(); static void extractArchive(Path archivePath, Path extractToDirectory); static void createArchive(ArchiveType archiveType, Path directoryToArchive, Path archiveFile); }
ArchiveUtils { public static void extractArchive (Path archivePath, Path extractToDirectory) throws IOException, CompressorException, ArchiveException { try (InputStream jdkInputStream = new BufferedInputStream(Files.newInputStream(archivePath))) { String compressorType = null; try { compressorType = CompressorStreamFactory.detect(jdkInputStream); } catch (CompressorException exception) { LOG.debug("Didn't detect any compression for archive " + archivePath + ": " + exception.getMessage()); } InputStream decompressedJdkInputStream = jdkInputStream; if (compressorType != null) { decompressedJdkInputStream = new BufferedInputStream( CompressorStreamFactory.getSingleton().createCompressorInputStream(compressorType, jdkInputStream)); } switch (ArchiveStreamFactory.detect(decompressedJdkInputStream)) { case ArchiveStreamFactory.ZIP: if (compressorType != null) { LOG.error("Cannot extract Zip archives that are wrapped in additional compression"); } else { extractZipArchive(archivePath, extractToDirectory); } break; case ArchiveStreamFactory.JAR: extractJarArchive(decompressedJdkInputStream, extractToDirectory); break; case ArchiveStreamFactory.TAR: extractTarArchive(decompressedJdkInputStream, extractToDirectory); break; default: LOG.error("No special handling for archive type " + archivePath + ". Permissions and links will not be properly handled."); extractGenericArchive(decompressedJdkInputStream, extractToDirectory); break; } } } private  ArchiveUtils(); static void extractArchive(Path archivePath, Path extractToDirectory); static void createArchive(ArchiveType archiveType, Path directoryToArchive, Path archiveFile); static final int DEFAULT_FILE_MODE; static final int DEFAULT_DIRECTORY_MODE; static final int DEFAULT_LINK_MODE; static final int ZIP_LINK_FLAG; static final int OWNER_READ_BIT_MASK; static final int OWNER_WRITE_BIT_MASK; static final int OWNER_EXECUTE_BIT_MASK; static final int GROUP_READ_BIT_MASK; static final int GROUP_WRITE_BIT_MASK; static final int GROUP_EXECUTE_BIT_MASK; static final int OTHERS_READ_BIT_MASK; static final int OTHERS_WRITE_BIT_MASK; static final int OTHERS_EXECUTE_BIT_MASK; }