Search in sources :

Example 1 with ArchiveStreamFactory

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

the class TarGzCompressionUtils method unTarOneFile.

public static InputStream unTarOneFile(InputStream tarGzInputStream, final String filename) throws FileNotFoundException, IOException, ArchiveException {
    TarArchiveInputStream debInputStream = null;
    InputStream is = null;
    try {
        is = new GzipCompressorInputStream(tarGzInputStream);
        debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
        TarArchiveEntry entry = null;
        while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
            if (entry.getName().contains(filename)) {
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                IOUtils.copy(debInputStream, byteArrayOutputStream);
                return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            }
        }
    } finally {
        IOUtils.closeQuietly(debInputStream);
        IOUtils.closeQuietly(is);
    }
    return null;
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) 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) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 2 with ArchiveStreamFactory

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

the class TarGzCompressionUtils 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 inputFile     the input .tar file
   * @param outputDir     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 File inputFile, final File outputDir) throws FileNotFoundException, IOException, ArchiveException {
    LOGGER.debug(String.format("Untaring %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath()));
    TarArchiveInputStream debInputStream = null;
    InputStream is = null;
    final List<File> untaredFiles = new LinkedList<File>();
    try {
        is = new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(inputFile)));
        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 (entry.isDirectory()) {
                LOGGER.debug(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
                if (!outputFile.exists()) {
                    LOGGER.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 {
                    LOGGER.error("The directory already there. Deleting - " + outputFile.getAbsolutePath());
                    FileUtils.deleteDirectory(outputFile);
                }
            } else {
                LOGGER.debug(String.format("Creating output file %s.", outputFile.getAbsolutePath()));
                File directory = outputFile.getParentFile();
                if (!directory.exists()) {
                    directory.mkdirs();
                }
                OutputStream outputFileStream = null;
                try {
                    outputFileStream = new FileOutputStream(outputFile);
                    IOUtils.copy(debInputStream, outputFileStream);
                } finally {
                    IOUtils.closeQuietly(outputFileStream);
                }
            }
            untaredFiles.add(outputFile);
        }
    } finally {
        IOUtils.closeQuietly(debInputStream);
        IOUtils.closeQuietly(is);
    }
    return untaredFiles;
}
Also used : GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) 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) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) 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) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 3 with ArchiveStreamFactory

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

the class KafkaTest method extractKafka.

private static File extractKafka() throws IOException, ArchiveException, CompressorException {
    File kafkaExtract = TMP_FOLDER.newFolder();
    InputStream kakfaResource = KafkaTest.class.getClassLoader().getResourceAsStream("kafka-0.7.2.tgz");
    ArchiveInputStream archiveInput = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR, new CompressorStreamFactory().createCompressorInputStream(CompressorStreamFactory.GZIP, kakfaResource));
    try {
        ArchiveEntry entry = archiveInput.getNextEntry();
        while (entry != null) {
            File file = new File(kafkaExtract, entry.getName());
            if (entry.isDirectory()) {
                file.mkdirs();
            } else {
                ByteStreams.copy(archiveInput, Files.newOutputStreamSupplier(file));
            }
            entry = archiveInput.getNextEntry();
        }
    } finally {
        archiveInput.close();
    }
    return kafkaExtract;
}
Also used : ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) InputStream(java.io.InputStream) CompressorStreamFactory(org.apache.commons.compress.compressors.CompressorStreamFactory) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) File(java.io.File)

Example 4 with ArchiveStreamFactory

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

the class ArchiveNavigator method untar.

/*
     * Un-TAR Once items are saved off to temp folder, they'll be converted by
     * the file converter. The converter can choose to do something else with
     * them.
     */
public File untar(File tarFile) throws IOException, ConfigException {
    String _working = FilenameUtils.concat(getWorkingDir(), FilenameUtils.getBaseName(tarFile.getPath()));
    if (_working == null) {
        throw new IOException("Invalid archive path for " + tarFile.getPath());
    }
    File workingDir = new File(_working);
    workingDir.mkdir();
    InputStream input = new BufferedInputStream(new FileInputStream(tarFile));
    TarArchiveInputStream in = null;
    try {
        in = (TarArchiveInputStream) (new ArchiveStreamFactory().createArchiveInputStream("tar", input));
        TarArchiveEntry tarEntry;
        while ((tarEntry = (TarArchiveEntry) in.getNextEntry()) != null) {
            if (filterEntry(tarEntry)) {
                continue;
            }
            try {
                File tmpFile = saveArchiveEntry(tarEntry, in, _working);
                converter.convert(tmpFile);
            } catch (IOException err) {
                log.error("Unable to save item, FILE=" + tarFile.getName() + "!" + tarEntry.getName(), err);
            }
        }
    } catch (ArchiveException ae) {
        throw new IOException(ae);
    } finally {
        in.close();
    }
    return workingDir;
}
Also used : ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) GZIPInputStream(java.util.zip.GZIPInputStream) ArchiveException(org.apache.commons.compress.archivers.ArchiveException)

Example 5 with ArchiveStreamFactory

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

the class SubmitByMergeIfNecessaryIT method testPreviewSubmitTgz.

@Test
public void testPreviewSubmitTgz() throws Exception {
    Project.NameKey p1 = createProject("project-name");
    TestRepository<?> repo1 = cloneProject(p1);
    PushOneCommit.Result change1 = createChange(repo1, "master", "test", "a.txt", "1", "topic");
    approve(change1.getChangeId());
    // get a preview before submitting:
    File tempfile;
    try (BinaryResult request = submitPreview(change1.getChangeId(), "tgz")) {
        assertThat(request.getContentType()).isEqualTo("application/x-gzip");
        tempfile = File.createTempFile("test", null);
        request.writeTo(Files.newOutputStream(tempfile.toPath()));
    }
    InputStream is = new GZIPInputStream(Files.newInputStream(tempfile.toPath()));
    List<String> untarredFiles = new ArrayList<>();
    try (TarArchiveInputStream tarInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is)) {
        TarArchiveEntry entry = null;
        while ((entry = (TarArchiveEntry) tarInputStream.getNextEntry()) != null) {
            untarredFiles.add(entry.getName());
        }
    }
    assertThat(untarredFiles).containsExactly(name("project-name") + ".git");
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) GZIPInputStream(java.util.zip.GZIPInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) Project(com.google.gerrit.reviewdb.client.Project) File(java.io.File) BinaryResult(com.google.gerrit.extensions.restapi.BinaryResult) Test(org.junit.Test)

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