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

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

Introduction

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

Prototype

public void finish() throws IOException 

Source Link

Document

Ends the TAR archive without closing the underlying OutputStream.

Usage

From source file:fr.insalyon.creatis.vip.applicationimporter.server.business.TargzUtils.java

public static void createTargz(List<File> pathIn, String pathOut) throws BusinessException {
    try {/*from   w  ww  .ja  v a2 s. c  om*/

        FileOutputStream fos = new FileOutputStream(pathOut);
        TarArchiveOutputStream tos = new TarArchiveOutputStream(
                new GZIPOutputStream(new BufferedOutputStream(fos)));
        for (File entry : pathIn) {
            addFileToTarGz(tos, entry, null);
        }
        tos.finish();
        tos.close();
    } catch (IOException ex) {
        throw new BusinessException(ex);
    }
}

From source file:com.ipcglobal.fredimport.util.FredUtils.java

/**
 * Creates a tar.gz file at the specified path with the contents of the specified directory.
 *
 * @param directoryPath the directory path
 * @param tarGzPath the tar gz path/*from w  ww.j  av a 2s  . c o m*/
 * @throws IOException             If anything goes wrong
 */
public static void createTarGzOfDirectory(String directoryPath, String tarGzPath) throws IOException {
    FileOutputStream fOut = null;
    BufferedOutputStream bOut = null;
    GzipCompressorOutputStream gzOut = null;
    TarArchiveOutputStream tOut = null;
    try {
        fOut = new FileOutputStream(new File(tarGzPath));
        bOut = new BufferedOutputStream(fOut);
        gzOut = new GzipCompressorOutputStream(bOut);
        tOut = new TarArchiveOutputStream(gzOut);
        addFileToTarGz(tOut, directoryPath, "/");
    } finally {
        tOut.finish();
        tOut.close();
        gzOut.close();
        bOut.close();
        fOut.close();
    }
}

From source file:com.linkedin.thirdeye.bootstrap.util.TarGzCompressionUtils.java

/**
 * Creates a tar.gz file at the specified path with the contents of the
 * specified directory./*from ww  w.  j av  a2  s  .  co  m*/
 *
 * @param dirPath
 *          The path to the directory to create an archive of
 * @param archivePath
 *          The path to the archive to create
 *
 * @throws IOException
 *           If anything goes wrong
 */
public static void createTarGzOfDirectory(String directoryPath, String tarGzPath) throws IOException {
    FileOutputStream fOut = null;
    BufferedOutputStream bOut = null;
    GzipCompressorOutputStream gzOut = null;
    TarArchiveOutputStream tOut = null;

    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();
    }
}

From source file:com.flurry.proguard.UploadProGuardMapping.java

/**
 * Create a gzipped tar archive containing the ProGuard mapping file
 *
 * @param file the mapping.txt file/*from   w w w. ja va 2  s.  co m*/
 * @param uuid the build uuid
 * @return the tar-gzipped archive
 */
private static File createArchive(File file, String uuid) {
    try {
        File tarZippedFile = File.createTempFile("tar-zipped-file", ".tgz");
        TarArchiveOutputStream taos = new TarArchiveOutputStream(
                new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(tarZippedFile))));
        taos.putArchiveEntry(new TarArchiveEntry(file, uuid + ".txt"));
        IOUtils.copy(new FileInputStream(file), taos);
        taos.closeArchiveEntry();
        taos.finish();
        taos.close();
        return tarZippedFile;
    } catch (IOException e) {
        failWithError("IO Exception while trying to tar and zip the file.", e);
        return null;
    }
}

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./*from  w w  w.j av a  2 s. c  om*/
 *
 * @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:frameworks.Masken.java

public static void CreateTarGZ(String dirPath, String tarGzPath)

{

    FileOutputStream fOut = null;
    try {/*from w w  w. j  a  va 2  s.c  o  m*/
        System.out.println(new File(".").getAbsolutePath());
        //   dirPath = "parent/childDirToCompress/";
        //   tarGzPath = "archive.tar.gz";
        fOut = new FileOutputStream(new File(tarGzPath));
        BufferedOutputStream bOut = new BufferedOutputStream(fOut);
        GzipCompressorOutputStream gzOut = new GzipCompressorOutputStream(bOut);
        TarArchiveOutputStream tOut = new TarArchiveOutputStream(gzOut);
        addFileToTarGz(tOut, dirPath, "");
        tOut.finish();
        tOut.close();
        gzOut.close();
        bOut.close();
        fOut.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Masken.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Masken.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            fOut.close();
        } catch (IOException ex) {
            Logger.getLogger(Masken.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

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
 *//*  ww  w .j  av a 2  s .com*/
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.flurry.proguard.UploadMapping.java

/**
 * Create a gzipped tar archive containing the ProGuard/Native mapping files
 *
 * @param files array of mapping.txt files
 * @return the tar-gzipped archive/* w w w  .j a  va  2  s . c  o m*/
 */
private static File createArchive(List<File> files, String uuid) {
    try {
        File tarZippedFile = File.createTempFile("tar-zipped-file", ".tgz");
        TarArchiveOutputStream taos = new TarArchiveOutputStream(
                new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(tarZippedFile))));
        for (File file : files) {
            taos.putArchiveEntry(new TarArchiveEntry(file,
                    (uuid != null && !uuid.isEmpty() ? uuid : UUID.randomUUID()) + ".txt"));
            IOUtils.copy(new FileInputStream(file), taos);
            taos.closeArchiveEntry();
        }
        taos.finish();
        taos.close();
        return tarZippedFile;
    } catch (IOException e) {
        failWithError("IO Exception while trying to tar and zip the file.", e);
        return null;
    }
}

From source file:ezbake.deployer.utilities.ArtifactHelpers.java

/**
 * Append to the given ArchiveInputStream writing to the given outputstream, the given entries to add.
 * This will duplicate the InputStream to the Output.
 *
 * @param inputStream - archive input to append to
 * @param output      - what to copy the modified archive to
 * @param filesToAdd  - what entries to append.
 *//*from   w  w  w. j  a  v a  2s  .com*/
private static void appendFilesInTarArchive(ArchiveInputStream inputStream, OutputStream output,
        Iterable<ArtifactDataEntry> filesToAdd) throws DeploymentException {
    ArchiveStreamFactory asf = new ArchiveStreamFactory();

    try {
        HashMap<String, ArtifactDataEntry> newFiles = new HashMap<>();
        for (ArtifactDataEntry entry : filesToAdd) {
            newFiles.put(entry.getEntry().getName(), entry);
        }
        GZIPOutputStream gzs = new GZIPOutputStream(output);
        TarArchiveOutputStream aos = (TarArchiveOutputStream) asf
                .createArchiveOutputStream(ArchiveStreamFactory.TAR, gzs);
        aos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        // copy the existing entries
        ArchiveEntry nextEntry;
        while ((nextEntry = inputStream.getNextEntry()) != null) {
            //If we're passing in the same file, don't copy into the new archive
            if (!newFiles.containsKey(nextEntry.getName())) {
                aos.putArchiveEntry(nextEntry);
                IOUtils.copy(inputStream, aos);
                aos.closeArchiveEntry();
            }
        }

        for (ArtifactDataEntry entry : filesToAdd) {
            aos.putArchiveEntry(entry.getEntry());
            IOUtils.write(entry.getData(), aos);
            aos.closeArchiveEntry();
        }
        aos.finish();
        gzs.finish();
    } catch (ArchiveException | IOException e) {
        log.error(e.getMessage(), e);
        throw new DeploymentException(e.getMessage());
    }
}

From source file:com.openshift.client.utils.TarFileTestUtils.java

/**
 * Replaces the given file(-name), that might exist anywhere nested in the
 * given archive, by a new entry with the given content. The replacement is
 * faked by adding a new entry into the archive which will overwrite the
 * existing (older one) on extraction./*from   w  w  w  .  jav  a2 s  . c  o  m*/
 * 
 * @param name
 *            the name of the file to replace (no path required)
 * @param newContent
 *            the content of the replacement file
 * @param in
 * @return
 * @throws IOException
 * @throws ArchiveException
 * @throws CompressorException
 */
public static File fakeReplaceFile(String name, String newContent, InputStream in) throws IOException {
    Assert.notNull(name);
    Assert.notNull(in);

    File newArchive = FileUtils.createRandomTempFile(".tar.gz");
    newArchive.deleteOnExit();

    TarArchiveOutputStream newArchiveOut = new TarArchiveOutputStream(
            new GZIPOutputStream(new FileOutputStream(newArchive)));
    newArchiveOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

    TarArchiveInputStream archiveIn = new TarArchiveInputStream(new GZIPInputStream(in));
    String pathToReplace = null;
    try {
        // copy the existing entries
        for (ArchiveEntry nextEntry = null; (nextEntry = archiveIn.getNextEntry()) != null;) {
            if (nextEntry.getName().endsWith(name)) {
                pathToReplace = nextEntry.getName();
            }
            newArchiveOut.putArchiveEntry(nextEntry);
            IOUtils.copy(archiveIn, newArchiveOut);
            newArchiveOut.closeArchiveEntry();
        }

        if (pathToReplace == null) {
            throw new IllegalStateException("Could not find file " + name + " in the given archive.");
        }
        TarArchiveEntry newEntry = new TarArchiveEntry(pathToReplace);
        newEntry.setSize(newContent.length());
        newArchiveOut.putArchiveEntry(newEntry);
        IOUtils.copy(new ByteArrayInputStream(newContent.getBytes()), newArchiveOut);
        newArchiveOut.closeArchiveEntry();

        return newArchive;
    } finally {
        newArchiveOut.finish();
        newArchiveOut.flush();
        StreamUtils.close(archiveIn);
        StreamUtils.close(newArchiveOut);
    }
}