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, int blockSize) 

Source Link

Document

Constructor for TarInputStream.

Usage

From source file:de.uzk.hki.da.pkg.ArchiveBuilder.java

/**
 * Create an archive file out of the given source folder
 * //from ww  w .j a  va2 s  .  c  o m
 * @param srcFolder The folder to archive
 * @param destFile The archive file to build
 * @param includeFolder Indicates if the source folder will be included to the archive file on
 * the first level or not
 * @param compress Indicates if the archive file will be compressed (tgz file) or not (tar file)
 * @return false if the SIP creation process was aborted during the archive file creation process, otherwise true
 * @throws Exception
 */
public boolean archiveFolder(File srcFolder, File destFile, boolean includeFolder, boolean compress)
        throws Exception {

    FileOutputStream fOut = null;
    GzipCompressorOutputStream gzOut = null;
    TarArchiveOutputStream tOut = null;

    try {
        fOut = new FileOutputStream(destFile);

        if (compress) {
            gzOut = new GzipCompressorOutputStream(fOut);
            tOut = new TarArchiveOutputStream(gzOut, "UTF-8");
        } else
            tOut = new TarArchiveOutputStream(fOut, "UTF-8");

        tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        tOut.setBigNumberMode(2);

        if (includeFolder) {
            if (!addFileToArchive(tOut, srcFolder, ""))
                return false;
        } else {
            File children[] = srcFolder.listFiles();

            for (int i = 0; i < children.length; i++) {
                if (!addFileToArchive(tOut, children[i], ""))
                    return false;
            }
        }
    } finally {
        tOut.finish();
        tOut.close();
        if (gzOut != null)
            gzOut.close();
        fOut.close();
    }

    return true;
}

From source file:gobblin.util.io.StreamUtils.java

/**
 * Similiar to {@link #tar(FileSystem, Path, Path)} except the source and destination {@link FileSystem} can be different.
 *
 * @see #tar(FileSystem, Path, Path)//from   ww w .ja va 2  s  . c  o m
 */
public static void tar(FileSystem sourceFs, FileSystem destFs, Path sourcePath, Path destPath)
        throws IOException {
    try (FSDataOutputStream fsDataOutputStream = destFs.create(destPath);
            TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream(
                    new GzipCompressorOutputStream(fsDataOutputStream),
                    ConfigurationKeys.DEFAULT_CHARSET_ENCODING.name())) {

        FileStatus fileStatus = sourceFs.getFileStatus(sourcePath);

        if (sourceFs.isDirectory(sourcePath)) {
            dirToTarArchiveOutputStreamRecursive(fileStatus, sourceFs, Optional.<Path>absent(),
                    tarArchiveOutputStream);
        } else {
            try (FSDataInputStream fsDataInputStream = sourceFs.open(sourcePath)) {
                fileToTarArchiveOutputStream(fileStatus, fsDataInputStream, new Path(sourcePath.getName()),
                        tarArchiveOutputStream);
            }
        }
    }
}

From source file:edu.wisc.doit.tcrypt.BouncyCastleFileEncrypter.java

@Override
public OutputStream encrypt(String fileName, int size, OutputStream outputStream)
        throws InvalidCipherTextException, IOException, DecoderException {
    final TarArchiveOutputStream tarOutputStream = new TarArchiveOutputStream(outputStream, ENCODING);

    final BufferedBlockCipher cipher = createCipher(tarOutputStream);

    startEncryptedFile(fileName, size, tarOutputStream, cipher);

    //Protect the underlying TAR stream from being closed by the cipher stream
    final CloseShieldOutputStream closeShieldTarStream = new CloseShieldOutputStream(tarOutputStream);

    //Setup the encrypting cipher stream
    final CipherOutputStream chipherStream = new CipherOutputStream(closeShieldTarStream, cipher);

    //Generate a digest of the pre-encryption data
    final GeneralDigest digest = this.createDigester();
    final DigestOutputStream digestOutputStream = new DigestOutputStream(digest);

    //Write data to both the digester and cipher
    final TeeOutputStream teeStream = new TeeOutputStream(digestOutputStream, chipherStream);
    return new EncryptingOutputStream(teeStream, tarOutputStream, digest);
}

From source file:org.apache.ant.compress.util.TarStreamFactory.java

/**
 * @param stream the stream to write to, should be buffered
 * @param encoding the encoding of the entry names
 *//*from ww  w. ja  va  2s . c om*/
public ArchiveOutputStream getArchiveStream(OutputStream stream, String encoding) throws IOException {
    return new TarArchiveOutputStream(stream, encoding);
}

From source file:org.apache.tika.parser.pkg.TikaArchiveStreamFactory.java

@Override
public ArchiveOutputStream createArchiveOutputStream(final String archiverName, final OutputStream out,
        final String actualEncoding) throws ArchiveException {
    if (archiverName == null) {
        throw new IllegalArgumentException("Archivername must not be null.");
    }/*from   w w  w  .  j  av  a 2  s.c o m*/
    if (out == null) {
        throw new IllegalArgumentException("OutputStream must not be null.");
    }

    if (AR.equalsIgnoreCase(archiverName)) {
        return new ArArchiveOutputStream(out);
    }
    if (ZIP.equalsIgnoreCase(archiverName)) {
        final ZipArchiveOutputStream zip = new ZipArchiveOutputStream(out);
        if (actualEncoding != null) {
            zip.setEncoding(actualEncoding);
        }
        return zip;
    }
    if (TAR.equalsIgnoreCase(archiverName)) {
        if (actualEncoding != null) {
            return new TarArchiveOutputStream(out, actualEncoding);
        }
        return new TarArchiveOutputStream(out);
    }
    if (JAR.equalsIgnoreCase(archiverName)) {
        if (actualEncoding != null) {
            return new JarArchiveOutputStream(out, actualEncoding);
        }
        return new JarArchiveOutputStream(out);
    }
    if (CPIO.equalsIgnoreCase(archiverName)) {
        if (actualEncoding != null) {
            return new CpioArchiveOutputStream(out, actualEncoding);
        }
        return new CpioArchiveOutputStream(out);
    }
    if (SEVEN_Z.equalsIgnoreCase(archiverName)) {
        throw new StreamingNotSupportedException(SEVEN_Z);
    }

    final ArchiveStreamProvider archiveStreamProvider = getArchiveOutputStreamProviders()
            .get(toKey(archiverName));
    if (archiveStreamProvider != null) {
        return archiveStreamProvider.createArchiveOutputStream(archiverName, out, actualEncoding);
    }

    throw new ArchiveException("Archiver: " + archiverName + " not found.");
}

From source file:org.codehaus.plexus.archiver.tar.TarArchiver.java

protected void execute() throws ArchiverException, IOException {
    if (!checkForced()) {
        return;// w  ww  .j a va 2s.  c  o  m
    }

    ResourceIterator iter = getResources();
    if (!iter.hasNext()) {
        throw new ArchiverException("You must set at least one file.");
    }

    File tarFile = getDestFile();

    if (tarFile == null) {
        throw new ArchiverException("You must set the destination tar file.");
    }
    if (tarFile.exists() && !tarFile.isFile()) {
        throw new ArchiverException(tarFile + " isn't a file.");
    }
    if (tarFile.exists() && !tarFile.canWrite()) {
        throw new ArchiverException(tarFile + " is read-only.");
    }

    getLogger().info("Building tar: " + tarFile.getAbsolutePath());

    final OutputStream os = new FileOutputStream(tarFile);
    tOut = new TarArchiveOutputStream(compress(compression, os), "UTF8");
    if (longFileMode.isTruncateMode()) {
        tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_TRUNCATE);
    } else if (longFileMode.isPosixMode() || longFileMode.isPosixWarnMode()) {
        tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
        // Todo: Patch 2.5.1   for this fix. Also make closeable fix on 2.5.1
        tOut.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
    } else if (longFileMode.isFailMode() || longFileMode.isOmitMode()) {
        tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_ERROR);
    } else {
        // warn or GNU
        tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    }

    longWarningGiven = false;
    try {
        while (iter.hasNext()) {
            ArchiveEntry entry = iter.next();
            // Check if we don't add tar file in itself
            if (ResourceUtils.isSame(entry.getResource(), tarFile)) {
                throw new ArchiverException("A tar file cannot include itself.");
            }
            String fileName = entry.getName();
            String name = StringUtils.replace(fileName, File.separatorChar, '/');

            tarFile(entry, tOut, name);
        }
    } finally {
        IOUtil.close(tOut);
    }
}

From source file:org.eclipse.jgit.archive.TarFormat.java

/**
 * @since 4.0//from   w  ww. j a v a 2s . com
 */
public ArchiveOutputStream createArchiveOutputStream(OutputStream s, Map<String, Object> o) throws IOException {
    TarArchiveOutputStream out = new TarArchiveOutputStream(s, "UTF-8"); //$NON-NLS-1$
    out.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
    out.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
    return applyFormatOptions(out, o);
}

From source file:org.eclipse.tycho.plugins.tar.TarGzArchiver.java

public void createArchive() throws IOException {
    validate();//from w w  w. ja  v  a  2s. c  o m
    log.info("Building tar: " + destFile);
    TarArchiveOutputStream tarStream = null;
    try {
        destFile.getAbsoluteFile().getParentFile().mkdirs();
        GzipCompressorOutputStream gzipStream = new GzipCompressorOutputStream(
                new BufferedOutputStream(new FileOutputStream(destFile)));
        tarStream = new TarArchiveOutputStream(gzipStream, "UTF-8");
        // allow "long" file paths (> 100 chars)
        tarStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        for (File sourceDir : sourceDirs) {
            for (File child : sourceDir.listFiles()) {
                addToTarRecursively(sourceDir, child, tarStream);
            }
        }
    } finally {
        if (tarStream != null) {
            tarStream.close();
        }
    }
}

From source file:org.gradle.caching.internal.packaging.impl.TarBuildCacheEntryPacker.java

@Override
public PackResult pack(CacheableEntity entity, Map<String, CurrentFileCollectionFingerprint> fingerprints,
        OutputStream output, OriginWriter writeOrigin) throws IOException {
    BufferedOutputStream bufferedOutput;
    if (output instanceof BufferedOutputStream) {
        bufferedOutput = (BufferedOutputStream) output;
    } else {/*from ww  w.j a v a2s .c  o  m*/
        bufferedOutput = new BufferedOutputStream(output);
    }
    try (TarArchiveOutputStream tarOutput = new TarArchiveOutputStream(bufferedOutput, "utf-8")) {
        tarOutput.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
        tarOutput.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
        tarOutput.setAddPaxHeadersForNonAsciiNames(true);
        packMetadata(writeOrigin, tarOutput);
        long entryCount = pack(entity, fingerprints, tarOutput);
        return new PackResult(entryCount + 1);
    }
}