Search in sources :

Example 36 with TarArchiveOutputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project jib by google.

the class TarStreamBuilder method writeEntriesAsTarArchive.

/**
 * Writes each entry in the filesystem to the tarball archive stream.
 */
private static void writeEntriesAsTarArchive(List<TarArchiveEntry> entries, OutputStream tarByteStream) throws IOException {
    try (TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream(tarByteStream)) {
        // Enables PAX extended headers to support long file names.
        tarArchiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
        for (TarArchiveEntry entry : entries) {
            tarArchiveOutputStream.putArchiveEntry(entry);
            if (entry.isFile()) {
                InputStream contentStream = new BufferedInputStream(new FileInputStream(entry.getFile()));
                ByteStreams.copy(contentStream, tarArchiveOutputStream);
            }
            tarArchiveOutputStream.closeArchiveEntry();
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) FileInputStream(java.io.FileInputStream)

Example 37 with TarArchiveOutputStream

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

the class Tar method doTar.

/**
 *  Crete tar.
 *  @param os output stream to write into.
 *  @param base base directory.
 *
 *  Only regular files and directories are supported.
 *  Files will be owner rw and optional execute bit.
 */
public static void doTar(OutputStream os, File base) throws SecurityException, IOException {
    if (!base.exists()) {
        throw new FileNotFoundException(String.format("File or directory %1$s not found", base));
    }
    try (TarArchiveOutputStream archive = new TarArchiveOutputStream(os)) {
        // TODO: use LONGFILE_POSIX in newer version of commons-compress
        archive.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        recurse(archive, base, "./");
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)

Example 38 with TarArchiveOutputStream

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

the class CompressedApplicationInputStreamTest method require_that_valid_tar_application_in_subdir_can_be_unpacked.

@Test
public void require_that_valid_tar_application_in_subdir_can_be_unpacked() throws IOException {
    File outFile = File.createTempFile("testapp", ".tar.gz");
    ArchiveOutputStream archiveOutputStream = new TarArchiveOutputStream(new GZIPOutputStream(new FileOutputStream(outFile)));
    File app = new File("src/test/resources/deploy/validapp");
    File file = new File(app, "services.xml");
    archiveOutputStream.putArchiveEntry(archiveOutputStream.createArchiveEntry(file, "application/" + file.getName()));
    ByteStreams.copy(new FileInputStream(file), archiveOutputStream);
    archiveOutputStream.closeArchiveEntry();
    file = new File(app, "hosts.xml");
    archiveOutputStream.putArchiveEntry(archiveOutputStream.createArchiveEntry(file, "application/" + file.getName()));
    ByteStreams.copy(new FileInputStream(file), archiveOutputStream);
    archiveOutputStream.closeArchiveEntry();
    file = new File(app, "deployment.xml");
    archiveOutputStream.putArchiveEntry(archiveOutputStream.createArchiveEntry(file, "application/" + file.getName()));
    ByteStreams.copy(new FileInputStream(file), archiveOutputStream);
    archiveOutputStream.closeArchiveEntry();
    archiveOutputStream.close();
    CompressedApplicationInputStream unpacked = CompressedApplicationInputStream.createFromCompressedStream(new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(outFile))));
    File outApp = unpacked.decompress();
    // gets the name of the subdir
    assertThat(outApp.getName(), is("application"));
    assertTestApp(outApp);
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) File(java.io.File) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 39 with TarArchiveOutputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project xodus by JetBrains.

the class CompressBackupUtil method tar.

/**
 * Compresses the content of source and stores newly created archive in dest.
 * In case source is a directory, it will be compressed recursively.
 *
 * @param source file or folder to be archived. Should exist on method call.
 * @param dest   path to the archive to be created. Should not exist on method call.
 * @throws IOException           in case of any issues with underlying store.
 * @throws FileNotFoundException in case source does not exist.
 */
public static void tar(@NotNull File source, @NotNull File dest) throws IOException {
    if (!source.exists()) {
        throw new IllegalArgumentException("No source file or folder exists: " + source.getAbsolutePath());
    }
    if (dest.exists()) {
        throw new IllegalArgumentException("Destination refers to existing file or folder: " + dest.getAbsolutePath());
    }
    TarArchiveOutputStream tarOut = null;
    try {
        tarOut = new TarArchiveOutputStream(new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(dest)), 0x1000));
        doTar("", source, tarOut);
        tarOut.close();
    } catch (IOException e) {
        // operation filed, let's remove the destination archive
        cleanUp(tarOut, dest);
        throw e;
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)

Example 40 with TarArchiveOutputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project elastest-torm by elastest.

the class FilesService method createTarFile.

public File createTarFile(String path, File... files) throws IOException {
    logger.info("Directory path to cormpress: {}", path);
    logger.info("Init tar compression.");
    try (TarArchiveOutputStream out = getTarArchiveOutputStream(path)) {
        for (File file : files) {
            addToArchiveCompression(out, file, ".");
        }
    }
    logger.info("Path of the tar file: {}", files[0].getAbsolutePath() + ".tar");
    return new File(files[0].getAbsolutePath() + ".tar");
}
Also used : TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) File(java.io.File)

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