Search in sources :

Example 41 with GzipCompressorInputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project incubator-gobblin by apache.

the class StreamUtilsTest method testTarFile.

@Test
public void testTarFile() throws IOException {
    FileSystem localFs = FileSystem.getLocal(new Configuration());
    // Set of expected Paths to be in the resulting tar file
    Set<Path> expectedPaths = Sets.newHashSet();
    // Create input file path
    Path testFile = new Path("testFile");
    expectedPaths.add(testFile);
    // Create output file path
    Path testOutFile = new Path("testTarOut" + UUID.randomUUID() + ".tar.gz");
    try {
        // Create the input file
        FSDataOutputStream testFileOut1 = localFs.create(testFile);
        testFileOut1.close();
        // tar the input file to the specific output file
        StreamUtils.tar(localFs, testFile, testOutFile);
        // Confirm the contents of the tar file are valid
        try (TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(new GzipCompressorInputStream(localFs.open(testOutFile)))) {
            TarArchiveEntry tarArchiveEntry;
            while (null != (tarArchiveEntry = tarArchiveInputStream.getNextTarEntry())) {
                MatcherAssert.assertThat(new Path(tarArchiveEntry.getName()), Matchers.isIn(expectedPaths));
            }
        }
    } finally {
        if (localFs.exists(testFile)) {
            localFs.delete(testFile, true);
        }
        if (localFs.exists(testOutFile)) {
            localFs.delete(testOutFile, true);
        }
    }
}
Also used : Path(org.apache.hadoop.fs.Path) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) Configuration(org.apache.hadoop.conf.Configuration) FileSystem(org.apache.hadoop.fs.FileSystem) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) Test(org.testng.annotations.Test)

Example 42 with GzipCompressorInputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project twister2 by DSC-SPIDAL.

the class KubernetesWorker method unpackJobPackage.

/**
 * unpack the received job package
 * job package needs to be a tar.gz package
 * it unpacks to the directory where the job package resides
 * @param sourceGzip
 * @return
 */
private static boolean unpackJobPackage(final String sourceGzip) {
    File sourceGzipFile = new File(sourceGzip);
    File outputDir = sourceGzipFile.getParentFile();
    GzipCompressorInputStream gzIn = null;
    TarArchiveInputStream tarInputStream = null;
    try {
        // construct input stream
        InputStream fin = Files.newInputStream(Paths.get(sourceGzip));
        BufferedInputStream in = new BufferedInputStream(fin);
        gzIn = new GzipCompressorInputStream(in);
        tarInputStream = new TarArchiveInputStream(gzIn);
        TarArchiveEntry entry = null;
        while ((entry = (TarArchiveEntry) tarInputStream.getNextEntry()) != null) {
            File outputFile = new File(outputDir, entry.getName());
            if (!outputFile.getParentFile().exists()) {
                boolean dirCreated = outputFile.getParentFile().mkdirs();
                if (!dirCreated) {
                    LOG.severe("Can not create the output directory: " + outputFile.getParentFile() + "\nFile unpack is unsuccessful.");
                    return false;
                }
            }
            if (!outputFile.isDirectory()) {
                final OutputStream outputFileStream = new FileOutputStream(outputFile);
                IOUtils.copy(tarInputStream, outputFileStream);
                outputFileStream.close();
            // LOG.info("Unpacked the file: " + outputFile.getAbsolutePath());
            }
        }
        tarInputStream.close();
        gzIn.close();
        return true;
    } catch (IOException e) {
        LOG.log(Level.SEVERE, "Exception when unpacking job package. ", e);
        return false;
    }
}
Also used : GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) 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) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 43 with GzipCompressorInputStream

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

the class TarGzs method untargz.

public static void untargz(final InputStream read, final File destination, final boolean noparent, final FileFilter fileFilter) throws IOException {
    Objects.requireNonNull(fileFilter, "'fileFilter' is required.");
    Files.dir(destination);
    Files.writable(destination);
    try {
        GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(read);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn);
        TarArchiveEntry entry;
        while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
            String path = entry.getName();
            if (noparent) {
                path = path.replaceFirst("^[^/]+/", "");
            }
            File file = new File(destination, path);
            if (!fileFilter.accept(file))
                continue;
            if (entry.isDirectory()) {
                Files.mkdir(file);
            } else {
                Files.mkdir(file.getParentFile());
                IO.copy(tarIn, file);
                long lastModified = entry.getLastModifiedDate().getTime();
                if (lastModified > 0L) {
                    file.setLastModified(lastModified);
                }
                // Elasticsearch tar has entries with 33261 that are executable
                if (33261 == entry.getMode()) {
                    file.setExecutable(true);
                }
                // Kibana tar has entries with 493 that are executable
                if (493 == entry.getMode()) {
                    file.setExecutable(true);
                }
            }
        }
        tarIn.close();
    } catch (IOException var9) {
        throw new IOException("Unable to unzip " + read, var9);
    }
}
Also used : GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) IOException(java.io.IOException) File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 44 with GzipCompressorInputStream

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

the class AxiomReranker method getReadFileStream.

public InputStream getReadFileStream(String path) throws IOException {
    InputStream fin = Files.newInputStream(Paths.get(path), StandardOpenOption.READ);
    BufferedInputStream in = new BufferedInputStream(fin);
    if (path.endsWith(".bz2")) {
        BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(in);
        return bzIn;
    } else if (path.endsWith(".gz")) {
        GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
        return gzIn;
    } else if (path.endsWith(".zip")) {
        GzipCompressorInputStream zipIn = new GzipCompressorInputStream(in);
        return zipIn;
    }
    return in;
}
Also used : GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) InputStream(java.io.InputStream)

Example 45 with GzipCompressorInputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project spring-boot by spring-projects.

the class TarGzipBuildpack method copyAndRebaseEntries.

private void copyAndRebaseEntries(OutputStream outputStream) throws IOException {
    String id = this.coordinates.getSanitizedId();
    Path basePath = Paths.get("/cnb/buildpacks/", id, this.coordinates.getVersion());
    try (TarArchiveInputStream tar = new TarArchiveInputStream(new GzipCompressorInputStream(Files.newInputStream(this.path)));
        TarArchiveOutputStream output = new TarArchiveOutputStream(outputStream)) {
        writeBasePathEntries(output, basePath);
        TarArchiveEntry entry = tar.getNextTarEntry();
        while (entry != null) {
            entry.setName(basePath + "/" + entry.getName());
            output.putArchiveEntry(entry);
            StreamUtils.copy(tar, output);
            output.closeArchiveEntry();
            entry = tar.getNextTarEntry();
        }
        output.finish();
    }
}
Also used : Path(java.nio.file.Path) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Aggregations

GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)58 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)46 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)40 IOException (java.io.IOException)29 FileInputStream (java.io.FileInputStream)26 File (java.io.File)23 BufferedInputStream (java.io.BufferedInputStream)22 FileOutputStream (java.io.FileOutputStream)20 InputStream (java.io.InputStream)16 OutputStream (java.io.OutputStream)10 Path (java.nio.file.Path)9 ArrayList (java.util.ArrayList)8 BufferedOutputStream (java.io.BufferedOutputStream)7 ByteArrayInputStream (java.io.ByteArrayInputStream)7 BZip2CompressorInputStream (org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream)6 BufferedReader (java.io.BufferedReader)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 InputStreamReader (java.io.InputStreamReader)4 URL (java.net.URL)4 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)4