@Test public void testExternalTarExtraction (@TempDir Path tempDir) throws IOException, ArchiveException, CompressorException { assumeCreatedSymbolicLink(tempDir); Path archiveFilePath = tempDir.resolve("archive-file.tar"); Files.copy(ArchiveUtilsTest.class.getResourceAsStream("/test-symlink-permissions.tar"), archiveFilePath); Path extractIntoPath = tempDir.resolve("extract-into"); ArchiveUtils.extractArchive(archiveFilePath, extractIntoPath); Path someDir = extractIntoPath.resolve("some-dir"); Path symlinkToSomeFile = someDir.resolve("symlink-to-some-file.txt"); Path someFile = extractIntoPath.resolve("some-file.txt"); Path someScript = extractIntoPath.resolve("some-script.sh"); assertTrue(Files.exists(someDir), "some-dir wasn't extracted from the Tar"); assertTrue(Files.isDirectory(someDir), "some-dir wasn't extracted as a directory"); assertPosixPermissions(someDir, OWNER_READ, OWNER_WRITE, OWNER_EXECUTE, GROUP_READ, GROUP_EXECUTE, OTHERS_READ, OTHERS_EXECUTE); assertTrue(Files.exists(symlinkToSomeFile), "symlink-to-some-file.txt wasn't extracted from the Tar"); assertTrue(Files.isSymbolicLink(symlinkToSomeFile), "symlink-to-some-file.txt wasn't extracted as a symbolic link"); assertTrue(Files.exists(someFile), "some-file.txt wasn't extracted from the Tar"); assertTrue(Files.isRegularFile(someFile), "some-file.txt wasn't extracted as a file"); assertPosixPermissions(someFile, OWNER_READ, OWNER_WRITE, GROUP_READ, OTHERS_READ); assertTrue(Files.exists(someScript), "some-script.sh wasn't extracted from the Tar"); assertTrue(Files.isRegularFile(someScript), "some-script.sh wasn't extracted as a file"); assertPosixPermissions(someScript, OWNER_READ, OWNER_WRITE, OWNER_EXECUTE, GROUP_READ, GROUP_EXECUTE, OTHERS_READ, OTHERS_EXECUTE); }
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; }