Search in sources :

Example 16 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project twister2 by DSC-SPIDAL.

the class TarGzipPacker method addTarGzipToArchive.

/**
 * given tar.gz file will be copied to this tar.gz file.
 * all files will be transferred to new tar.gz file one by one.
 * original directory structure will be kept intact
 *
 * @param tarGzipFile the archive file to be copied to the new archive
 */
public boolean addTarGzipToArchive(String tarGzipFile) {
    try {
        // construct input stream
        InputStream fin = Files.newInputStream(Paths.get(tarGzipFile));
        BufferedInputStream in = new BufferedInputStream(fin);
        GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
        TarArchiveInputStream tarInputStream = new TarArchiveInputStream(gzIn);
        // copy the existing entries from source gzip file
        ArchiveEntry nextEntry;
        while ((nextEntry = tarInputStream.getNextEntry()) != null) {
            tarOutputStream.putArchiveEntry(nextEntry);
            IOUtils.copy(tarInputStream, tarOutputStream);
            tarOutputStream.closeArchiveEntry();
        }
        tarInputStream.close();
        return true;
    } catch (IOException ioe) {
        LOG.log(Level.SEVERE, "Archive File can not be added: " + tarGzipFile, ioe);
        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) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) InputStream(java.io.InputStream) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) IOException(java.io.IOException)

Example 17 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream 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 18 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream 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 19 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream 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 20 with TarArchiveInputStream

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

the class DefaultDockerClientTest method integrationTest.

@Test
@SuppressWarnings("deprecation")
public void integrationTest() throws Exception {
    // Pull image
    sut.pull(BUSYBOX_LATEST);
    // Create container
    final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).cmd("sh", "-c", "while :; do sleep 1; done").build();
    final String name = randomName();
    final ContainerCreation creation = sut.createContainer(config, name);
    final String id = creation.id();
    assertThat(creation.warnings(), anyOf(is(empty()), is(nullValue())));
    assertThat(id, is(any(String.class)));
    // Inspect using container ID
    {
        final ContainerInfo info = sut.inspectContainer(id);
        assertThat(info.id(), equalTo(id));
        assertThat(info.config().image(), equalTo(config.image()));
        assertThat(info.config().cmd(), equalTo(config.cmd()));
    }
    // Inspect using container name
    {
        final ContainerInfo info = sut.inspectContainer(name);
        assertThat(info.config().image(), equalTo(config.image()));
        assertThat(info.config().cmd(), equalTo(config.cmd()));
    }
    // Start container
    sut.startContainer(id);
    final Path dockerDirectory = getResource("dockerSslDirectory");
    // to a directory in a container
    if (dockerApiVersionAtLeast("1.20")) {
        try {
            sut.copyToContainer(dockerDirectory, id, "/tmp");
        } catch (Exception e) {
            fail("error copying files to container");
        }
        // Copy the same files from container
        final ImmutableSet.Builder<String> filesDownloaded = ImmutableSet.builder();
        try (TarArchiveInputStream tarStream = new TarArchiveInputStream(dockerApiVersionLessThan("1.24") ? sut.copyContainer(id, "/tmp") : sut.archiveContainer(id, "/tmp"))) {
            TarArchiveEntry entry;
            while ((entry = tarStream.getNextTarEntry()) != null) {
                filesDownloaded.add(entry.getName());
            }
        }
        // Check that we got back what we put in
        final File folder = new File(dockerDirectory.toString());
        final File[] files = folder.listFiles();
        if (files != null) {
            for (final File file : files) {
                if (!file.isDirectory()) {
                    Boolean found = false;
                    for (final String fileDownloaded : filesDownloaded.build()) {
                        if (fileDownloaded.contains(file.getName())) {
                            found = true;
                        }
                    }
                    assertTrue(found);
                }
            }
        }
    }
    // Kill container
    sut.killContainer(id);
    try {
        // Remove the container
        sut.removeContainer(id);
    } catch (DockerRequestException e) {
        // CircleCI doesn't let you remove a container :(
        if (!CIRCLECI) {
            // Verify that the container is gone
            exception.expect(ContainerNotFoundException.class);
            sut.inspectContainer(id);
        }
    }
}
Also used : Path(java.nio.file.Path) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) DockerException(com.spotify.docker.client.exceptions.DockerException) DockerTimeoutException(com.spotify.docker.client.exceptions.DockerTimeoutException) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) URISyntaxException(java.net.URISyntaxException) ImagePushFailedException(com.spotify.docker.client.exceptions.ImagePushFailedException) NetworkNotFoundException(com.spotify.docker.client.exceptions.NetworkNotFoundException) BadParamException(com.spotify.docker.client.exceptions.BadParamException) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) NotFoundException(com.spotify.docker.client.exceptions.NotFoundException) ContainerRenameConflictException(com.spotify.docker.client.exceptions.ContainerRenameConflictException) ConflictException(com.spotify.docker.client.exceptions.ConflictException) UnsupportedApiVersionException(com.spotify.docker.client.exceptions.UnsupportedApiVersionException) VolumeNotFoundException(com.spotify.docker.client.exceptions.VolumeNotFoundException) ExpectedException(org.junit.rules.ExpectedException) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) 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) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SecretFile(com.spotify.docker.client.messages.swarm.SecretFile) File(java.io.File) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) 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