Search in sources :

Example 51 with TarArchiveInputStream

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

the class LayerTests method ofCreatesLayer.

@Test
void ofCreatesLayer() throws Exception {
    Layer layer = Layer.of((layout) -> {
        layout.directory("/directory", Owner.ROOT);
        layout.file("/directory/file", Owner.ROOT, Content.of("test"));
    });
    assertThat(layer.getId().toString()).isEqualTo("sha256:d03a34f73804698c875eb56ff694fc2fceccc69b645e4adceb004ed13588613b");
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    layer.writeTo(outputStream);
    try (TarArchiveInputStream tarStream = new TarArchiveInputStream(new ByteArrayInputStream(outputStream.toByteArray()))) {
        assertThat(tarStream.getNextTarEntry().getName()).isEqualTo("/directory/");
        assertThat(tarStream.getNextTarEntry().getName()).isEqualTo("/directory/file");
        assertThat(tarStream.getNextTarEntry()).isNull();
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 52 with TarArchiveInputStream

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

the class TarArchiveTests method ofWritesTarContent.

@Test
void ofWritesTarContent() throws Exception {
    Owner owner = Owner.of(123, 456);
    TarArchive tarArchive = TarArchive.of((content) -> {
        content.directory("/workspace", owner);
        content.directory("/layers", owner);
        content.directory("/cnb", Owner.ROOT);
        content.directory("/cnb/buildpacks", Owner.ROOT);
        content.directory("/platform", Owner.ROOT);
        content.directory("/platform/env", Owner.ROOT);
    });
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    tarArchive.writeTo(outputStream);
    try (TarArchiveInputStream tarStream = new TarArchiveInputStream(new ByteArrayInputStream(outputStream.toByteArray()))) {
        List<TarArchiveEntry> entries = new ArrayList<>();
        TarArchiveEntry entry = tarStream.getNextTarEntry();
        while (entry != null) {
            entries.add(entry);
            entry = tarStream.getNextTarEntry();
        }
        assertThat(entries).hasSize(6);
        assertThat(entries.get(0).getName()).isEqualTo("/workspace/");
        assertThat(entries.get(0).getLongUserId()).isEqualTo(123);
        assertThat(entries.get(0).getLongGroupId()).isEqualTo(456);
        assertThat(entries.get(2).getName()).isEqualTo("/cnb/");
        assertThat(entries.get(2).getLongUserId()).isEqualTo(0);
        assertThat(entries.get(2).getLongGroupId()).isEqualTo(0);
    }
}
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) Test(org.junit.jupiter.api.Test)

Example 53 with TarArchiveInputStream

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

the class ZipFileTarArchiveTests method writeToAdaptsContent.

@Test
void writeToAdaptsContent() throws Exception {
    Owner owner = Owner.of(123, 456);
    File file = new File(this.tempDir, "test.zip");
    writeTestZip(file);
    TarArchive tarArchive = TarArchive.fromZip(file, owner);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    tarArchive.writeTo(outputStream);
    try (TarArchiveInputStream tarStream = new TarArchiveInputStream(new ByteArrayInputStream(outputStream.toByteArray()))) {
        TarArchiveEntry dirEntry = tarStream.getNextTarEntry();
        assertThat(dirEntry.getName()).isEqualTo("spring/");
        assertThat(dirEntry.getLongUserId()).isEqualTo(123);
        assertThat(dirEntry.getLongGroupId()).isEqualTo(456);
        TarArchiveEntry fileEntry = tarStream.getNextTarEntry();
        assertThat(fileEntry.getName()).isEqualTo("spring/boot");
        assertThat(fileEntry.getLongUserId()).isEqualTo(123);
        assertThat(fileEntry.getLongGroupId()).isEqualTo(456);
        assertThat(fileEntry.getSize()).isEqualTo(4);
        assertThat(fileEntry.getMode()).isEqualTo(0755);
        assertThat(tarStream).hasContent("test");
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) Test(org.junit.jupiter.api.Test)

Example 54 with TarArchiveInputStream

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

the class BuildpacksTests method assertThatLayerContentIsCorrect.

private void assertThatLayerContentIsCorrect(Layer layer, String path) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    layer.writeTo(out);
    try (TarArchiveInputStream tar = new TarArchiveInputStream(new ByteArrayInputStream(out.toByteArray()))) {
        assertThat(tar.getNextEntry().getName()).isEqualTo("/cnb/buildpacks/" + path + "/buildpack.toml");
        assertThat(tar.getNextEntry()).isNull();
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 55 with TarArchiveInputStream

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

the class EphemeralBuilderTests method getLayer.

private TarArchiveInputStream getLayer(ImageArchive archive, int index) throws Exception {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    archive.writeTo(outputStream);
    TarArchiveInputStream tar = new TarArchiveInputStream(new ByteArrayInputStream(outputStream.toByteArray()));
    for (int i = 0; i <= index; i++) {
        tar.getNextEntry();
    }
    return new TarArchiveInputStream(tar);
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

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