Search in sources :

Example 51 with GzipCompressorInputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream 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 52 with GzipCompressorInputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream 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 53 with GzipCompressorInputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project flink by apache.

the class CompressionUtils method extractTarFileUsingJava.

// Follow the pattern suggested in
// https://commons.apache.org/proper/commons-compress/examples.html
private static void extractTarFileUsingJava(String inFilePath, String targetDirPath, boolean gzipped) throws IOException {
    try (InputStream fi = Files.newInputStream(Paths.get(inFilePath));
        InputStream bi = new BufferedInputStream(fi);
        final TarArchiveInputStream tai = new TarArchiveInputStream(gzipped ? new GzipCompressorInputStream(bi) : bi)) {
        final File targetDir = new File(targetDirPath);
        TarArchiveEntry entry;
        while ((entry = tai.getNextTarEntry()) != null) {
            unpackEntry(tai, entry, targetDir);
        }
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) InputStream(java.io.InputStream) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 54 with GzipCompressorInputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project alluxio by Alluxio.

the class TarUtils method decompress.

/**
 * Decompresses a tarball to one destination.
 *
 * @param in the input file path
 * @param out destination to decompress files to
 */
public static void decompress(String in, File out) throws IOException {
    try (TarArchiveInputStream fin = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(in)))) {
        TarArchiveEntry entry;
        while ((entry = fin.getNextTarEntry()) != null) {
            if (entry.isDirectory()) {
                continue;
            }
            File curfile = new File(out, entry.getName());
            File parent = curfile.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }
            IOUtils.copy(fin, new FileOutputStream(curfile));
        }
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 55 with GzipCompressorInputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project MSEC by Tencent.

the class GzipUtil method unzip.

public static void unzip(String srcFile) throws Exception {
    GzipCompressorInputStream in = new GzipCompressorInputStream(new FileInputStream(srcFile));
    int index = srcFile.indexOf(".gz");
    String destFile = "";
    if (index == srcFile.length() - 3) {
        destFile = srcFile.substring(0, index);
    } else {
        destFile = srcFile + ".decompress";
    }
    FileOutputStream out = new FileOutputStream(destFile);
    byte[] buf = new byte[10240];
    while (true) {
        int len = in.read(buf);
        if (len <= 0) {
            break;
        }
        out.write(buf, 0, len);
    }
    out.flush();
    out.close();
    in.close();
}
Also used : GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) FileOutputStream(java.io.FileOutputStream) FileInputStream(java.io.FileInputStream)

Aggregations

GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)60 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)47 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)41 IOException (java.io.IOException)30 FileInputStream (java.io.FileInputStream)27 BufferedInputStream (java.io.BufferedInputStream)24 File (java.io.File)24 FileOutputStream (java.io.FileOutputStream)21 InputStream (java.io.InputStream)18 OutputStream (java.io.OutputStream)12 Path (java.nio.file.Path)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 ArrayList (java.util.ArrayList)8 BufferedOutputStream (java.io.BufferedOutputStream)7 BZip2CompressorInputStream (org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream)6 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)5 BufferedReader (java.io.BufferedReader)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 InputStreamReader (java.io.InputStreamReader)4 URL (java.net.URL)4