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

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

Introduction

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

Prototype

public void setLongFileMode(int longFileMode) 

Source Link

Document

Set the long file mode.

Usage

From source file:com.gitblit.utils.CompressionUtils.java

/**
 * Compresses/archives the contents of the tree at the (optionally)
 * specified revision and the (optionally) specified basepath to the
 * supplied outputstream./* w w w  .  j a va 2  s  . co  m*/
 * 
 * @param algorithm
 *            compression algorithm for tar (optional)
 * @param repository
 * @param basePath
 *            if unspecified, entire repository is assumed.
 * @param objectId
 *            if unspecified, HEAD is assumed.
 * @param os
 * @return true if repository was successfully zipped to supplied output
 *         stream
 */
private static boolean tar(String algorithm, Repository repository, String basePath, String objectId,
        OutputStream os) {
    RevCommit commit = JGitUtils.getCommit(repository, objectId);
    if (commit == null) {
        return false;
    }

    OutputStream cos = os;
    if (!StringUtils.isEmpty(algorithm)) {
        try {
            cos = new CompressorStreamFactory().createCompressorOutputStream(algorithm, os);
        } catch (CompressorException e1) {
            error(e1, repository, "{0} failed to open {1} stream", algorithm);
        }
    }
    boolean success = false;
    RevWalk rw = new RevWalk(repository);
    TreeWalk tw = new TreeWalk(repository);
    try {
        tw.reset();
        tw.addTree(commit.getTree());
        TarArchiveOutputStream tos = new TarArchiveOutputStream(cos);
        tos.setAddPaxHeadersForNonAsciiNames(true);
        tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
        if (!StringUtils.isEmpty(basePath)) {
            PathFilter f = PathFilter.create(basePath);
            tw.setFilter(f);
        }
        tw.setRecursive(true);
        MutableObjectId id = new MutableObjectId();
        long modified = commit.getAuthorIdent().getWhen().getTime();
        while (tw.next()) {
            FileMode mode = tw.getFileMode(0);
            if (mode == FileMode.GITLINK || mode == FileMode.TREE) {
                continue;
            }
            tw.getObjectId(id, 0);

            ObjectLoader loader = repository.open(id);
            if (FileMode.SYMLINK == mode) {
                TarArchiveEntry entry = new TarArchiveEntry(tw.getPathString(), TarArchiveEntry.LF_SYMLINK);
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                loader.copyTo(bos);
                entry.setLinkName(bos.toString());
                entry.setModTime(modified);
                tos.putArchiveEntry(entry);
                tos.closeArchiveEntry();
            } else {
                TarArchiveEntry entry = new TarArchiveEntry(tw.getPathString());
                entry.setMode(mode.getBits());
                entry.setModTime(modified);
                entry.setSize(loader.getSize());
                tos.putArchiveEntry(entry);
                loader.copyTo(tos);
                tos.closeArchiveEntry();
            }
        }
        tos.finish();
        tos.close();
        cos.close();
        success = true;
    } catch (IOException e) {
        error(e, repository, "{0} failed to {1} stream files from commit {2}", algorithm, commit.getName());
    } finally {
        tw.release();
        rw.dispose();
    }
    return success;
}

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

public static void compressDirectory(Path path, OutputStream output) {
    try {//from w  w w . ja  v  a2  s .  c  o  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.linkedin.thirdeye.bootstrap.util.TarGzCompressionUtils.java

/**
 * Creates a tar.gz file at the specified path with the contents of the
 * specified directory./*  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();
    }
}

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

/**
 * Creates a tar entry for the path specified with a name built from the base
 * passed in and the file/directory name. If the path is a directory, a
 * recursive call is made such that the full directory is added to the tar.
 *
 * @param tOut/*from w ww.ja v  a  2s.c o m*/
 *          The tar file's output stream
 * @param path
 *          The filesystem path of the file/directory being added
 * @param base
 *          The base prefix to for the name of the tar file entry
 *
 * @throws IOException
 *           If anything goes wrong
 */
private static void addFileToTarGz(TarArchiveOutputStream tOut, String path, String base) throws IOException {
    File f = new File(path);
    String entryName = base + f.getName();
    TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
    FileInputStream fis = null;

    try {
        tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

        tOut.putArchiveEntry(tarEntry);

        if (f.isFile()) {
            fis = new FileInputStream(f);
            IOUtils.copy(fis, tOut);

            tOut.closeArchiveEntry();
        } else {
            tOut.closeArchiveEntry();

            File[] children = f.listFiles();

            if (children != null) {
                for (File child : children) {
                    addFileToTarGz(tOut, child.getAbsolutePath(), entryName + "/");
                }
            }
        }
    } finally {
        if (fis != null) {
            fis.close();
        }
    }

}

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

private static void addFile(final TarArchiveOutputStream tarOutputStream, final String path, final String base)
        throws IOException {
    final File file = new File(path);
    final String entryName = base + file.getName();
    final TarArchiveEntry tarEntry = new TarArchiveEntry(file, entryName);

    tarOutputStream.setLongFileMode(LONGFILE_GNU);
    tarOutputStream.putArchiveEntry(tarEntry);

    if (file.isFile()) {
        copy(new FileInputStream(file), tarOutputStream);
        tarOutputStream.closeArchiveEntry();
    } else {/*from  w w w  .  j ava  2s.c o  m*/
        tarOutputStream.closeArchiveEntry();
        final File[] children = file.listFiles();
        if (children != null) {
            for (final File child : children) {
                addFile(tarOutputStream, child.getAbsolutePath(), entryName + separator);
            }
        }
    }
}

From source file:com.shopzilla.hadoop.repl.commands.util.ClusterStateManager.java

public static void compressFiles(Collection<File> files, File output) throws IOException {
    // Create the output stream for the output file
    FileOutputStream fos = new FileOutputStream(output);
    // Wrap the output file stream in streams that will tar and gzip everything
    TarArchiveOutputStream taos = new TarArchiveOutputStream(
            new GZIPOutputStream(new BufferedOutputStream(fos)));
    // 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);

    // Get to putting all the files in the compressed output file
    for (File f : files) {
        addFilesToCompression(taos, f, ".");
    }/*from   w  ww  .  j  ava2  s  .  c om*/

    // Close everything up
    taos.close();
    fos.close();
}

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  ww  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:lucee.commons.io.compress.CompressUtil.java

public static void compressTar(Resource[] sources, OutputStream target, int mode) throws IOException {
    if (target instanceof TarArchiveOutputStream) {
        compressTar("", sources, (TarArchiveOutputStream) target, mode);
        return;//www.j  a  va2  s .  c  o  m
    }
    TarArchiveOutputStream tos = new TarArchiveOutputStream(target);
    tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    try {
        compressTar("", sources, tos, mode);
    } finally {
        IOUtil.closeEL(tos);
    }
}

From source file:bb.io.TarUtil.java

/**
* Writes each element of pathsToArchive to a new TAR format archive file specified by tarFile.
* If any element is a directory, the entire contents of its directory tree will be archived (as limited by filter).
* Paths that would otherwise be archived may be screened out by supplying a non null value for filter.
* <p>/*from w w  w .ja  v a2s .c om*/
* Altho this method does not use {@link DirUtil#getTree DirUtil.getTree},
* it uses filter to control subdirectory exploration in a similar manner.
* <p>
* In general, the path stored in the archive
* is the path relative to the <i>parent</i> of the relevant element of pathsToArchive.
* For example, suppose that some element of pathsToArchive corresponds to <code>D:/someDirectory</code>,
* and suppose that that directory contains the subdirectory and child file <code>D:/someDirectory/anotherDirectory/childFile</code>.
* Then the paths stored in the archive are <code>anotherDirectory</code> and <code>anotherDirectory/childFile</code> respectively.
* <p>
* One complication with the above scheme is paths which are file system roots: they have no parents.
* Examples include the windows path <code>C:</code> or the unix path <code>/</code>.
* In cases like these, this method uses an imaginary parent name of the form <code>rootXXX</code> (where XXX is an integer).
* For example, on a windows machine, if pathsToArchive contains the paths <code>C:</code> and <code>D:</code>,
* then the contents of <code>C:</code> might be stored in the archive
* with a path that starts with <code>root1</code>, and the contents of <code>D:</code>
* may have an archive path that starts with <code>root2</code>.
* This behavior ensures that the archive preserves the separate origins of the 2 sources,
* which is necessary so that they do not get mixed when extracted.
* <p>
* The TAR archive witten by this method will use GNU TAR rules for the entry headers if long path names are encountered.
* <i>This means that standard POSIX compliant programs that do not support the GNU TAR extension
* will be unable to extract the contents.</i>
* <p>
* Optional GZIP compression may also be done.
* Normally, tarFile must be a path which ends in a ".tar" (case insensitive) extension.
* However, this method will also accept either ".tar.gz" or ".tgz" extensions,
* in which case it will perform GZIP compression on tarFile as part of archiving.
* <p>
* @param tarFile the TAR File that will write the archive data to
* @param filter a FileFilter that can use to screen out paths from being written to the archive;
* may be null, which means everything inside pathsToArchive gets archived;
* if not null, see warnings in {@link DirUtil#getTree DirUtil.getTree} on directory acceptance
* @param pathsToArchive array of all the paths to archive
* @throws Exception if any Throwable is caught; the Throwable is stored as the cause, and the message stores the path of tarFile;
* here are some of the possible causes:
* <ol>
*  <li>
*      IllegalArgumentException if pathsToArchive == null; pathsToArchive.length == 0;
*      tarFile == null;
*      tarFile already exists and either is not a normal file or is but already has data inside it;
*      tarFile has an invalid extension;
*      any element of pathsToArchive is null, does not exist, cannot be read, is equal to tarFile, its path contains tarFile,
*      or it fails {@link #isTarable isTarable}
*  </li>
*  <li>SecurityException if a security manager exists and its SecurityManager.checkRead method denies read access to some path</li>
*  <li>IOException if an I/O problem occurs</li>
* </ol>
*/
public static void archive(File tarFile, FileFilter filter, File... pathsToArchive) throws Exception {
    try {
        Check.arg().notNull(tarFile);
        if (tarFile.exists()) {
            if (!tarFile.isFile())
                throw new IllegalArgumentException(
                        "tarFile = " + tarFile.getPath() + " exists but is not a normal file");
            if (tarFile.length() != 0)
                throw new IllegalArgumentException("tarFile = " + tarFile.getPath()
                        + " already exists and already has data inside it; this method will not overwrite it");
        }
        Check.arg().notEmpty(pathsToArchive);
        for (int i = 0; i < pathsToArchive.length; i++) {
            Check.arg().notNull(pathsToArchive[i]);
            if (!pathsToArchive[i].exists())
                throw new IllegalArgumentException("pathsToArchive[" + i + "] = " + pathsToArchive[i].getPath()
                        + " is a non-existent path");
            if (!pathsToArchive[i].canRead())
                throw new IllegalArgumentException("pathsToArchive[" + i + "] = " + pathsToArchive[i].getPath()
                        + " cannot be read by this application");
            if (pathsToArchive[i].equals(tarFile))
                throw new IllegalArgumentException("pathsToArchive[" + i + "] = " + pathsToArchive[i].getPath()
                        + " is the same path as tarFile = " + tarFile.getPath());
            if (pathsToArchive[i].isDirectory() && DirUtil.contains(pathsToArchive[i], tarFile))
                throw new IllegalArgumentException("the directory corresponding to pathsToArchive[" + i + "] = "
                        + pathsToArchive[i].getCanonicalPath() + " will contain the path of tarFile = "
                        + tarFile.getCanonicalPath());
        }

        TarArchiveOutputStream taos = null;
        try {
            taos = new TarArchiveOutputStream(getOutputStream(tarFile));
            taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
            for (File path : pathsToArchive) {
                archive(path, new FileParent(path), taos, filter);
            }
        } finally {
            if (giveUserFeedback)
                ConsoleUtil.eraseLine();
            StreamUtil.close(taos);
        }
    } catch (Throwable t) {
        throw new Exception("See cause for the underlying Throwable; happened for tarFile = "
                + (tarFile != null ? tarFile.getPath() : "<null>"), t);
    }
}

From source file:adams.core.io.TarUtils.java

/**
 * Returns an output stream for the specified tar archive. Automatically
 * determines the compression used for the archive. Uses GNU long filename
 * support.//  ww  w. ja v a  2  s . c o  m
 *
 * @param input   the tar archive to create the output stream for
 * @param stream   the output stream to wrap
 * @return      the output stream
 * @throws Exception   if file not found or similar problems
 * @see      TarArchiveOutputStream#LONGFILE_GNU
 */
protected static TarArchiveOutputStream openArchiveForWriting(File input, FileOutputStream stream)
        throws Exception {
    TarArchiveOutputStream result;
    Compression comp;

    comp = determineCompression(input);
    if (comp == Compression.GZIP)
        result = new TarArchiveOutputStream(new GzipCompressorOutputStream(new BufferedOutputStream(stream)));
    else if (comp == Compression.BZIP2)
        result = new TarArchiveOutputStream(new BZip2CompressorOutputStream(new BufferedOutputStream(stream)));
    else
        result = new TarArchiveOutputStream(new BufferedOutputStream(stream));

    result.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

    return result;
}