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:org.finra.herd.service.helper.TarHelper.java

/**
 * Creates a TAR archive file of a directory.
 *
 * @param tarFile the TAR file to be created
 * @param dirPath the directory path/*  w w w  .j av  a 2 s  .  c  om*/
 *
 * @throws IOException on error
 */
public void createTarArchive(File tarFile, Path dirPath) throws IOException {
    try (TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream(
            new BufferedOutputStream(new FileOutputStream(tarFile)))) {
        tarArchiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
        tarArchiveOutputStream.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
        addEntryToTarArchive(tarArchiveOutputStream, dirPath, Paths.get(""));
    }
}

From source file:org.gradle.caching.internal.tasks.CommonsTarPacker.java

@Override
public void pack(List<DataSource> inputs, DataTarget output) throws IOException {
    TarArchiveOutputStream tarOutput = new TarArchiveOutputStream(output.openOutput());
    for (DataSource input : inputs) {
        TarArchiveEntry entry = new TarArchiveEntry(input.getName());
        entry.setSize(input.getLength());
        tarOutput.putArchiveEntry(entry);
        PackerUtils.packEntry(input, tarOutput, buffer);
        tarOutput.closeArchiveEntry();//from ww  w.  j a  v  a 2s.  c  o  m
    }
    tarOutput.close();
}

From source file:org.haiku.haikudepotserver.pkg.job.AbstractPkgResourceExportArchiveJobRunner.java

@Override
public void run(JobService jobService, T specification) throws IOException {

    Preconditions.checkArgument(null != jobService);
    Preconditions.checkArgument(null != specification);

    Stopwatch stopwatch = Stopwatch.createStarted();
    final ObjectContext context = serverRuntime.newContext();
    int offset = 0;

    // this will register the outbound data against the job.
    JobDataWithByteSink jobDataWithByteSink = jobService.storeGeneratedData(specification.getGuid(), "download",
            MediaType.TAR.toString());//from  w  w w .  j a v a  2s  . c o m

    try (final OutputStream outputStream = jobDataWithByteSink.getByteSink().openBufferedStream();
            final GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream); // tars assumed to be compressed
            final TarArchiveOutputStream tarOutputStream = new TarArchiveOutputStream(gzipOutputStream)) {

        State state = new State();
        state.tarArchiveOutputStream = tarOutputStream;
        EJBQLQuery query = createEjbqlQuery(specification);
        query.setFetchLimit(getBatchSize());
        int countLastQuery;

        do {
            query.setFetchOffset(offset);
            List<Object[]> queryResults = context.performQuery(query);
            countLastQuery = queryResults.size();
            appendFromRawRows(state, queryResults);
            offset += countLastQuery;

            if (0 == offset % 100) {
                LOGGER.debug("processed {} entries", offset + 1);
            }

        } while (countLastQuery > 0);

        appendArchiveInfo(state);
    }

    LOGGER.info("did produce report for {} entries in {}ms", offset, stopwatch.elapsed(TimeUnit.MILLISECONDS));

}

From source file:org.hyperledger.fabric.sdk.helper.SDKUtil.java

/**
 * Compress the given directory src to target tar.gz file
 * @param src The source directory// w w w  .  ja v  a  2  s .c  o m
 * @param target The target tar.gz file
 * @throws IOException
 */
public static void generateTarGz(String src, String target) throws IOException {
    File sourceDirectory = new File(src);
    File destinationArchive = new File(target);

    String sourcePath = sourceDirectory.getAbsolutePath();
    FileOutputStream destinationOutputStream = new FileOutputStream(destinationArchive);

    TarArchiveOutputStream archiveOutputStream = new TarArchiveOutputStream(
            new GzipCompressorOutputStream(new BufferedOutputStream(destinationOutputStream)));
    archiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

    try {
        Collection<File> childrenFiles = org.apache.commons.io.FileUtils.listFiles(sourceDirectory, null, true);
        childrenFiles.remove(destinationArchive);

        ArchiveEntry archiveEntry;
        FileInputStream fileInputStream;
        for (File childFile : childrenFiles) {
            String childPath = childFile.getAbsolutePath();
            String relativePath = childPath.substring((sourcePath.length() + 1), childPath.length());

            relativePath = FilenameUtils.separatorsToUnix(relativePath);
            archiveEntry = new TarArchiveEntry(childFile, relativePath);
            fileInputStream = new FileInputStream(childFile);
            archiveOutputStream.putArchiveEntry(archiveEntry);

            try {
                IOUtils.copy(fileInputStream, archiveOutputStream);
            } finally {
                IOUtils.closeQuietly(fileInputStream);
                archiveOutputStream.closeArchiveEntry();
            }
        }
    } finally {
        IOUtils.closeQuietly(archiveOutputStream);
    }
}

From source file:org.hyperledger.fabric.sdk.helper.Utils.java

/**
 * Compress the contents of given directory using Tar and Gzip to an in-memory byte array.
 *
 * @param sourceDirectory  the source directory.
 * @param pathPrefix       a path to be prepended to every file name in the .tar.gz output, or {@code null} if no prefix is required.
 * @param chaincodeMetaInf//from   www.j  a va2 s  .co  m
 * @return the compressed directory contents.
 * @throws IOException
 */
public static byte[] generateTarGz(File sourceDirectory, String pathPrefix, File chaincodeMetaInf)
        throws IOException {
    logger.trace(format("generateTarGz: sourceDirectory: %s, pathPrefix: %s, chaincodeMetaInf: %s",
            sourceDirectory == null ? "null" : sourceDirectory.getAbsolutePath(), pathPrefix,
            chaincodeMetaInf == null ? "null" : chaincodeMetaInf.getAbsolutePath()));

    ByteArrayOutputStream bos = new ByteArrayOutputStream(500000);

    String sourcePath = sourceDirectory.getAbsolutePath();

    TarArchiveOutputStream archiveOutputStream = new TarArchiveOutputStream(
            new GzipCompressorOutputStream(bos));
    archiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

    try {
        Collection<File> childrenFiles = org.apache.commons.io.FileUtils.listFiles(sourceDirectory, null, true);

        ArchiveEntry archiveEntry;
        FileInputStream fileInputStream;
        for (File childFile : childrenFiles) {
            String childPath = childFile.getAbsolutePath();
            String relativePath = childPath.substring((sourcePath.length() + 1), childPath.length());

            if (pathPrefix != null) {
                relativePath = Utils.combinePaths(pathPrefix, relativePath);
            }

            relativePath = FilenameUtils.separatorsToUnix(relativePath);

            if (TRACE_ENABED) {
                logger.trace(format("generateTarGz: Adding '%s' entry from source '%s' to archive.",
                        relativePath, childFile.getAbsolutePath()));
            }

            archiveEntry = new TarArchiveEntry(childFile, relativePath);
            fileInputStream = new FileInputStream(childFile);
            archiveOutputStream.putArchiveEntry(archiveEntry);

            try {
                IOUtils.copy(fileInputStream, archiveOutputStream);
            } finally {
                IOUtils.closeQuietly(fileInputStream);
                archiveOutputStream.closeArchiveEntry();
            }

        }

        if (null != chaincodeMetaInf) {
            childrenFiles = org.apache.commons.io.FileUtils.listFiles(chaincodeMetaInf, null, true);

            final URI metabase = chaincodeMetaInf.toURI();

            for (File childFile : childrenFiles) {

                final String relativePath = Paths
                        .get("META-INF", metabase.relativize(childFile.toURI()).getPath()).toString();

                if (TRACE_ENABED) {
                    logger.trace(format("generateTarGz: Adding '%s' entry from source '%s' to archive.",
                            relativePath, childFile.getAbsolutePath()));
                }

                archiveEntry = new TarArchiveEntry(childFile, relativePath);
                fileInputStream = new FileInputStream(childFile);
                archiveOutputStream.putArchiveEntry(archiveEntry);

                try {
                    IOUtils.copy(fileInputStream, archiveOutputStream);
                } finally {
                    IOUtils.closeQuietly(fileInputStream);
                    archiveOutputStream.closeArchiveEntry();
                }

            }

        }
    } finally {
        IOUtils.closeQuietly(archiveOutputStream);
    }

    return bos.toByteArray();
}

From source file:org.hyperledger.fabric.sdkintegration.Util.java

/**
 * Generate a targz inputstream from source folder.
 *
 * @param src        Source location/* w w w. ja v  a 2 s.c  o m*/
 * @param pathPrefix prefix to add to the all files found.
 * @return return inputstream.
 * @throws IOException
 */
public static InputStream generateTarGzInputStream(File src, String pathPrefix) throws IOException {
    File sourceDirectory = src;

    ByteArrayOutputStream bos = new ByteArrayOutputStream(500000);

    String sourcePath = sourceDirectory.getAbsolutePath();

    TarArchiveOutputStream archiveOutputStream = new TarArchiveOutputStream(
            new GzipCompressorOutputStream(new BufferedOutputStream(bos)));
    archiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

    try {
        Collection<File> childrenFiles = org.apache.commons.io.FileUtils.listFiles(sourceDirectory, null, true);

        ArchiveEntry archiveEntry;
        FileInputStream fileInputStream;
        for (File childFile : childrenFiles) {
            String childPath = childFile.getAbsolutePath();
            String relativePath = childPath.substring((sourcePath.length() + 1), childPath.length());

            if (pathPrefix != null) {
                relativePath = Utils.combinePaths(pathPrefix, relativePath);
            }

            relativePath = FilenameUtils.separatorsToUnix(relativePath);

            archiveEntry = new TarArchiveEntry(childFile, relativePath);
            fileInputStream = new FileInputStream(childFile);
            archiveOutputStream.putArchiveEntry(archiveEntry);

            try {
                IOUtils.copy(fileInputStream, archiveOutputStream);
            } finally {
                IOUtils.closeQuietly(fileInputStream);
                archiveOutputStream.closeArchiveEntry();
            }
        }
    } finally {
        IOUtils.closeQuietly(archiveOutputStream);
    }

    return new ByteArrayInputStream(bos.toByteArray());
}

From source file:org.icgc.dcc.download.client.io.ArchiveOutputStream.java

private static TarArchiveOutputStream createTarOutputStream(OutputStream out) {
    val tarOut = new TarArchiveOutputStream(new BufferedOutputStream(out));
    tarOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    tarOut.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);

    return tarOut;
}

From source file:org.icgc.dcc.portal.manifest.ManifestArchive.java

private static TarArchiveOutputStream createTar(OutputStream output) throws IOException {
    val tar = new TarArchiveOutputStream(new GZIPOutputStream(new BufferedOutputStream(output)));
    tar.setLongFileMode(LONGFILE_GNU);//from   w  w  w .j a  v  a2  s .com

    return tar;
}

From source file:org.jboss.tools.windup.ui.internal.archiver.TarFileExporter.java

/**
 * @see org.jboss.tools.windup.ui.internal.archiver.AbstractArchiveFileExporter#createOutputStream(java.lang.String, boolean)
 *///from w  w w  . j a  va  2  s.  c o  m
@Override
protected ArchiveOutputStream createOutputStream(String archiveName, boolean compress) throws IOException {

    ArchiveOutputStream archiveOut;
    // if compression enabled use GZip to do the compression
    if (compress) {
        this.gzipOutputStream = new GZIPOutputStream(new FileOutputStream(archiveName));
        archiveOut = new TarArchiveOutputStream(new BufferedOutputStream(this.gzipOutputStream));
    } else {
        archiveOut = new TarArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(archiveName)));
    }

    return archiveOut;
}

From source file:org.jclouds.docker.compute.features.internal.Archives.java

public static File tar(File baseDir, String archivePath) throws IOException {
    // Check that the directory is a directory, and get its contents
    checkArgument(baseDir.isDirectory(), "%s is not a directory", baseDir);
    File[] files = baseDir.listFiles();
    File tarFile = new File(archivePath);

    String token = getLast(Splitter.on("/").split(archivePath.substring(0, archivePath.lastIndexOf("/"))));

    byte[] buf = new byte[1024];
    int len;/*from  w w w .  j a v  a 2 s.  c o  m*/
    TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(tarFile));
    tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    for (File file : files) {
        TarArchiveEntry tarEntry = new TarArchiveEntry(file);
        tarEntry.setName("/" + getLast(Splitter.on(token).split(file.toString())));
        tos.putArchiveEntry(tarEntry);
        if (!file.isDirectory()) {
            FileInputStream fin = new FileInputStream(file);
            BufferedInputStream in = new BufferedInputStream(fin);
            while ((len = in.read(buf)) != -1) {
                tos.write(buf, 0, len);
            }
            in.close();
        }
        tos.closeArchiveEntry();
    }
    tos.close();
    return tarFile;
}