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

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

Introduction

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

Prototype

public void putArchiveEntry(ArchiveEntry archiveEntry) throws IOException 

Source Link

Document

Put an entry on the output stream.

Usage

From source file:com.flurry.proguard.UploadMapping.java

/**
 * Create a gzipped tar archive containing the ProGuard/Native mapping files
 *
 * @param files array of mapping.txt files
 * @return the tar-gzipped archive// www .  j a  v a2s  . c  o  m
 */
private static File createArchive(List<File> files, String uuid) {
    try {
        File tarZippedFile = File.createTempFile("tar-zipped-file", ".tgz");
        TarArchiveOutputStream taos = new TarArchiveOutputStream(
                new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(tarZippedFile))));
        for (File file : files) {
            taos.putArchiveEntry(new TarArchiveEntry(file,
                    (uuid != null && !uuid.isEmpty() ? uuid : UUID.randomUUID()) + ".txt"));
            IOUtils.copy(new FileInputStream(file), taos);
            taos.closeArchiveEntry();
        }
        taos.finish();
        taos.close();
        return tarZippedFile;
    } catch (IOException e) {
        failWithError("IO Exception while trying to tar and zip the file.", e);
        return null;
    }
}

From source file:ezbake.protect.ezca.EzCABootstrap.java

protected static void addTarArchiveEntry(final TarArchiveOutputStream tao, String name, byte[] data) {
    TarArchiveEntry tae = new TarArchiveEntry(name);
    try {/*from  ww w .jav a2s .  c  om*/
        tae.setSize(data.length);
        tao.putArchiveEntry(tae);
        tao.write(data);
        tao.closeArchiveEntry();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

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.
 *///from  w w w . jav a 2  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.st.maven.debian.DebianPackageMojo.java

private static void writeDirectory(TarArchiveOutputStream tar, String dirName) throws MojoExecutionException {
    try {//from w ww  .  j a v a 2  s . c  o m
        if (!dirName.endsWith("/")) {
            dirName = dirName + "/";
        }
        TarArchiveEntry curEntry = new TarArchiveEntry(dirName);
        tar.putArchiveEntry(curEntry);
        tar.closeArchiveEntry();
    } catch (Exception e) {
        throw new MojoExecutionException("unable to add directory: " + dirName, e);
    }
}

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 a  2s  .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.fizzed.stork.assembly.AssemblyUtils.java

static public void addFileToTGZStream(TarArchiveOutputStream tgzout, File f, String base, boolean appendName)
        throws IOException {
    //File f = new File(path);
    String entryName = base;/* w  w  w.  j a  v a 2 s.c om*/
    if (appendName) {
        if (!entryName.equals("")) {
            if (!entryName.endsWith("/")) {
                entryName += "/" + f.getName();
            } else {
                entryName += f.getName();
            }
        } else {
            entryName += f.getName();
        }
    }
    TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);

    if (f.isFile()) {
        if (f.canExecute()) {
            // -rwxr-xr-x
            tarEntry.setMode(493);
        } else {
            // keep default mode
        }
    }

    tgzout.putArchiveEntry(tarEntry);

    if (f.isFile()) {
        try (FileInputStream in = new FileInputStream(f)) {
            IOUtils.copy(in, tgzout);
        }
        tgzout.closeArchiveEntry();
    } else {
        tgzout.closeArchiveEntry();
        File[] children = f.listFiles();
        if (children != null) {
            for (File child : children) {
                logger.info(" adding: " + entryName + "/" + child.getName());
                addFileToTGZStream(tgzout, child, entryName + "/", true);
            }
        }
    }
}

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

static public void addFileToTGZStream(TarArchiveOutputStream tgzout, File f, String base, boolean appendName)
        throws IOException {
    //File f = new File(path);
    String entryName = base;//from www .  j  ava2 s  .c o m
    if (appendName) {
        if (!entryName.equals("")) {
            if (!entryName.endsWith("/")) {
                entryName += "/" + f.getName();
            } else {
                entryName += f.getName();
            }
        } else {
            entryName += f.getName();
        }
    }
    TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);

    if (f.isFile()) {
        if (f.canExecute()) {
            // -rwxr-xr-x
            tarEntry.setMode(493);
        } else {
            // keep default mode
        }
    }

    tgzout.putArchiveEntry(tarEntry);

    if (f.isFile()) {
        FileInputStream in = new FileInputStream(f);
        IOUtils.copy(in, tgzout);
        in.close();
        tgzout.closeArchiveEntry();

    } else {
        tgzout.closeArchiveEntry();
        File[] children = f.listFiles();
        if (children != null) {
            for (File child : children) {
                logger.info(" adding: " + entryName + "/" + child.getName());
                addFileToTGZStream(tgzout, child, entryName + "/", true);
            }
        }
    }
}

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

private static void append(File file, FileFilter filter, int baseNameLen, String addedTopFolder,
        TarArchiveOutputStream tarOut) throws IOException {

    String name = file.getAbsolutePath();
    if (name.length() <= baseNameLen)
        name = "";
    else/*from   w w  w .j ava  2  s . com*/
        name = name.substring(baseNameLen);
    if (File.separatorChar == '\\')
        name = name.replace('\\', '/');
    if (addedTopFolder != null)
        name = addedTopFolder + '/' + name;

    if (FileUtils.isSymlink(file)) {
        String linkTarget = FileUtils.readSymbolicLink(file);
        if (linkTarget != null) {
            TarArchiveEntry entry = new TarArchiveEntry(name, TarConstants.LF_SYMLINK);
            entry.setName(name);
            entry.setLinkName(linkTarget);
            tarOut.putArchiveEntry(entry);
        }
        return;
    }

    ArchiveEntry entry = tarOut.createArchiveEntry(file, name);
    tarOut.putArchiveEntry(entry);
    File[] children = file.listFiles(filter);
    if (children != null) {
        tarOut.closeArchiveEntry();
        // This is a directory. Append its children
        for (File child : children)
            append(child, filter, baseNameLen, addedTopFolder, tarOut);
        return;
    }

    // Append the content of the file
    InputStream input = new FileInputStream(file);
    try {
        StreamUtil.copy(input, tarOut);
        tarOut.closeArchiveEntry();
    } finally {
        StreamUtil.close(input);
    }
}

From source file:frameworks.Masken.java

public static void addFileToTarGz(TarArchiveOutputStream tOut, String path, String base) throws IOException

{
    File f = new File(path);
    System.out.println(f.exists());
    //tmp wegschneiden
    if (base.startsWith("tmp")) {
        base = base.substring(4, base.length());
    }//w w  w . jav a  2  s.c  o m
    String entryName = base + f.getName();

    TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
    tarEntry.setSize(f.length());
    tOut.putArchiveEntry(tarEntry);

    if (f.isFile()) {
        IOUtils.copy(new FileInputStream(f), tOut);
        //  FileInputStream in = new FileInputStream(f);
        //  IOUtils.copy(in, tOut);
        // in.close();

    } else {
        tOut.closeArchiveEntry();

        File[] children = f.listFiles();
        if (children != null) {
            for (File child : children) {
                System.out.println(child.getName());
                if (!child.getName().startsWith("screen.")) {
                    addFileToTarGz(tOut, child.getAbsolutePath(), entryName + "/");

                }
            }

        }
    }
}

From source file:io.syndesis.project.converter.visitor.GeneratorContext.java

default void addTarEntry(String path, byte[] content) throws IOException {
    TarArchiveOutputStream tos = getTarArchiveOutputStream();
    TarArchiveEntry entry = new TarArchiveEntry(path);
    entry.setSize(content.length);/*w ww  .j a va  2s. co m*/
    tos.putArchiveEntry(entry);
    tos.write(content);
    tos.closeArchiveEntry();
}