Search in sources :

Example 71 with TarArchiveEntry

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

the class ImageBuildpackTests method writeTarEntry.

private void writeTarEntry(TarArchiveOutputStream tarOut, String name) throws IOException {
    TarArchiveEntry entry = new TarArchiveEntry(name);
    tarOut.putArchiveEntry(entry);
    tarOut.closeArchiveEntry();
}
Also used : TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 72 with TarArchiveEntry

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

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

the class TestTarGzip method writeEntry.

private void writeEntry(TarArchiveOutputStream tar, String entryName, String content) throws IOException {
    TarArchiveEntry entry = new TarArchiveEntry(entryName);
    entry.setSize(content.length());
    tar.putArchiveEntry(entry);
    IOUtils.copy(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)), tar);
    tar.closeArchiveEntry();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 74 with TarArchiveEntry

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

Example 75 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project zeppelin by apache.

the class HeliumBundleFactory method unTgz.

private static List<String> unTgz(File tarFile, File directory) throws IOException {
    List<String> result = new ArrayList<>();
    try (TarArchiveInputStream in = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(tarFile)))) {
        TarArchiveEntry entry = in.getNextTarEntry();
        while (entry != null) {
            if (entry.isDirectory()) {
                entry = in.getNextTarEntry();
                continue;
            }
            File curfile = new File(directory, entry.getName());
            File parent = curfile.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }
            try (OutputStream out = new FileOutputStream(curfile)) {
                IOUtils.copy(in, out);
            }
            result.add(entry.getName());
            entry = in.getNextTarEntry();
        }
    }
    return result;
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) File(java.io.File) FileInputStream(java.io.FileInputStream) 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