Search in sources :

Example 56 with GzipCompressorInputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project frontend-maven-plugin by eirslett.

the class DefaultArchiveExtractor method extract.

@Override
public void extract(String archive, String destinationDirectory) throws ArchiveExtractionException {
    final File archiveFile = new File(archive);
    try (FileInputStream fis = new FileInputStream(archiveFile)) {
        if ("msi".equals(FileUtils.getExtension(archiveFile.getAbsolutePath()))) {
            String command = "msiexec /a " + archiveFile.getAbsolutePath() + " /qn TARGETDIR=\"" + destinationDirectory + "\"";
            Process child = Runtime.getRuntime().exec(command);
            try {
                int result = child.waitFor();
                if (result != 0) {
                    throw new ArchiveExtractionException("Could not extract " + archiveFile.getAbsolutePath() + "; return code " + result);
                }
            } catch (InterruptedException e) {
                throw new ArchiveExtractionException("Unexpected interruption of while waiting for extraction process", e);
            }
        } else if ("zip".equals(FileUtils.getExtension(archiveFile.getAbsolutePath()))) {
            ZipFile zipFile = new ZipFile(archiveFile);
            try {
                Enumeration<? extends ZipEntry> entries = zipFile.entries();
                while (entries.hasMoreElements()) {
                    ZipEntry entry = entries.nextElement();
                    final File destPath = new File(destinationDirectory + File.separator + entry.getName());
                    prepDestination(destPath, entry.isDirectory());
                    if (!entry.isDirectory()) {
                        InputStream in = null;
                        OutputStream out = null;
                        try {
                            in = zipFile.getInputStream(entry);
                            out = new FileOutputStream(destPath);
                            IOUtils.copy(in, out);
                        } finally {
                            IOUtils.closeQuietly(in);
                            IOUtils.closeQuietly(out);
                        }
                    }
                }
            } finally {
                zipFile.close();
            }
        } else {
            // TarArchiveInputStream can be constructed with a normal FileInputStream if
            // we ever need to extract regular '.tar' files.
            TarArchiveInputStream tarIn = null;
            try {
                tarIn = new TarArchiveInputStream(new GzipCompressorInputStream(fis));
                TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
                String canonicalDestinationDirectory = new File(destinationDirectory).getCanonicalPath();
                while (tarEntry != null) {
                    // Create a file for this tarEntry
                    final File destPath = new File(destinationDirectory + File.separator + tarEntry.getName());
                    prepDestination(destPath, tarEntry.isDirectory());
                    if (!startsWithPath(destPath.getCanonicalPath(), canonicalDestinationDirectory)) {
                        throw new IOException("Expanding " + tarEntry.getName() + " would create file outside of " + canonicalDestinationDirectory);
                    }
                    if (!tarEntry.isDirectory()) {
                        destPath.createNewFile();
                        boolean isExecutable = (tarEntry.getMode() & 0100) > 0;
                        destPath.setExecutable(isExecutable);
                        OutputStream out = null;
                        try {
                            out = new FileOutputStream(destPath);
                            IOUtils.copy(tarIn, out);
                        } finally {
                            IOUtils.closeQuietly(out);
                        }
                    }
                    tarEntry = tarIn.getNextTarEntry();
                }
            } finally {
                IOUtils.closeQuietly(tarIn);
            }
        }
    } catch (IOException e) {
        throw new ArchiveExtractionException("Could not extract archive: '" + archive + "'", e);
    }
}
Also used : GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) Enumeration(java.util.Enumeration) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) FileInputStream(java.io.FileInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ZipFile(java.util.zip.ZipFile) FileOutputStream(java.io.FileOutputStream) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 57 with GzipCompressorInputStream

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

the class GetArchiveIT method getTgzArchive.

@Test
public void getTgzArchive() throws Exception {
    BinaryResult res = gApi.changes().id(changeId).current().getArchive(ArchiveFormat.TGZ);
    assertThat(res.getAttachmentName()).isEqualTo(commit.abbreviate(ObjectIds.ABBREV_STR_LEN).name() + ".tar.gz");
    assertThat(res.getContentType()).isEqualTo("application/x-gzip");
    assertThat(res.canGzip()).isFalse();
    byte[] archiveBytes = getBinaryContent(res);
    try (ByteArrayInputStream in = new ByteArrayInputStream(archiveBytes);
        GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(in)) {
        HashMap<String, String> archiveEntries = getTarContent(gzipIn);
        assertThat(archiveEntries).containsExactly(DIRECTORY_NAME + "/", null, FILE_NAME, FILE_CONTENT);
    }
}
Also used : GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) BinaryResult(com.google.gerrit.extensions.restapi.BinaryResult) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 58 with GzipCompressorInputStream

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

the class TarGzipPacker method unpack.

/**
 * unpackage the given tar.gz file to the provided output directory
 */
public static boolean unpack(final Path sourceGzip, Path outputDir) {
    GzipCompressorInputStream gzIn = null;
    TarArchiveInputStream tarInputStream = null;
    try {
        // construct input stream
        InputStream fin = Files.newInputStream(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.toFile(), 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();
                // if the file has sh extension, make it executable
                if (entry.getName().endsWith(".sh")) {
                    outputFile.setExecutable(true);
                }
            }
        }
        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) ByteArrayInputStream(java.io.ByteArrayInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ZipFile(java.util.zip.ZipFile) File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 59 with GzipCompressorInputStream

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

the class StreamUtilsTest method testTarDir.

@Test
public void testTarDir() 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 directory
    Path testInDir = new Path("testDir");
    expectedPaths.add(testInDir);
    // Create output file path
    Path testOutFile = new Path("testTarOut" + UUID.randomUUID() + ".tar.gz");
    try {
        localFs.mkdirs(testInDir);
        // Create a test file path
        Path testFile1 = new Path(testInDir, "testFile1");
        expectedPaths.add(testFile1);
        FSDataOutputStream testFileOut1 = localFs.create(testFile1);
        testFileOut1.close();
        // Create a test file path
        Path testFile2 = new Path(testInDir, "testFile2");
        expectedPaths.add(testFile2);
        FSDataOutputStream testFileOut2 = localFs.create(testFile2);
        testFileOut2.close();
        // tar the input directory to the specific output file
        StreamUtils.tar(localFs, testInDir, 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())) {
                assertThat(new Path(tarArchiveEntry.getName()), isIn(expectedPaths));
            }
        }
    } finally {
        if (localFs.exists(testInDir)) {
            localFs.delete(testInDir, 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 60 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)

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