Search in sources :

Example 56 with TarArchiveInputStream

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

the class DirectoryBuildpackTests method assertHasExpectedLayers.

private void assertHasExpectedLayers(Buildpack buildpack) throws IOException {
    List<ByteArrayOutputStream> layers = new ArrayList<>();
    buildpack.apply((layer) -> {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        layer.writeTo(out);
        layers.add(out);
    });
    assertThat(layers).hasSize(1);
    byte[] content = layers.get(0).toByteArray();
    try (TarArchiveInputStream tar = new TarArchiveInputStream(new ByteArrayInputStream(content))) {
        List<TarArchiveEntry> entries = new ArrayList<>();
        TarArchiveEntry entry = tar.getNextTarEntry();
        while (entry != null) {
            entries.add(entry);
            entry = tar.getNextTarEntry();
        }
        assertThat(entries).extracting("name", "mode").containsExactlyInAnyOrder(tuple("/cnb/", 0755), tuple("/cnb/buildpacks/", 0755), tuple("/cnb/buildpacks/example_buildpack1/", 0755), tuple("/cnb/buildpacks/example_buildpack1/0.0.1/", 0755), tuple("/cnb/buildpacks/example_buildpack1/0.0.1/buildpack.toml", 0644), tuple("/cnb/buildpacks/example_buildpack1/0.0.1/bin/", 0755), tuple("/cnb/buildpacks/example_buildpack1/0.0.1/bin/detect", 0744), tuple("/cnb/buildpacks/example_buildpack1/0.0.1/bin/build", 0744));
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 57 with TarArchiveInputStream

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

the class ImageBuildpackTests method assertHasExpectedLayers.

private void assertHasExpectedLayers(Buildpack buildpack) throws IOException {
    List<ByteArrayOutputStream> layers = new ArrayList<>();
    buildpack.apply((layer) -> {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        layer.writeTo(out);
        layers.add(out);
    });
    assertThat(layers).hasSize(1);
    byte[] content = layers.get(0).toByteArray();
    List<TarArchiveEntry> entries = new ArrayList<>();
    try (TarArchiveInputStream tar = new TarArchiveInputStream(new ByteArrayInputStream(content))) {
        TarArchiveEntry entry = tar.getNextTarEntry();
        while (entry != null) {
            entries.add(entry);
            entry = tar.getNextTarEntry();
        }
    }
    assertThat(entries).extracting("name", "mode").containsExactlyInAnyOrder(tuple("cnb/", TarArchiveEntry.DEFAULT_DIR_MODE), tuple("cnb/buildpacks/", TarArchiveEntry.DEFAULT_DIR_MODE), tuple("cnb/buildpacks/example_buildpack/", TarArchiveEntry.DEFAULT_DIR_MODE), tuple("cnb/buildpacks/example_buildpack/0.0.1/", TarArchiveEntry.DEFAULT_DIR_MODE), tuple("cnb/buildpacks/example_buildpack/0.0.1/buildpack.toml", TarArchiveEntry.DEFAULT_FILE_MODE), tuple("cnb/buildpacks/example_buildpack/0.0.1/" + this.longFilePath, TarArchiveEntry.DEFAULT_FILE_MODE));
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 58 with TarArchiveInputStream

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

the class TestTarGzip method assertHasExpectedLayers.

void assertHasExpectedLayers(Buildpack buildpack) throws IOException {
    List<ByteArrayOutputStream> layers = new ArrayList<>();
    buildpack.apply((layer) -> {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        layer.writeTo(out);
        layers.add(out);
    });
    assertThat(layers).hasSize(1);
    byte[] content = layers.get(0).toByteArray();
    try (TarArchiveInputStream tar = new TarArchiveInputStream(new ByteArrayInputStream(content))) {
        assertThat(tar.getNextEntry().getName()).isEqualTo("cnb/");
        assertThat(tar.getNextEntry().getName()).isEqualTo("cnb/buildpacks/");
        assertThat(tar.getNextEntry().getName()).isEqualTo("cnb/buildpacks/example_buildpack1/");
        assertThat(tar.getNextEntry().getName()).isEqualTo("cnb/buildpacks/example_buildpack1/0.0.1/");
        assertThat(tar.getNextEntry().getName()).isEqualTo("cnb/buildpacks/example_buildpack1/0.0.1/buildpack.toml");
        assertThat(tar.getNextEntry().getName()).isEqualTo("cnb/buildpacks/example_buildpack1/0.0.1/bin/");
        assertThat(tar.getNextEntry().getName()).isEqualTo("cnb/buildpacks/example_buildpack1/0.0.1/bin/detect");
        assertThat(tar.getNextEntry().getName()).isEqualTo("cnb/buildpacks/example_buildpack1/0.0.1/bin/build");
        assertThat(tar.getNextEntry()).isNull();
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 59 with TarArchiveInputStream

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

the class BuildRequestTests method hasExpectedJarContent.

private void hasExpectedJarContent(TarArchive archive) {
    try {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        archive.writeTo(outputStream);
        try (TarArchiveInputStream tar = new TarArchiveInputStream(new ByteArrayInputStream(outputStream.toByteArray()))) {
            assertThat(tar.getNextEntry().getName()).isEqualTo("spring/");
            assertThat(tar.getNextEntry().getName()).isEqualTo("spring/boot");
            assertThat(tar.getNextEntry()).isNull();
        }
    } catch (IOException ex) {
        throw new IllegalStateException(ex);
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 60 with TarArchiveInputStream

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

the class SymbolicLinkPreservingUntarTransform method unpack.

public void unpack(File tarFile, File targetDir) throws IOException {
    Logging.getLogger(SymbolicLinkPreservingUntarTransform.class).info("Unpacking " + tarFile.getName() + " using " + SymbolicLinkPreservingUntarTransform.class.getSimpleName() + ".");
    TarArchiveInputStream tar = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(tarFile)));
    final Path destinationPath = targetDir.toPath();
    TarArchiveEntry entry = tar.getNextTarEntry();
    while (entry != null) {
        final Path relativePath = UnpackTransform.trimArchiveExtractPath(entry.getName());
        if (relativePath == null) {
            entry = tar.getNextTarEntry();
            continue;
        }
        final Path destination = destinationPath.resolve(relativePath);
        final Path parent = destination.getParent();
        if (Files.exists(parent) == false) {
            Files.createDirectories(parent);
        }
        if (entry.isDirectory()) {
            Files.createDirectory(destination);
        } else if (entry.isSymbolicLink()) {
            Files.createSymbolicLink(destination, Paths.get(entry.getLinkName()));
        } else {
            // copy the file from the archive using a small buffer to avoid heaping
            Files.createFile(destination);
            try (FileOutputStream fos = new FileOutputStream(destination.toFile())) {
                tar.transferTo(fos);
            }
        }
        if (entry.isSymbolicLink() == false) {
            // check if the underlying file system supports POSIX permissions
            final PosixFileAttributeView view = Files.getFileAttributeView(destination, PosixFileAttributeView.class);
            if (view != null) {
                final Set<PosixFilePermission> permissions = PosixFilePermissions.fromString(permissions((entry.getMode() >> 6) & 07) + permissions((entry.getMode() >> 3) & 07) + permissions((entry.getMode() >> 0) & 07));
                Files.setPosixFilePermissions(destination, permissions);
            }
        }
        entry = tar.getNextTarEntry();
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) Path(java.nio.file.Path) FileOutputStream(java.io.FileOutputStream) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) FileInputStream(java.io.FileInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

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