Search in sources :

Example 16 with ArchiveStreamFactory

use of org.apache.commons.compress.archivers.ArchiveStreamFactory in project hive by apache.

the class CompressionUtils method unTar.

/**
 * Untar an input file into an output file.
 *
 * The output file is created in the output folder, having the same name as the input file, minus
 * the '.tar' extension.
 *
 * @param inputFileName the input .tar file
 * @param outputDirName the output directory file.
 * @throws IOException
 * @throws FileNotFoundException
 *
 * @return The {@link List} of {@link File}s with the untared content.
 * @throws ArchiveException
 */
public static List<File> unTar(final String inputFileName, final String outputDirName, boolean flatten) throws FileNotFoundException, IOException, ArchiveException {
    File inputFile = new File(inputFileName);
    File outputDir = new File(outputDirName);
    final List<File> untaredFiles = new LinkedList<File>();
    InputStream is = null;
    try {
        if (inputFileName.endsWith(".gz")) {
            is = new GzipCompressorInputStream(new FileInputStream(inputFile));
        } else {
            is = new FileInputStream(inputFile);
        }
        final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
        TarArchiveEntry entry = null;
        while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
            final File outputFile = new File(outputDir, entry.getName());
            if (!outputFile.toPath().toAbsolutePath().normalize().startsWith(outputDir.toPath().toAbsolutePath().normalize())) {
                throw new IOException("Untarred file is not under the output directory");
            }
            if (entry.isDirectory()) {
                if (flatten) {
                    // no sub-directories
                    continue;
                }
                LOG.debug(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
                if (!outputFile.exists()) {
                    LOG.debug(String.format("Attempting to create output directory %s.", outputFile.getAbsolutePath()));
                    if (!outputFile.mkdirs()) {
                        throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
                    }
                }
            } else {
                final OutputStream outputFileStream;
                if (flatten) {
                    File flatOutputFile = new File(outputDir, outputFile.getName());
                    LOG.debug(String.format("Creating flat output file %s.", flatOutputFile.getAbsolutePath()));
                    outputFileStream = new FileOutputStream(flatOutputFile);
                } else if (!outputFile.getParentFile().exists()) {
                    LOG.debug(String.format("Attempting to create output directory %s.", outputFile.getParentFile().getAbsoluteFile()));
                    if (!outputFile.getParentFile().getAbsoluteFile().mkdirs()) {
                        throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getParentFile().getAbsolutePath()));
                    }
                    LOG.debug(String.format("Creating output file %s.", outputFile.getAbsolutePath()));
                    outputFileStream = new FileOutputStream(outputFile);
                } else {
                    outputFileStream = new FileOutputStream(outputFile);
                }
                IOUtils.copy(debInputStream, outputFileStream);
                outputFileStream.close();
            }
            untaredFiles.add(outputFile);
        }
        debInputStream.close();
        return untaredFiles;
    } finally {
        if (is != null)
            is.close();
    }
}
Also used : GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) FileInputStream(java.io.FileInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ZipOutputStream(org.apache.tools.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) IOException(java.io.IOException) LinkedList(java.util.LinkedList) FileInputStream(java.io.FileInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 17 with ArchiveStreamFactory

use of org.apache.commons.compress.archivers.ArchiveStreamFactory in project packr by libgdx.

the class ArchiveUtils method createArchive.

/**
 * Creates a new archive from the contents in {@code directoryToArchive}.
 *
 * @param archiveType        the type of archive to create
 * @param directoryToArchive the directory to archive the contents of
 * @param archiveFile        the file to write the archive to
 * @throws IOException      if an IO error occurs
 * @throws ArchiveException if an archive error occurs
 */
public static void createArchive(ArchiveType archiveType, Path directoryToArchive, Path archiveFile) throws IOException, ArchiveException {
    try (OutputStream fileOutputStream = new BufferedOutputStream(Files.newOutputStream(archiveFile));
        ArchiveOutputStream archiveOutputStream = new ArchiveStreamFactory().createArchiveOutputStream(archiveType.getCommonsCompressName(), fileOutputStream)) {
        if (archiveType == ArchiveType.TAR) {
            ((TarArchiveOutputStream) archiveOutputStream).setLongFileMode(LONGFILE_GNU);
        }
        Files.walkFileTree(directoryToArchive, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                createAndPutArchiveEntry(archiveType, archiveOutputStream, directoryToArchive, file);
                archiveOutputStream.closeArchiveEntry();
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                if (Files.isSameFile(dir, directoryToArchive)) {
                    return FileVisitResult.CONTINUE;
                }
                ArchiveEntry entry = archiveOutputStream.createArchiveEntry(dir.toFile(), getRelativePathString(dir, directoryToArchive));
                archiveOutputStream.putArchiveEntry(entry);
                archiveOutputStream.closeArchiveEntry();
                return FileVisitResult.CONTINUE;
            }
        });
        archiveOutputStream.finish();
    }
}
Also used : ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) Path(java.nio.file.Path) BufferedOutputStream(java.io.BufferedOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) OutputStream(java.io.OutputStream) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream) FileVisitResult(java.nio.file.FileVisitResult) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) JarArchiveEntry(org.apache.commons.compress.archivers.jar.JarArchiveEntry) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 18 with ArchiveStreamFactory

use of org.apache.commons.compress.archivers.ArchiveStreamFactory in project packr by libgdx.

the class ArchiveUtilsTest method testArchiveDuplicateEntry.

/**
 * Adds the same entry to a Zip file to ensure that extraction handles duplicates properly.
 */
@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");
    // Create an archive, add entry, update file, add same entry
    try (OutputStream fileOutputStream = new BufferedOutputStream(Files.newOutputStream(archiveZip));
        ArchiveOutputStream archiveOutputStream = new ArchiveStreamFactory().createArchiveOutputStream(ZIP.getCommonsCompressName(), fileOutputStream)) {
        // Create an entry for some file
        ArchiveEntry entry = archiveOutputStream.createArchiveEntry(someFilePath.toFile(), someFilename);
        archiveOutputStream.putArchiveEntry(entry);
        Files.copy(someFilePath, archiveOutputStream);
        archiveOutputStream.closeArchiveEntry();
        // Update some file, and put it into the archive again
        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");
}
Also used : Path(java.nio.file.Path) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) BufferedOutputStream(java.io.BufferedOutputStream) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream) Test(org.junit.jupiter.api.Test)

Example 19 with ArchiveStreamFactory

use of org.apache.commons.compress.archivers.ArchiveStreamFactory in project quickutil by quickutil.

the class CompressUtil method decompressTarGz.

/**
 * 解压tar.gz文件:
 *
 * @return 解压后的根目录路径
 */
public static String decompressTarGz(String sourcePath, String targetPath) {
    String rootPath = null;
    try (FileInputStream fInput = new FileInputStream(sourcePath);
        BufferedInputStream bufInput = new BufferedInputStream(fInput);
        GZIPInputStream gzipInput = new GZIPInputStream(bufInput);
        ArchiveInputStream archiveInput = new ArchiveStreamFactory().createArchiveInputStream("tar", gzipInput)) {
        // tar压缩文件条目
        TarArchiveEntry entry;
        boolean isRootPath = true;
        while ((entry = (TarArchiveEntry) archiveInput.getNextEntry()) != null) {
            String entryName = entry.getName();
            // 转换为目标路径
            if (targetPath != null) {
                entryName = targetPath + File.separator + entryName;
            }
            if (isRootPath) {
                rootPath = entryName;
                isRootPath = false;
            }
            if (entry.isDirectory()) {
                FileUtil.mkdirByFile(entryName);
            } else if (entry.isFile()) {
                FileUtil.stream2file(archiveInput, entryName, false);
            }
        }
    } catch (Exception e) {
        LOGGER.error(Symbol.BLANK, e);
    }
    return rootPath;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 20 with ArchiveStreamFactory

use of org.apache.commons.compress.archivers.ArchiveStreamFactory in project camel by apache.

the class TarFileDataFormat method unmarshal.

@Override
public Object unmarshal(final Exchange exchange, final InputStream stream) throws Exception {
    if (usingIterator) {
        return new TarIterator(exchange.getIn(), stream);
    } else {
        BufferedInputStream bis = new BufferedInputStream(stream);
        TarArchiveInputStream tis = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR, bis);
        OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange);
        try {
            TarArchiveEntry entry = tis.getNextTarEntry();
            if (entry != null) {
                exchange.getOut().setHeader(FILE_NAME, entry.getName());
                IOHelper.copy(tis, osb);
            }
            entry = tis.getNextTarEntry();
            if (entry != null) {
                throw new IllegalStateException("Tar file has more than 1 entry.");
            }
            return osb.build();
        } finally {
            IOHelper.close(osb, tis, bis);
        }
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) BufferedInputStream(java.io.BufferedInputStream) OutputStreamBuilder(org.apache.camel.converter.stream.OutputStreamBuilder) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Aggregations

ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)38 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)18 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)14 File (java.io.File)12 FileInputStream (java.io.FileInputStream)12 ArchiveInputStream (org.apache.commons.compress.archivers.ArchiveInputStream)11 InputStream (java.io.InputStream)10 BufferedInputStream (java.io.BufferedInputStream)9 FileOutputStream (java.io.FileOutputStream)9 IOException (java.io.IOException)9 LinkedList (java.util.LinkedList)9 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)9 ArchiveException (org.apache.commons.compress.archivers.ArchiveException)8 ArchiveOutputStream (org.apache.commons.compress.archivers.ArchiveOutputStream)8 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)8 OutputStream (java.io.OutputStream)7 GZIPInputStream (java.util.zip.GZIPInputStream)5 BufferedOutputStream (java.io.BufferedOutputStream)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Path (java.nio.file.Path)4