Search in sources :

Example 66 with TarArchiveEntry

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

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry 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)

Example 68 with TarArchiveEntry

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

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

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry 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)

Aggregations

TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)213 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)102 File (java.io.File)91 FileInputStream (java.io.FileInputStream)59 IOException (java.io.IOException)59 FileOutputStream (java.io.FileOutputStream)46 GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)40 InputStream (java.io.InputStream)32 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)32 BufferedInputStream (java.io.BufferedInputStream)31 ByteArrayInputStream (java.io.ByteArrayInputStream)28 ByteArrayOutputStream (java.io.ByteArrayOutputStream)24 Test (org.junit.Test)24 Path (java.nio.file.Path)21 BufferedOutputStream (java.io.BufferedOutputStream)20 OutputStream (java.io.OutputStream)18 ArrayList (java.util.ArrayList)18 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)16 HashMap (java.util.HashMap)12 GZIPInputStream (java.util.zip.GZIPInputStream)12