Example usage for org.apache.commons.compress.archivers.tar TarArchiveOutputStream TarArchiveOutputStream

List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveOutputStream TarArchiveOutputStream

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.tar TarArchiveOutputStream TarArchiveOutputStream.

Prototype

public TarArchiveOutputStream(OutputStream os) 

Source Link

Document

Constructor for TarInputStream.

Usage

From source file:com.espringtran.compressor4j.processor.TarBz2Processor.java

/**
 * Compress data/*w  w  w . j  a  va  2s  .  c  o  m*/
 * 
 * @param fileCompressor
 *            FileCompressor object
 * @return
 * @throws Exception
 */
@Override
public byte[] compressData(FileCompressor fileCompressor) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BZip2CompressorOutputStream cos = new BZip2CompressorOutputStream(baos);
    TarArchiveOutputStream aos = new TarArchiveOutputStream(cos);
    try {
        for (BinaryFile binaryFile : fileCompressor.getMapBinaryFile().values()) {
            TarArchiveEntry entry = new TarArchiveEntry(binaryFile.getDesPath());
            entry.setSize(binaryFile.getActualSize());
            aos.putArchiveEntry(entry);
            aos.write(binaryFile.getData());
            aos.closeArchiveEntry();
        }
        aos.flush();
        aos.finish();
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on compress data", e);
    } finally {
        aos.close();
        cos.close();
        baos.close();
    }
    return baos.toByteArray();
}

From source file:com.twitter.heron.apiserver.utils.FileHelper.java

public static boolean createTarGz(File archive, File... files) {
    try (FileOutputStream fileOutputStream = new FileOutputStream(archive);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
            GzipCompressorOutputStream gzipOuputStream = new GzipCompressorOutputStream(bufferedOutputStream);
            TarArchiveOutputStream archiveOutputStream = new TarArchiveOutputStream(gzipOuputStream)) {
        for (File file : files) {
            addFileToArchive(archiveOutputStream, file, "");
        }//from w  ww. ja va  2  s  .  c om
        archiveOutputStream.finish();
    } catch (IOException ioe) {
        LOG.error("Failed to create archive {} file.", archive, ioe);
        return false;
    }
    return true;
}

From source file:br.com.thiaguten.archive.GzipArchive.java

@Override
protected ArchiveOutputStream createArchiveOutputStream(OutputStream outputStream) throws IOException {
    return new TarArchiveOutputStream(new GzipCompressorOutputStream(outputStream));
}

From source file:company.gonapps.loghut.utils.FileUtils.java

public static void archive(List<String> filePathStrings, OutputStream outputStream) throws IOException {
    try (TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream(
            new CloseShieldOutputStream(outputStream))) {

        for (String filePathString : filePathStrings)
            addFileToArchive(tarArchiveOutputStream, filePathString, "");

        tarArchiveOutputStream.finish();
    }/*  w ww  .  j  a  v  a 2  s . c om*/
}

From source file:com.linkedin.pinot.common.utils.TarGzCompressionUtils.java

/**
 * Creates a tar.gz file at the specified path with the contents of the
 * specified directory./* w  ww. ja v a  2s .  com*/
 *
 * @param directoryPath
 *          The path to the directory to create an archive of
 * @param tarGzPath
 *          The path to the archive to create
 * @return tarGzPath
 * @throws IOException
 *           If anything goes wrong
 */
public static String createTarGzOfDirectory(String directoryPath, String tarGzPath) 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, "");
    } finally {
        tOut.finish();

        tOut.close();
        gzOut.close();
        bOut.close();
        fOut.close();
    }
    return tarGzPath;
}

From source file:com.eleybourn.bookcatalogue.backup.tar.TarBackupWriter.java

/**
 * Constructor//w ww .j  av a  2 s  .  c o m
 * 
 * @param container      Parent
 * @throws IOException
 */
public TarBackupWriter(TarBackupContainer container) throws IOException {
    mContainer = container;
    // Open the archive for writing
    FileOutputStream out = new FileOutputStream(container.getFile());
    mOutput = new TarArchiveOutputStream(out);
}

From source file:com.codenvy.commons.lang.TarUtils.java

/**
 * Add content of directory {@code dir} to tar archive {@code tar}.
 *
 * @param parentPath//  w w w.j  a  va  2  s  .c om
 *         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);
    }
}

From source file:eu.eubrazilcc.lvl.core.io.FileCompressor.java

/**
 * Creates a tarball from the source directory and writes it into the target directory.
 * @param srcDir - directory whose files will be added to the tarball
 * @param targetName - directory where tarball will be written to
 * @throws IOException when an exception occurs on creating the tarball
 *//*from  ww w  .  ja  v a  2 s. co  m*/
public static void tarGzipDir(final String srcDir, final String targetName) throws IOException {

    FileOutputStream fileOutputStream = null;
    BufferedOutputStream bufferedOutputStream = null;
    GzipCompressorOutputStream gzipOutputStream = null;
    TarArchiveOutputStream tarArchiveOutputStream = null;

    try {
        forceMkdir(new File(getFullPath(targetName)));
        fileOutputStream = new FileOutputStream(new File(targetName));
        bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        gzipOutputStream = new GzipCompressorOutputStream(bufferedOutputStream);
        tarArchiveOutputStream = new TarArchiveOutputStream(gzipOutputStream);

        addFilesInDirectory(tarArchiveOutputStream, srcDir);
    } finally {
        if (tarArchiveOutputStream != null) {
            tarArchiveOutputStream.finish();
        }
        if (tarArchiveOutputStream != null) {
            tarArchiveOutputStream.close();
        }
        if (gzipOutputStream != null) {
            gzipOutputStream.close();
        }
        if (bufferedOutputStream != null) {
            bufferedOutputStream.close();
        }
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
    }
}

From source file:com.ttech.cordovabuild.infrastructure.archive.ArchiveUtils.java

public static void compressDirectory(Path path, OutputStream output) {
    try {//from  w  ww. ja  v a2  s . co  m
        // Wrap the output file stream in streams that will tar and gzip everything
        TarArchiveOutputStream taos = new TarArchiveOutputStream(new GZIPOutputStream(output));
        // TAR has an 8 gig file limit by default, this gets around that
        taos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR); // to get past the 8 gig limit
        // TAR originally didn't support long file names, so enable the support for it
        taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        for (File child : path.toFile().listFiles()) {
            addFileToTarGz(taos, child.toPath(), "");
        }
        taos.close();
    } catch (IOException e) {
        throw new ApplicationSourceException(e);
    }
}

From source file:com.puppetlabs.geppetto.forge.v2.api.it.ReleaseTestCreate.java

private byte[] getReleaseImage(ModuleName moduleName, Version version) throws IOException {
    ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
    TarArchiveOutputStream tarImage = null;
    try {/*from  www .  j  av a  2 s . co  m*/
        tarImage = new TarArchiveOutputStream(new GZIPOutputStream(bytesOut));
        putEntry(tarImage, moduleName, version, "metadata.json",
                createMetadata(moduleName, version).toString());
        putEntry(tarImage, moduleName, version, "manifests/init.pp", createInitPP(moduleName));
    } finally {
        if (tarImage != null)
            tarImage.close();
    }
    return bytesOut.toByteArray();
}