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.pinterest.deployservice.common.TarUtils.java

/**
 * Bundle the given map as a list of files, with key as file name and value as content.
 *///from   w w  w .j a v a2 s  .  c o m
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();/* ww  w .j  a  v  a 2  s .c  o  m*/
    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  ww  w .  j a  v  a2 s .  c o m
 * 
 * @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.github.trask.comet.loadtest.aws.ChefBootstrap.java

private static void addToTarWithBase(File file, TarArchiveOutputStream tarOut, String base) throws IOException {

    String entryName = base + file.getName();
    TarArchiveEntry tarEntry = new TarArchiveEntry(file, entryName);

    tarOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    tarOut.putArchiveEntry(tarEntry);/* ww w . ja  va  2s  .  c o  m*/

    if (file.isFile()) {
        IOUtils.copy(new FileInputStream(file), tarOut);
        tarOut.closeArchiveEntry();
    } else {
        tarOut.closeArchiveEntry();
        File[] children = file.listFiles();
        if (children != null) {
            for (File child : children) {
                addToTarWithBase(child, tarOut, entryName + "/");
            }
        }
    }
}

From source file:com.puppetlabs.geppetto.forge.util.TarUtils.java

public static void pack(File sourceFolder, OutputStream output, FileFilter filter, boolean includeTopFolder,
        String addedTopFolder) throws IOException {
    TarArchiveOutputStream tarOut = new TarArchiveOutputStream(output);
    tarOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
    String absName = sourceFolder.getAbsolutePath();
    int baseNameLen = absName.length() + 1;
    if (includeTopFolder)
        baseNameLen -= (sourceFolder.getName().length() + 1);

    try {/*  w  ww. j  a  v a 2 s  . c o  m*/
        append(sourceFolder, filter, baseNameLen, addedTopFolder, tarOut);
    } finally {
        StreamUtil.close(tarOut);
    }
}

From source file:com.vmware.admiral.closures.util.ClosureUtils.java

private static TarArchiveOutputStream buildTarStream(OutputStream outputStream) throws IOException {
    OutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
    bufferedOutputStream = new GzipCompressorOutputStream(bufferedOutputStream);

    TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream(bufferedOutputStream);
    tarArchiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    return tarArchiveOutputStream;
}

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:ezbake.deployer.utilities.ArtifactHelpers.java

/**
 * Append to the given ArchiveInputStream writing to the given outputstream, the given entries to add.
 * This will duplicate the InputStream to the Output.
 *
 * @param inputStream - archive input to append to
 * @param output      - what to copy the modified archive to
 * @param filesToAdd  - what entries to append.
 *//*ww w . j av a2  s.  c o m*/
private static void appendFilesInTarArchive(ArchiveInputStream inputStream, OutputStream output,
        Iterable<ArtifactDataEntry> filesToAdd) throws DeploymentException {
    ArchiveStreamFactory asf = new ArchiveStreamFactory();

    try {
        HashMap<String, ArtifactDataEntry> newFiles = new HashMap<>();
        for (ArtifactDataEntry entry : filesToAdd) {
            newFiles.put(entry.getEntry().getName(), entry);
        }
        GZIPOutputStream gzs = new GZIPOutputStream(output);
        TarArchiveOutputStream aos = (TarArchiveOutputStream) asf
                .createArchiveOutputStream(ArchiveStreamFactory.TAR, gzs);
        aos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        // copy the existing entries
        ArchiveEntry nextEntry;
        while ((nextEntry = inputStream.getNextEntry()) != null) {
            //If we're passing in the same file, don't copy into the new archive
            if (!newFiles.containsKey(nextEntry.getName())) {
                aos.putArchiveEntry(nextEntry);
                IOUtils.copy(inputStream, aos);
                aos.closeArchiveEntry();
            }
        }

        for (ArtifactDataEntry entry : filesToAdd) {
            aos.putArchiveEntry(entry.getEntry());
            IOUtils.write(entry.getData(), aos);
            aos.closeArchiveEntry();
        }
        aos.finish();
        gzs.finish();
    } catch (ArchiveException | IOException e) {
        log.error(e.getMessage(), e);
        throw new DeploymentException(e.getMessage());
    }
}

From source file:com.fizzed.stork.deploy.Archive.java

static private ArchiveOutputStream newArchiveOutputStream(Path file, String format) throws IOException {
    switch (format) {
    case "tar.gz":
        TarArchiveOutputStream tgzout = new TarArchiveOutputStream(
                new GzipCompressorOutputStream(Files.newOutputStream(file)));
        tgzout.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);
        tgzout.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        return tgzout;
    case "zip":
        return new ZipArchiveOutputStream(Files.newOutputStream(file));
    default:/* www .  ja  v a  2  s.  c  o  m*/
        throw new IOException("Unsupported archive file type (we support .tar.gz and .zip)");
    }
}

From source file:com.linkedin.pinot.common.utils.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  www .j  a va 2  s . com*/
 *          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);

    tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    tOut.putArchiveEntry(tarEntry);

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

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

        File[] children = f.listFiles();

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