Search in sources :

Example 1 with TarArchiveOutputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project pinot by linkedin.

the class TarGzCompressionUtils method createTarGzOfDirectory.

public static String createTarGzOfDirectory(String directoryPath, String tarGzPath, String entryPrefix) throws IOException {
    FileOutputStream fOut = null;
    BufferedOutputStream bOut = null;
    GzipCompressorOutputStream gzOut = null;
    TarArchiveOutputStream tOut = null;
    if (!tarGzPath.endsWith(TAR_GZ_FILE_EXTENTION)) {
        tarGzPath = tarGzPath + TAR_GZ_FILE_EXTENTION;
    }
    try {
        fOut = new FileOutputStream(new File(tarGzPath));
        bOut = new BufferedOutputStream(fOut);
        gzOut = new GzipCompressorOutputStream(bOut);
        tOut = new TarArchiveOutputStream(gzOut);
        tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        addFileToTarGz(tOut, directoryPath, entryPrefix);
    } catch (IOException e) {
        LOGGER.error("Failed to create tar.gz file for {} at path: {}", directoryPath, tarGzPath, e);
        Utils.rethrowException(e);
    } finally {
        if (tOut != null) {
            tOut.finish();
            tOut.close();
        }
        if (gzOut != null) {
            gzOut.close();
        }
        if (bOut != null) {
            bOut.close();
        }
        if (fOut != null) {
            fOut.close();
        }
    }
    return tarGzPath;
}
Also used : GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) FileOutputStream(java.io.FileOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) File(java.io.File)

Example 2 with TarArchiveOutputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project che by eclipse.

the class TarUtils method tarDir.

/**
     * Add content of directory {@code dir} to tar archive {@code tar}.
     *
     * @param parentPath
     *         parent path of tar archive. Typically if need add only content of {@code dir} this path should be absolute path to {@code
     *         dir} but if need to have in archive some parents this parameter may be used. For example if need add to archive content of
     *         directory '/a/b/c' but need to save directory 'c' in path:
     *         <pre>
     *                         {@code File dir = new File("a/b/c");
     *                      File tar = new File("archive.tar");
     *                      TarUtils.tarDir(dir.getParentFile().getAbsolutePath(), dir, tar, -1, IoUtil.ANY_FILTER);
     *                         }
     *                         </pre>
     *         In this case directory 'c' is added in tar archive.
     * @param dir
     *         dir to add
     * @param tar
     *         tar archive
     * @param modTime
     *         modification time that applied to all entries in archive instead modification time provided by method {@link
     *         File#lastModified()}. This parameter should be {@code -1} if don't need to set any specified time
     * @param filter
     *         optional filter for files to add in archive
     * @throws IOException
     *         if i/o error occurs
     * @throws IllegalArgumentException
     *         if {@code dir} is not directory or if {@code parentPath} is invalid, e.g. is neither parent nor equals to path of {@code
     *         dir}
     */
public static void tarDir(String parentPath, File dir, File tar, long modTime, FilenameFilter filter) throws IOException {
    if (!dir.isDirectory()) {
        throw new IllegalArgumentException("Not a directory.");
    }
    if (!dir.getAbsolutePath().startsWith(parentPath)) {
        throw new IllegalArgumentException("Invalid parent directory path " + parentPath);
    }
    if (filter == null) {
        filter = IoUtil.ANY_FILTER;
    }
    try (TarArchiveOutputStream tarOut = new TarArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(tar)))) {
        tarOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
        addDirectoryRecursively(tarOut, parentPath, dir, modTime, filter);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 3 with TarArchiveOutputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project che by eclipse.

the class TarUtils method tarFiles.

public static void tarFiles(File tar, long modTime, File... files) throws IOException {
    try (TarArchiveOutputStream tarOut = new TarArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(tar)))) {
        tarOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
        for (File f : files) {
            if (f.isDirectory()) {
                addDirectoryEntry(tarOut, f.getName(), f, modTime);
                final String parentPath = f.getParentFile().getAbsolutePath();
                addDirectoryRecursively(tarOut, parentPath, f, modTime, IoUtil.ANY_FILTER);
            } else if (f.isFile()) {
                addFileEntry(tarOut, f.getName(), f, modTime);
            }
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) File(java.io.File)

Example 4 with TarArchiveOutputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project che by eclipse.

the class TarArchiverTest method createTestTarArchive.

private byte[] createTestTarArchive() throws IOException {
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    TarArchiveOutputStream tarOut = new TarArchiveOutputStream(byteOut);
    addDirectoryEntry(tarOut, new TarArchiveEntry("arc/"));
    addDirectoryEntry(tarOut, new TarArchiveEntry("arc/a/"));
    addFileEntry(tarOut, "arc/a/_a.txt");
    addDirectoryEntry(tarOut, new TarArchiveEntry("arc/b/"));
    addFileEntry(tarOut, "arc/b/_b.txt");
    addDirectoryEntry(tarOut, new TarArchiveEntry("arc/c/"));
    addFileEntry(tarOut, "arc/c/_c.txt");
    tarOut.close();
    return byteOut.toByteArray();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 5 with TarArchiveOutputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project tdi-studio-se by Talend.

the class Zip method doTarGZip.

private void doTarGZip(Map<String, File> filesMap) throws Exception {
    File targetFile = new File(targetZip);
    targetFile.setLastModified(System.currentTimeMillis());
    FileOutputStream fos = new FileOutputStream(targetFile);
    final boolean syncFlush = this.syncFlush;
    final int compressLevel = this.compressLevel;
    TarArchiveOutputStream taos = new TarArchiveOutputStream(new GZIPOutputStream(fos, syncFlush) {

        {
            this.def.setLevel(compressLevel);
        }
    });
    taos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);
    taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    try {
        for (String relativeDir : filesMap.keySet()) {
            File file = filesMap.get(relativeDir);
            taos.putArchiveEntry(new TarArchiveEntry(file, relativeDir));
            FileInputStream is = new FileInputStream(file);
            try {
                IOUtils.copy(is, taos);
                taos.closeArchiveEntry();
            } finally {
                is.close();
            }
        }
    } finally {
        taos.close();
        fos.close();
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) ZipFile(net.lingala.zip4j.core.ZipFile) File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) FileInputStream(java.io.FileInputStream)

Aggregations

TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)68 File (java.io.File)27 FileOutputStream (java.io.FileOutputStream)27 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)26 GzipCompressorOutputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream)19 BufferedOutputStream (java.io.BufferedOutputStream)18 IOException (java.io.IOException)18 FileInputStream (java.io.FileInputStream)14 GZIPOutputStream (java.util.zip.GZIPOutputStream)14 Path (java.nio.file.Path)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 ZipArchiveOutputStream (org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream)9 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)8 OutputStream (java.io.OutputStream)7 InputStream (java.io.InputStream)6 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)6 ArchiveOutputStream (org.apache.commons.compress.archivers.ArchiveOutputStream)5 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Date (java.util.Date)4