Search in sources :

Example 31 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project jib by google.

the class TarStreamBuilderTest method testToBlob.

@Test
public void testToBlob() throws IOException {
    Blob blob = testTarStreamBuilder.toBlob();
    // Writes the BLOB and captures the output.
    ByteArrayOutputStream tarByteOutputStream = new ByteArrayOutputStream();
    blob.writeTo(tarByteOutputStream);
    // Rearrange the output into input for verification.
    ByteArrayInputStream tarByteInputStream = new ByteArrayInputStream(tarByteOutputStream.toByteArray());
    TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(tarByteInputStream);
    verifyTarArchive(tarArchiveInputStream);
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) Blob(com.google.cloud.tools.jib.blob.Blob) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 32 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project onebusaway-application-modules by camsys.

the class FileUtility 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 List<File> unTar(final File inputFile, final File outputDir) throws FileNotFoundException, IOException, ArchiveException {
    _log.info(String.format("Untaring %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath()));
    final List<File> untaredFiles = new LinkedList<File>();
    final InputStream is = new FileInputStream(inputFile);
    final TarArchiveInputStream 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()) {
            _log.info(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
            if (!outputFile.exists()) {
                _log.info(String.format("Attempting to create output directory %s.", outputFile.getAbsolutePath()));
                if (!outputFile.mkdirs()) {
                    throw new IllegalStateException(String.format("CHUNouldn't create directory %s.", outputFile.getAbsolutePath()));
                }
            }
        } else {
            _log.info(String.format("Creating output file %s.", outputFile.getAbsolutePath()));
            final OutputStream outputFileStream = new FileOutputStream(outputFile);
            IOUtils.copy(debInputStream, outputFileStream);
            outputFileStream.close();
        }
        untaredFiles.add(outputFile);
    }
    debInputStream.close();
    return untaredFiles;
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) GZIPInputStream(java.util.zip.GZIPInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) DataOutputStream(java.io.DataOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) LinkedList(java.util.LinkedList) FileInputStream(java.io.FileInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 33 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project tycho by eclipse.

the class TarGzArchiverTest method getTarEntry.

private byte[] getTarEntry(String name) throws IOException {
    TarArchiveInputStream tarStream = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(tarGzArchive)));
    try {
        TarArchiveEntry tarEntry = null;
        while ((tarEntry = tarStream.getNextTarEntry()) != null) {
            if (name.equals(tarEntry.getName())) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                IOUtils.copy(tarStream, baos);
                return baos.toByteArray();
            }
        }
    } finally {
        tarStream.close();
    }
    throw new IOException(name + " not found in " + tarGzArchive);
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 34 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project tycho by eclipse.

the class TarGzArchiverTest method getTarEntries.

private Map<String, TarArchiveEntry> getTarEntries() throws IOException, FileNotFoundException {
    TarArchiveInputStream tarStream = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(tarGzArchive)));
    Map<String, TarArchiveEntry> entries = new HashMap<>();
    try {
        TarArchiveEntry tarEntry = null;
        while ((tarEntry = tarStream.getNextTarEntry()) != null) {
            entries.put(tarEntry.getName(), tarEntry);
        }
    } finally {
        tarStream.close();
    }
    return entries;
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) HashMap(java.util.HashMap) FileInputStream(java.io.FileInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 35 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project docker-client by spotify.

the class DefaultDockerClientTest method testExportContainer.

@Test
public void testExportContainer() throws Exception {
    // Pull image
    sut.pull(BUSYBOX_LATEST);
    // Create container
    final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).build();
    final String name = randomName();
    final ContainerCreation creation = sut.createContainer(config, name);
    final String id = creation.id();
    final ImmutableSet.Builder<String> files = ImmutableSet.builder();
    try (TarArchiveInputStream tarStream = new TarArchiveInputStream(sut.exportContainer(id))) {
        TarArchiveEntry entry;
        while ((entry = tarStream.getNextTarEntry()) != null) {
            files.add(entry.getName());
        }
    }
    // Check that some common files exist
    assertThat(files.build(), both(hasItem("bin/")).and(hasItem("bin/sh")));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) ImmutableSet(com.google.common.collect.ImmutableSet) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) Test(org.junit.Test)

Aggregations

TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)131 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)98 File (java.io.File)51 IOException (java.io.IOException)49 FileInputStream (java.io.FileInputStream)45 GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)45 InputStream (java.io.InputStream)34 FileOutputStream (java.io.FileOutputStream)33 BufferedInputStream (java.io.BufferedInputStream)30 ByteArrayInputStream (java.io.ByteArrayInputStream)27 Test (org.junit.Test)23 ArrayList (java.util.ArrayList)20 GZIPInputStream (java.util.zip.GZIPInputStream)20 ByteArrayOutputStream (java.io.ByteArrayOutputStream)19 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)17 Path (java.nio.file.Path)16 OutputStream (java.io.OutputStream)15 BufferedOutputStream (java.io.BufferedOutputStream)12 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)12 ArchiveInputStream (org.apache.commons.compress.archivers.ArchiveInputStream)7