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

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

Introduction

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

Prototype

int LONGFILE_GNU

To view the source code for org.apache.commons.compress.archivers.tar TarArchiveOutputStream LONGFILE_GNU.

Click Source Link

Document

GNU tar extensions are used to store long file names in the archive.

Usage

From source file:com.pinterest.deployservice.common.TarUtils.java

/**
 * Bundle the given map as a list of files, with key as file name and value as content.
 *//* w ww . ja  va  2 s  . com*/
public static byte[] tar(Map<String, String> data) throws Exception {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    TarArchiveOutputStream taos = new TarArchiveOutputStream(new GZIPOutputStream(os));
    // 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 (Map.Entry<String, String> entry : data.entrySet()) {
        byte[] bytes = entry.getValue().getBytes("UTF8");
        InputStream is = new ByteArrayInputStream(bytes);
        addInputStreamToTar(taos, is, entry.getKey(), bytes.length, TarArchiveEntry.DEFAULT_FILE_MODE);
    }

    taos.close();
    return os.toByteArray();
}

From source file:msec.org.TarUtil.java

public static void archive(File srcFile, File destFile) throws Exception {

    TarArchiveOutputStream taos = new TarArchiveOutputStream(new FileOutputStream(destFile));
    taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

    archive(srcFile, taos, "");

    taos.flush();//from w  w w .  ja  v  a 2  s .c  om
    taos.close();
}

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 .  j  a  v a2 s.  c om
 * 
 * @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);
    }
}

From source file:com.fizzed.stork.assembly.AssemblyUtils.java

static public TarArchiveOutputStream createTGZStream(File tgzFile) throws IOException {
    TarArchiveOutputStream tgzout = new TarArchiveOutputStream(
            new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(tgzFile))));
    tgzout.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);
    tgzout.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    return tgzout;
}

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.linkedin.thirdeye.bootstrap.util.TarGzCompressionUtils.java

/**
 * Creates a tar.gz file at the specified path with the contents of the
 * specified directory./* ww w .  j  a  v  a  2 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.linkedin.pinot.common.utils.TarGzCompressionUtils.java

/**
 * Creates a tar.gz file at the specified path with the contents of the
 * specified directory./*from  w ww .ja  v  a  2s.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:com.ttech.cordovabuild.infrastructure.archive.ArchiveUtils.java

public static void compressDirectory(Path path, OutputStream output) {
    try {// w  w w.jav 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.salsaberries.narchiver.Writer.java

/**
 *
 * @param files/* w w w  .  java2s . c om*/
 * @param output
 * @throws IOException
 */
public static void compressFiles(Collection<File> files, File output) throws IOException {
    // Wrap the output file stream in streams that will tar and gzip everything
    try (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)))) {

        // Enable support for long file names
        taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

        // Put all the files in a compressed output file
        for (File f : files) {
            addFilesToCompression(taos, f, ".");
        }
    }
}

From source file:divconq.ctp.stream.TarStream.java

@Override
public ReturnOption handle(FileDescriptor file, ByteBuf data) {
    if (file == FileDescriptor.FINAL) {
        if (this.tstream == null)
            return this.downstream.handle(file, data);

        this.finalflag = true;
    }/*  w w w .  j  av  a2  s.c  o m*/

    // I don't think tar cares about folder entries at this stage - tar is for file content only
    // folder scanning is upstream in the FileSourceStream and partners
    // TODO try with ending / to file name
    if (file.isFolder())
        return ReturnOption.CONTINUE;

    // init if not set for this round of processing 
    if (this.tstream == null) {
        this.bstream = new CyclingByteBufferOutputStream();
        this.tstream = new TarArchiveOutputStream(this.bstream);
        this.tstream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    }

    ByteBuf in = data;
    ByteBuf out = null;

    // always allow for a header (512) and/or footer (1024) in addition to content
    int sizeEstimate = (in != null) ? in.readableBytes() + 2048 : 2048;
    out = Hub.instance.getBufferAllocator().heapBuffer(sizeEstimate);

    this.bstream.installBuffer(out);

    // TODO if there is no output available to send and not EOF then just request more,
    // no need to send a message that is empty and not EOF

    FileDescriptor blk = new FileDescriptor();

    if (StringUtil.isNotEmpty(this.lastpath)) {
        blk.setPath(this.lastpath);
    } else {
        if (file.hasPath())
            this.lastpath = "/"
                    + (StringUtil.isNotEmpty(this.nameHint) ? this.nameHint : file.path().getFileName())
                    + ".tar";
        else if (StringUtil.isNotEmpty(this.nameHint))
            this.lastpath = "/" + this.nameHint + ".tar";
        else
            this.lastpath = "/" + FileUtil.randomFilename() + ".tar";

        blk.setPath(this.lastpath);
    }

    blk.setModTime(System.currentTimeMillis());

    if (!this.archiveopenflag && !this.finalflag) {
        TarArchiveEntry tentry = new TarArchiveEntry(file.getPath().toString().substring(1), true);
        tentry.setSize(file.getSize());
        tentry.setModTime(file.getModTime());

        try {
            this.tstream.putArchiveEntry(tentry);
        } catch (IOException x) {
            if (in != null)
                in.release();

            out.release();
            OperationContext.get().getTaskRun().kill("Problem writing tar entry: " + x);
            return ReturnOption.DONE;
        }

        this.archiveopenflag = true;
    }

    if (in != null)
        try {
            this.tstream.write(in.array(), in.arrayOffset(), in.writerIndex());
        } catch (IOException x) {
            in.release();
            out.release();
            OperationContext.get().getTaskRun().kill("Problem writing tar body: " + x);
            return ReturnOption.DONE;
        }

    if (file.isEof()) {
        try {
            this.tstream.closeArchiveEntry();
        } catch (IOException x) {
            if (in != null)
                in.release();

            out.release();
            OperationContext.get().getTaskRun().kill("Problem closing tar entry: " + x);
            return ReturnOption.DONE;
        }

        this.archiveopenflag = false;
    }

    if (in != null)
        in.release();

    if (file == FileDescriptor.FINAL) {
        blk.setEof(true);

        try {
            this.tstream.close();
        } catch (IOException x) {
            //in.release();
            out.release();
            OperationContext.get().getTaskRun().kill("Problem closing tar stream: " + x);
            return ReturnOption.DONE;
        }

        this.tstream = null;
        this.bstream = null;
    } else
        this.bstream.uninstallBuffer(); // we are done with out forever, don't reference it

    System.out.println("tar sending: " + out.readableBytes());

    ReturnOption v = this.downstream.handle(blk, out);

    if (!this.finalflag)
        return v;

    if (v == ReturnOption.CONTINUE)
        return this.downstream.handle(FileDescriptor.FINAL, null);

    return ReturnOption.DONE;
}