Search in sources :

Example 11 with GzipCompressorInputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project docker-client by spotify.

the class CompressedDirectoryTest method testFileWithIgnore.

@Test
public void testFileWithIgnore() throws Exception {
    // note: Paths.get(someURL.toUri()) is the platform-neutral way to convert a URL to a Path
    final URL dockerDirectory = Resources.getResource("dockerDirectoryWithIgnore");
    try (CompressedDirectory dir = CompressedDirectory.create(Paths.get(dockerDirectory.toURI()));
        BufferedInputStream fileIn = new BufferedInputStream(Files.newInputStream(dir.file()));
        GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(fileIn);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn)) {
        final List<String> names = new ArrayList<>();
        TarArchiveEntry entry;
        while ((entry = tarIn.getNextTarEntry()) != null) {
            final String name = entry.getName();
            names.add(name);
        }
        assertThat(names, containsInAnyOrder("Dockerfile", "bin/", "bin/date.sh", "subdir2/", "subdir2/keep.me", "subdir2/do-not.ignore", "subdir3/do.keep", ".dockerignore"));
    }
}
Also used : GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) BufferedInputStream(java.io.BufferedInputStream) ArrayList(java.util.ArrayList) URL(java.net.URL) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) Test(org.junit.Test)

Example 12 with GzipCompressorInputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project docker-client by spotify.

the class CompressedDirectoryTest method testFileWithEmptyDirectory.

@Test
public void testFileWithEmptyDirectory() throws Exception {
    Path tempDir = Files.createTempDirectory("dockerDirectoryEmptySubdirectory");
    tempDir.toFile().deleteOnExit();
    assertThat(new File(tempDir.toFile(), "emptySubDir").mkdir(), is(true));
    try (CompressedDirectory dir = CompressedDirectory.create(tempDir);
        BufferedInputStream fileIn = new BufferedInputStream(Files.newInputStream(dir.file()));
        GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(fileIn);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn)) {
        final List<String> names = new ArrayList<>();
        TarArchiveEntry entry;
        while ((entry = tarIn.getNextTarEntry()) != null) {
            final String name = entry.getName();
            names.add(name);
        }
        assertThat(names, contains("emptySubDir/"));
    }
}
Also used : Path(java.nio.file.Path) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) BufferedInputStream(java.io.BufferedInputStream) ArrayList(java.util.ArrayList) File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) Test(org.junit.Test)

Example 13 with GzipCompressorInputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project docker-client by spotify.

the class CompressedDirectoryTest method testFile.

@Test
public void testFile() throws Exception {
    // note: Paths.get(someURL.toUri()) is the platform-neutral way to convert a URL to a Path
    final URL dockerDirectory = Resources.getResource("dockerDirectory");
    try (CompressedDirectory dir = CompressedDirectory.create(Paths.get(dockerDirectory.toURI()));
        BufferedInputStream fileIn = new BufferedInputStream(Files.newInputStream(dir.file()));
        GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(fileIn);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn)) {
        final List<String> names = new ArrayList<>();
        TarArchiveEntry entry;
        while ((entry = tarIn.getNextTarEntry()) != null) {
            final String name = entry.getName();
            names.add(name);
        }
        assertThat(names, containsInAnyOrder("Dockerfile", "bin/", "bin/date.sh", "innerDir/", "innerDir/innerDockerfile"));
    }
}
Also used : GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) BufferedInputStream(java.io.BufferedInputStream) ArrayList(java.util.ArrayList) URL(java.net.URL) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) Test(org.junit.Test)

Example 14 with GzipCompressorInputStream

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

the class TarGzUtils method extract.

public static void extract(File tarFile, File destFile) throws IOException {
    logger.info("Extracting file: " + tarFile.getName());
    if (destFile.exists())
        throw new FileAlreadyExistsException(destFile.getAbsolutePath());
    if (!destFile.mkdir())
        throw new IOException("Can't make directory for " + destFile.getAbsolutePath());
    TarArchiveInputStream tarIn = new TarArchiveInputStream(new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tarFile))));
    TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
    while (tarEntry != null) {
        // create a file with the same name as the tarEntry
        File destPath = new File(destFile, tarEntry.getName());
        if (tarEntry.isDirectory()) {
            if (!destPath.mkdirs())
                logger.info("Can't make directory for " + destPath.getAbsolutePath());
        } else {
            if (!destPath.createNewFile())
                logger.info("Name of file already exists " + destPath.getAbsolutePath());
            BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath));
            byte[] btoRead = new byte[1024];
            int len;
            while ((len = tarIn.read(btoRead)) != -1) {
                bout.write(btoRead, 0, len);
            }
            bout.close();
        }
        tarEntry = tarIn.getNextTarEntry();
    }
    tarIn.close();
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 15 with GzipCompressorInputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream 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)

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