Search in sources :

Example 56 with TarArchiveOutputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project flink by splunk.

the class YarnTestArchiveJob method archiveFilesInDirectory.

private static void archiveFilesInDirectory(File directory, String target) throws IOException {
    for (Map.Entry<String, String> entry : srcFiles.entrySet()) {
        Files.write(Paths.get(directory.getAbsolutePath() + File.separator + entry.getKey()), entry.getValue().getBytes());
    }
    try (FileOutputStream fos = new FileOutputStream(target);
        GzipCompressorOutputStream gos = new GzipCompressorOutputStream(new BufferedOutputStream(fos));
        TarArchiveOutputStream taros = new TarArchiveOutputStream(gos)) {
        taros.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        for (File f : directory.listFiles()) {
            taros.putArchiveEntry(new TarArchiveEntry(f, directory.getName() + File.separator + f.getName()));
            try (FileInputStream fis = new FileInputStream(f);
                BufferedInputStream bis = new BufferedInputStream(fis)) {
                IOUtils.copy(bis, taros);
                taros.closeArchiveEntry();
            }
        }
    }
    for (Map.Entry<String, String> entry : srcFiles.entrySet()) {
        Files.delete(Paths.get(directory.getAbsolutePath() + File.separator + entry.getKey()));
    }
}
Also used : GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) HashMap(java.util.HashMap) Map(java.util.Map) BufferedOutputStream(java.io.BufferedOutputStream) File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) FileInputStream(java.io.FileInputStream)

Example 57 with TarArchiveOutputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project vespa by vespa-engine.

the class CompressedFileReference method compress.

public static byte[] compress(File baseDir, List<File> inputFiles) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    TarArchiveOutputStream archiveOutputStream = new TarArchiveOutputStream(new GZIPOutputStream(out));
    archiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
    createArchiveFile(archiveOutputStream, baseDir, inputFiles);
    return out.toByteArray();
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)

Example 58 with TarArchiveOutputStream

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

the class BootBuildImageIntegrationTests method tarGzipBuildpackContent.

private void tarGzipBuildpackContent() throws IOException {
    Path tarGzipPath = Paths.get(this.gradleBuild.getProjectDir().getAbsolutePath(), "hello-world.tgz");
    try (TarArchiveOutputStream tar = new TarArchiveOutputStream(new GzipCompressorOutputStream(Files.newOutputStream(Files.createFile(tarGzipPath))))) {
        File buildpackDir = new File(this.gradleBuild.getProjectDir(), "buildpack/hello-world");
        writeDirectoryToTar(tar, buildpackDir, buildpackDir.getAbsolutePath());
    }
}
Also used : Path(java.nio.file.Path) GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) File(java.io.File)

Example 59 with TarArchiveOutputStream

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

the class ImageBuildpackTests method withMockLayers.

private Object withMockLayers(InvocationOnMock invocation) {
    try {
        IOBiConsumer<String, TarArchive> consumer = invocation.getArgument(1);
        TarArchive archive = (out) -> {
            try (TarArchiveOutputStream tarOut = new TarArchiveOutputStream(out)) {
                tarOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
                writeTarEntry(tarOut, "/cnb/");
                writeTarEntry(tarOut, "/cnb/buildpacks/");
                writeTarEntry(tarOut, "/cnb/buildpacks/example_buildpack/");
                writeTarEntry(tarOut, "/cnb/buildpacks/example_buildpack/0.0.1/");
                writeTarEntry(tarOut, "/cnb/buildpacks/example_buildpack/0.0.1/buildpack.toml");
                writeTarEntry(tarOut, "/cnb/buildpacks/example_buildpack/0.0.1/" + this.longFilePath);
                tarOut.finish();
            }
        };
        consumer.accept("test", archive);
    } catch (IOException ex) {
        fail("Error writing mock layers", ex);
    }
    return null;
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ImageReference(org.springframework.boot.buildpack.platform.docker.type.ImageReference) Random(java.util.Random) ArrayList(java.util.ArrayList) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ByteArrayInputStream(java.io.ByteArrayInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) BDDMockito.given(org.mockito.BDDMockito.given) TarArchive(org.springframework.boot.buildpack.platform.io.TarArchive) Image(org.springframework.boot.buildpack.platform.docker.type.Image) Assertions.tuple(org.assertj.core.api.Assertions.tuple) IOException(java.io.IOException) IOBiConsumer(org.springframework.boot.buildpack.platform.io.IOBiConsumer) BDDMockito.willAnswer(org.mockito.BDDMockito.willAnswer) Test(org.junit.jupiter.api.Test) List(java.util.List) Assertions.fail(org.assertj.core.api.Assertions.fail) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) AbstractJsonTests(org.springframework.boot.buildpack.platform.json.AbstractJsonTests) Mockito.mock(org.mockito.Mockito.mock) TarArchive(org.springframework.boot.buildpack.platform.io.TarArchive) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) IOException(java.io.IOException)

Example 60 with TarArchiveOutputStream

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

the class TarGzipBuildpack method copyAndRebaseEntries.

private void copyAndRebaseEntries(OutputStream outputStream) throws IOException {
    String id = this.coordinates.getSanitizedId();
    Path basePath = Paths.get("/cnb/buildpacks/", id, this.coordinates.getVersion());
    try (TarArchiveInputStream tar = new TarArchiveInputStream(new GzipCompressorInputStream(Files.newInputStream(this.path)));
        TarArchiveOutputStream output = new TarArchiveOutputStream(outputStream)) {
        writeBasePathEntries(output, basePath);
        TarArchiveEntry entry = tar.getNextTarEntry();
        while (entry != null) {
            entry.setName(basePath + "/" + entry.getName());
            output.putArchiveEntry(entry);
            StreamUtils.copy(tar, output);
            output.closeArchiveEntry();
            entry = tar.getNextTarEntry();
        }
        output.finish();
    }
}
Also used : Path(java.nio.file.Path) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Aggregations

TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)101 File (java.io.File)42 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)42 FileOutputStream (java.io.FileOutputStream)37 GzipCompressorOutputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream)30 BufferedOutputStream (java.io.BufferedOutputStream)22 IOException (java.io.IOException)21 ByteArrayOutputStream (java.io.ByteArrayOutputStream)20 FileInputStream (java.io.FileInputStream)19 Path (java.nio.file.Path)17 GZIPOutputStream (java.util.zip.GZIPOutputStream)17 ByteArrayInputStream (java.io.ByteArrayInputStream)15 OutputStream (java.io.OutputStream)13 Test (org.junit.Test)12 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)11 ArchiveOutputStream (org.apache.commons.compress.archivers.ArchiveOutputStream)11 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)9 ZipArchiveOutputStream (org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream)9 ImageArchiveManifest (io.fabric8.maven.docker.model.ImageArchiveManifest)8 InputStream (java.io.InputStream)7