Search in sources :

Example 51 with ArchiveEntry

use of org.apache.commons.compress.archivers.ArchiveEntry in project grunt-maven-plugin by allegro.

the class TarUtil method untar.

public static void untar(File source, File target, Log logger) {
    TarArchiveInputStream tarInput = null;
    ArchiveEntry entry;
    OutputStream output = null;
    try {
        tarInput = new TarArchiveInputStream(new FileInputStream(source));
        entry = tarInput.getNextEntry();
        while (entry != null) {
            File outputFile = new File(target.getCanonicalPath() + File.separator + entry.getName());
            if (entry.isDirectory()) {
                logger.debug("creating dir at: " + outputFile.getCanonicalPath());
                outputFile.mkdirs();
            } else {
                logger.debug("creating file at: " + outputFile.getCanonicalPath());
                output = new FileOutputStream(outputFile);
                IOUtils.copy(tarInput, output);
                output.flush();
                output.close();
            }
            entry = tarInput.getNextEntry();
        }
    } catch (IOException exception) {
        throw new IllegalStateException(exception);
    } finally {
        IOUtils.closeQuietly(tarInput);
        IOUtils.closeQuietly(output);
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry)

Example 52 with ArchiveEntry

use of org.apache.commons.compress.archivers.ArchiveEntry in project presto by prestodb.

the class TargzBasedPackageSupplier method getRootDirectory.

private static String getRootDirectory(File packageFile) {
    try (TarArchiveInputStream archiveInputStream = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(packageFile)))) {
        ArchiveEntry entry = archiveInputStream.getNextEntry();
        if (entry == null) {
            throw new IllegalArgumentException(format("Archive is empty: %s", packageFile));
        }
        Path path = Paths.get(entry.getName());
        return path.getName(0).toString();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) Path(java.nio.file.Path) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) FileInputStream(java.io.FileInputStream)

Example 53 with ArchiveEntry

use of org.apache.commons.compress.archivers.ArchiveEntry in project presto by prestodb.

the class TargzBasedPackageSupplier method extractPackage.

private static void extractPackage(File packageFile, File outputDirectory) {
    try (TarArchiveInputStream archiveInputStream = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(packageFile)))) {
        while (true) {
            ArchiveEntry entry = archiveInputStream.getNextEntry();
            if (entry == null) {
                break;
            }
            File output = new File(outputDirectory, entry.getName());
            if (entry.isDirectory()) {
                if (output.exists()) {
                    verify(output.isDirectory(), "package directory is not a directory: %s", output);
                    continue;
                }
                createDirectories(output.toPath());
            } else {
                File directory = output.getParentFile();
                if (!directory.exists()) {
                    createDirectories(directory.toPath());
                }
                try (OutputStream outputStream = new FileOutputStream(output)) {
                    ByteStreams.copy(archiveInputStream, outputStream);
                }
            }
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) LauncherUtils.checkFile(com.facebook.presto.spark.launcher.LauncherUtils.checkFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 54 with ArchiveEntry

use of org.apache.commons.compress.archivers.ArchiveEntry in project ice by Netflix.

the class BillingFileProcessor method processBillingZipFile.

private void processBillingZipFile(File file, boolean withTags) throws IOException {
    InputStream input = new FileInputStream(file);
    ZipArchiveInputStream zipInput = new ZipArchiveInputStream(input);
    try {
        ArchiveEntry entry;
        while ((entry = zipInput.getNextEntry()) != null) {
            if (entry.isDirectory())
                continue;
            processBillingFile(entry.getName(), zipInput, withTags);
        }
    } catch (IOException e) {
        if (e.getMessage().equals("Stream closed"))
            logger.info("reached end of file.");
        else
            logger.error("Error processing " + file, e);
    } finally {
        try {
            zipInput.close();
        } catch (IOException e) {
            logger.error("Error closing " + file, e);
        }
        try {
            input.close();
        } catch (IOException e1) {
            logger.error("Cannot close input for " + file, e1);
        }
    }
}
Also used : ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry)

Example 55 with ArchiveEntry

use of org.apache.commons.compress.archivers.ArchiveEntry in project structr by structr.

the class UnarchiveCommand method handleArchiveInputStream.

private void handleArchiveInputStream(final ArchiveInputStream in, final App app, final SecurityContext securityContext, final Folder existingParentFolder) throws FrameworkException, IOException {
    int overallCount = 0;
    ArchiveEntry entry = in.getNextEntry();
    while (entry != null) {
        try (final Tx tx = app.tx(true, true, false)) {
            // don't send notifications for bulk commands
            int count = 0;
            while (entry != null && count++ < 50) {
                final String entryPath = "/" + PathHelper.clean(entry.getName());
                logger.info("Entry path: {}", entryPath);
                if (entry.isDirectory()) {
                    handleDirectory(securityContext, existingParentFolder, entryPath);
                } else {
                    handleFile(securityContext, in, existingParentFolder, entryPath);
                }
                entry = in.getNextEntry();
                overallCount++;
            }
            logger.info("Committing transaction after {} entries.", overallCount);
            tx.success();
        }
    }
    logger.info("Unarchived {} entries.", overallCount);
}
Also used : Tx(org.structr.core.graph.Tx) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) SevenZArchiveEntry(org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry)

Aggregations

ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)62 File (java.io.File)24 FileInputStream (java.io.FileInputStream)24 IOException (java.io.IOException)22 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)20 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)19 ZipArchiveInputStream (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)17 InputStream (java.io.InputStream)16 FileOutputStream (java.io.FileOutputStream)11 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)11 BufferedInputStream (java.io.BufferedInputStream)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 Path (java.nio.file.Path)9 ArchiveInputStream (org.apache.commons.compress.archivers.ArchiveInputStream)9 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 ArchiveException (org.apache.commons.compress.archivers.ArchiveException)6 OutputStream (java.io.OutputStream)5 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)5 BufferedOutputStream (java.io.BufferedOutputStream)4