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.TarProcessor.java

/**
 * Compress data/*from  ww  w  . j  av  a2  s. c o  m*/
 * 
 * @param fileCompressor
 *            FileCompressor object
 * @return
 * @throws Exception
 */
@Override
public byte[] compressData(FileCompressor fileCompressor) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    TarArchiveOutputStream aos = new TarArchiveOutputStream(baos);
    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();
        baos.close();
    }
    return baos.toByteArray();
}

From source file:com.streamsets.datacollector.restapi.TarEdgeArchiveBuilder.java

@Override
public void finish() throws IOException {
    try (TarArchiveOutputStream tarArchiveOutput = new TarArchiveOutputStream(
            new GzipCompressorOutputStream(outputStream));
            TarArchiveInputStream tarArchiveInput = new TarArchiveInputStream(
                    new GzipCompressorInputStream(new FileInputStream(edgeArchive)))) {
        tarArchiveOutput.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
        TarArchiveEntry entry = tarArchiveInput.getNextTarEntry();

        while (entry != null) {
            tarArchiveOutput.putArchiveEntry(entry);
            IOUtils.copy(tarArchiveInput, tarArchiveOutput);
            tarArchiveOutput.closeArchiveEntry();
            entry = tarArchiveInput.getNextTarEntry();
        }//  w w  w  .  j  a  v a  2 s . c o  m

        for (PipelineConfigurationJson pipelineConfiguration : pipelineConfigurationList) {
            addArchiveEntry(tarArchiveOutput, pipelineConfiguration, pipelineConfiguration.getPipelineId(),
                    PIPELINE_JSON_FILE);
            addArchiveEntry(tarArchiveOutput, pipelineConfiguration.getInfo(),
                    pipelineConfiguration.getPipelineId(), PIPELINE_INFO_FILE);
        }

        tarArchiveOutput.finish();
    }
}

From source file:hudson.util.io.TarArchiver.java

TarArchiver(OutputStream out) {
    tar = new TarArchiveOutputStream(out);
    tar.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);
    tar.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
}

From source file:com.ibm.util.merge.storage.TarArchive.java

@Override
public void openOutputStream() throws IOException {
    FileOutputStream fos = new FileOutputStream(getFilePath());
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    this.setOutputStream(new TarArchiveOutputStream(bos));
}

From source file:mase.stat.CompetitiveBestStat.java

@Override
public void setup(EvolutionState state, Parameter base) {
    super.setup(state, base);
    compress = state.parameters.getBoolean(base.push(P_COMPRESS), null, true);
    int n = state.parameters.getInt(new Parameter("pop.subpops"), null);
    outFile = new File[n];
    for (int i = 0; i < n; i++) {
        outFile[i] = state.parameters.getFile(base.push(P_FILE), null);
        String newName = compress ? outFile[i].getName().replace(".tar.gz", "." + i + ".tar.gz")
                : outFile[i].getName() + "." + i;
        outFile[i] = new File(outFile[i].getParent(), jobPrefix + newName);
    }/* w  w w  .j  a v  a2 s. c  o m*/
    if (compress) {
        taos = new TarArchiveOutputStream[n];
        for (int i = 0; i < n; i++) {
            try {
                taos[i] = new TarArchiveOutputStream(
                        new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(outFile[i]))));
            } catch (IOException ex) {
                Logger.getLogger(BestSolutionGenStat.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    for (int i = 0; i < n; i++) {
        if (!compress && !outFile[i].exists()) {
            outFile[i].mkdirs();
        }
    }
    if (state.parameters.getBoolean(base.push(P_KEEP_LAST), null, true)) {
        last = new File[n];
        for (int i = 0; i < n; i++) {
            last[i] = new File(outFile[i].getParent(), jobPrefix + "last." + i + ".xml");
        }
    }
}

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

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

From source file:jetbrains.exodus.util.CompressBackupUtil.java

@NotNull
public static File backup(@NotNull final Backupable target, @NotNull final File backupRoot,
        @Nullable final String backupNamePrefix, final boolean zip) throws Exception {
    if (!backupRoot.exists() && !backupRoot.mkdirs()) {
        throw new IOException("Failed to create " + backupRoot.getAbsolutePath());
    }//from  www  . j a v a  2 s . c  om
    final File backupFile;
    final BackupStrategy strategy = target.getBackupStrategy();
    strategy.beforeBackup();
    try {
        final ArchiveOutputStream archive;
        if (zip) {
            final String fileName = getTimeStampedZipFileName();
            backupFile = new File(backupRoot,
                    backupNamePrefix == null ? fileName : backupNamePrefix + fileName);
            final ZipArchiveOutputStream zipArchive = new ZipArchiveOutputStream(
                    new BufferedOutputStream(new FileOutputStream(backupFile)));
            zipArchive.setLevel(Deflater.BEST_COMPRESSION);
            archive = zipArchive;
        } else {
            final String fileName = getTimeStampedTarGzFileName();
            backupFile = new File(backupRoot,
                    backupNamePrefix == null ? fileName : backupNamePrefix + fileName);
            archive = new TarArchiveOutputStream(
                    new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(backupFile))));
        }
        try (ArchiveOutputStream aos = archive) {
            for (final BackupStrategy.FileDescriptor fd : strategy.listFiles()) {
                if (strategy.isInterrupted()) {
                    break;
                }
                final File file = fd.getFile();
                if (file.isFile()) {
                    final long fileSize = Math.min(fd.getFileSize(), strategy.acceptFile(file));
                    if (fileSize > 0L) {
                        archiveFile(aos, fd.getPath(), file, fileSize);
                    }
                }
            }
        }
        if (strategy.isInterrupted()) {
            logger.info("Backup interrupted, deleting \"" + backupFile.getName() + "\"...");
            IOUtil.deleteFile(backupFile);
        } else {
            logger.info("Backup file \"" + backupFile.getName() + "\" created.");
        }
    } catch (Throwable t) {
        strategy.onError(t);
        throw ExodusException.toExodusException(t, "Backup failed");
    } finally {
        strategy.afterBackup();
    }
    return backupFile;
}

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

/**
 * Compress data//from w w w.j  a v a2s.  c  o  m
 * 
 * @param fileCompressor
 *            FileCompressor object
 * @return
 * @throws Exception
 */
@Override
public byte[] compressData(FileCompressor fileCompressor) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XZCompressorOutputStream cos = new XZCompressorOutputStream(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.espringtran.compressor4j.processor.TarGzProcessor.java

/**
 * Compress data/*from  w w  w .j  a  v a 2s  . c om*/
 * 
 * @param fileCompressor
 *            FileCompressor object
 * @return
 * @throws Exception
 */
@Override
public byte[] compressData(FileCompressor fileCompressor) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GzipCompressorOutputStream cos = new GzipCompressorOutputStream(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.linkedin.thirdeye.bootstrap.util.TarGzCompressionUtils.java

/**
 * Creates a tar.gz file at the specified path with the contents of the
 * specified directory.//from   w ww . j a v a2  s.c  o 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();
    }
}