Search in sources :

Example 46 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project alluxio by Alluxio.

the class TarUtils method readTarGz.

/**
 * Reads a gzipped tar archive from a stream and writes it to the given path.
 *
 * @param dirPath the path to write the archive to
 * @param input the input stream
 */
public static void readTarGz(Path dirPath, InputStream input) throws IOException {
    InputStream zipStream = new GzipCompressorInputStream(input);
    TarArchiveInputStream archiveStream = new TarArchiveInputStream(zipStream);
    TarArchiveEntry entry;
    while ((entry = (TarArchiveEntry) archiveStream.getNextEntry()) != null) {
        File outputFile = new File(dirPath.toFile(), entry.getName());
        if (entry.isDirectory()) {
            outputFile.mkdirs();
        } else {
            outputFile.getParentFile().mkdirs();
            try (FileOutputStream fileOut = new FileOutputStream(outputFile)) {
                IOUtils.copy(archiveStream, fileOut);
            }
        }
    }
}
Also used : GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 47 with TarArchiveInputStream

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

the class ConfigUtils method getArchiveInputStream.

public static ArchiveInputStream getArchiveInputStream(String solrArchiveName, InputStream is) throws IOException {
    String archiveFormat;
    String solrArchiveExtension = FilenameUtils.getExtension(solrArchiveName);
    if (solrArchiveExtension == null || solrArchiveExtension.isEmpty()) {
        // assume that the archiveExtension was parsed
        archiveFormat = solrArchiveName;
    } else {
        archiveFormat = SUPPORTED_SOLR_ARCHIVE_FORMAT.get(solrArchiveExtension);
    }
    ArchiveInputStream ais;
    if ("zip".equals(archiveFormat)) {
        ais = new ZipArchiveInputStream(is);
    } else {
        if ("gz".equals(archiveFormat)) {
            is = new GZIPInputStream(is);
        } else if ("bz2".equals(archiveFormat)) {
            is = new BZip2CompressorInputStream(is);
        } else {
            throw new IllegalStateException("Unsupported compression format " + archiveFormat + "!. " + "Please report this to stanbol-dev mailing list!");
        }
        ais = new TarArchiveInputStream(is);
    }
    return ais;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)

Example 48 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project heron by twitter.

the class Extractor method extract.

static void extract(InputStream in, Path destination) throws IOException {
    try (final BufferedInputStream bufferedInputStream = new BufferedInputStream(in);
        final GzipCompressorInputStream gzipInputStream = new GzipCompressorInputStream(bufferedInputStream);
        final TarArchiveInputStream tarInputStream = new TarArchiveInputStream(gzipInputStream)) {
        final String destinationAbsolutePath = destination.toFile().getAbsolutePath();
        TarArchiveEntry entry;
        while ((entry = (TarArchiveEntry) tarInputStream.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                File f = Paths.get(destinationAbsolutePath, entry.getName()).toFile();
                f.mkdirs();
            } else {
                Path fileDestinationPath = Paths.get(destinationAbsolutePath, entry.getName());
                Files.copy(tarInputStream, fileDestinationPath, StandardCopyOption.REPLACE_EXISTING);
            }
        }
    }
}
Also used : GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) Path(java.nio.file.Path) BufferedInputStream(java.io.BufferedInputStream) File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 49 with TarArchiveInputStream

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

the class FileProjectManager method untar.

protected void untar(File destDir, InputStream inputStream) throws IOException {
    TarArchiveInputStream tin = new TarArchiveInputStream(inputStream);
    TarArchiveEntry tarEntry = null;
    while ((tarEntry = tin.getNextTarEntry()) != null) {
        File destEntry = new File(destDir, tarEntry.getName());
        File parent = destEntry.getParentFile();
        if (!parent.exists()) {
            parent.mkdirs();
        }
        if (tarEntry.isDirectory()) {
            destEntry.mkdirs();
        } else {
            FileOutputStream fout = new FileOutputStream(destEntry);
            try {
                IOUtils.copy(tin, fout);
            } finally {
                fout.close();
            }
        }
    }
    tin.close();
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 50 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project spring-boot by spring-projects.

the class TarLayoutWriterTests method writesTarArchive.

@Test
void writesTarArchive() throws Exception {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try (TarLayoutWriter writer = new TarLayoutWriter(outputStream)) {
        writer.directory("/foo", Owner.ROOT);
        writer.file("/foo/bar.txt", Owner.of(1, 1), 0777, Content.of("test"));
    }
    try (TarArchiveInputStream tarInputStream = new TarArchiveInputStream(new ByteArrayInputStream(outputStream.toByteArray()))) {
        TarArchiveEntry directoryEntry = tarInputStream.getNextTarEntry();
        TarArchiveEntry fileEntry = tarInputStream.getNextTarEntry();
        byte[] fileContent = new byte[(int) fileEntry.getSize()];
        tarInputStream.read(fileContent);
        assertThat(tarInputStream.getNextEntry()).isNull();
        assertThat(directoryEntry.getName()).isEqualTo("/foo/");
        assertThat(directoryEntry.getMode()).isEqualTo(0755);
        assertThat(directoryEntry.getLongUserId()).isEqualTo(0);
        assertThat(directoryEntry.getLongGroupId()).isEqualTo(0);
        assertThat(directoryEntry.getModTime()).isEqualTo(new Date(TarLayoutWriter.NORMALIZED_MOD_TIME));
        assertThat(fileEntry.getName()).isEqualTo("/foo/bar.txt");
        assertThat(fileEntry.getMode()).isEqualTo(0777);
        assertThat(fileEntry.getLongUserId()).isEqualTo(1);
        assertThat(fileEntry.getLongGroupId()).isEqualTo(1);
        assertThat(fileEntry.getModTime()).isEqualTo(new Date(TarLayoutWriter.NORMALIZED_MOD_TIME));
        assertThat(fileContent).isEqualTo("test".getBytes(StandardCharsets.UTF_8));
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Aggregations

TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)132 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)99 File (java.io.File)52 IOException (java.io.IOException)50 FileInputStream (java.io.FileInputStream)46 GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)46 InputStream (java.io.InputStream)35 FileOutputStream (java.io.FileOutputStream)34 BufferedInputStream (java.io.BufferedInputStream)31 ByteArrayInputStream (java.io.ByteArrayInputStream)28 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 OutputStream (java.io.OutputStream)16 Path (java.nio.file.Path)16 BufferedOutputStream (java.io.BufferedOutputStream)12 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)12 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)8