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:de.uzk.hki.da.pkg.NativeJavaTarArchiveBuilder.java

/**
 * @param tOut//w  ww.j  a va2  s. co  m
 * @param the actual file that should be added
 * @param base
 * @throws IOException
 */
private void addFileToTar(TarArchiveOutputStream tOut, File file, String base) throws IOException {

    String entryName = base + file.getName();
    logger.debug("addFileToTar: " + entryName);

    TarArchiveEntry entry = (TarArchiveEntry) tOut.createArchiveEntry(file, entryName);
    tOut.putArchiveEntry(entry);

    if (file.isFile()) {

        FileInputStream fileInputStream = new FileInputStream(file);
        IOUtils.copy(fileInputStream, tOut);
        fileInputStream.close();
        tOut.closeArchiveEntry();

    }

    if (file.isDirectory()) {

        tOut.closeArchiveEntry();

        File children[] = file.listFiles();
        if (children == null)
            return;

        for (int i = 0; i < children.length; i++) {
            addFileToTar(tOut, children[i], entryName + "/");
        }
    }
}

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

/**
 * Adds the given file to the archive//from  w  ww .  ja v  a2s.com
 * 
 * @param tOut The tar archive output stream
 * @param file The file to add
 * @param base The relative path to the file inside the archive (without the file name)
 * @throws IOException
 */
private boolean addFileToArchive(TarArchiveOutputStream tOut, File file, String base) throws IOException {

    if (sipBuildingProcess.isAborted())
        return false;

    String entryName = base + file.getName();

    TarArchiveEntry entry = (TarArchiveEntry) tOut.createArchiveEntry(file, entryName);
    tOut.putArchiveEntry(entry);

    if (file.isFile()) {
        FileInputStream fis = new FileInputStream(file);
        IOUtils.copy(fis, tOut);
        tOut.closeArchiveEntry();
        fis.close();

        progressManager.archiveProgress(jobId, FileUtils.sizeOf(file));
    }

    if (file.isDirectory()) {
        tOut.closeArchiveEntry();

        File children[] = file.listFiles();
        if (children == null)
            return true;

        for (int i = 0; i < children.length; i++) {

            if (!addFileToArchive(tOut, children[i], entryName + "/"))
                return false;
        }
    }

    return true;
}

From source file:com.dotcms.publisher.myTest.PushPublisher.java

/**
 * Does the work of compression and going recursive for nested directories
 * <p/>/* w  w w.ja  va  2s. c om*/
 *
 *
 * @param taos The archive
 * @param file The file to add to the archive
   * @param dir The directory that should serve as the parent directory in the archivew
 * @throws IOException
 */
private void addFilesToCompression(TarArchiveOutputStream taos, File file, String dir, String bundleRoot)
        throws IOException {
    if (!file.isHidden()) {
        // Create an entry for the file
        if (!dir.equals("."))
            taos.putArchiveEntry(new TarArchiveEntry(file, dir + File.separator + file.getName()));
        if (file.isFile()) {
            // Add the file to the archive
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            IOUtils.copy(new FileInputStream(file), taos);
            taos.closeArchiveEntry();
            bis.close();
        } else if (file.isDirectory()) {
            //Logger.info(this.getClass(),file.getPath().substring(bundleRoot.length()));
            // close the archive entry
            if (!dir.equals("."))
                taos.closeArchiveEntry();
            // go through all the files in the directory and using recursion, add them to the archive
            for (File childFile : file.listFiles()) {
                addFilesToCompression(taos, childFile, file.getPath().substring(bundleRoot.length()),
                        bundleRoot);
            }
        }
    }

}

From source file:io.syndesis.project.converter.DefaultProjectGenerator.java

private void addTarEntry(TarArchiveOutputStream tos, String path, byte[] content) throws IOException {

    TarArchiveEntry entry = new TarArchiveEntry(path);
    entry.setSize(content.length);/*from  w  w  w.  ja  v a  2s . c om*/
    tos.putArchiveEntry(entry);
    tos.write(content);
    tos.closeArchiveEntry();
}

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./*from ww w  . ja  v  a2s .  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.mobilesorcery.sdk.builder.linux.deb.BuilderUtil.java

/**
 * Recursivly add a directory structure to a tar output stream
 *
 * @param t Tar output stream/*from  ww  w.j  av  a 2  s  .c  o m*/
 * @param r Relative path up to this point
 * @param c Current file
 */
private void doAddFileToTar(TarArchiveOutputStream t, String r, File c) throws IOException {
    // Update relative path
    r += (r.isEmpty() ? "" : (r.endsWith("/") ? "" : "/")) + c.getName();

    // Is it a file?
    if (c.isFile() == true) {
        ArchiveEntry e = t.createArchiveEntry(c, r);
        t.putArchiveEntry(e);
        copyFileToOutputStream(t, c);
        t.closeArchiveEntry();
        return;
    }

    // It's a directory
    for (File f : c.listFiles())
        doAddFileToTar(t, r, f);
}

From source file:com.mobilesorcery.sdk.builder.linux.deb.DebBuilder.java

/**
 *
 * @param os//from  ww  w.j  a v a 2s.  c o  m
 * @throws IOException
 */
private void doCreateControlTarGZip(File f) throws Exception {
    File ftemp;
    FileOutputStream os = new FileOutputStream(f);
    GzipCompressorOutputStream gzos = new GzipCompressorOutputStream(os);
    TarArchiveOutputStream tos = new TarArchiveOutputStream(gzos);

    // Write control file
    ftemp = File.createTempFile(System.currentTimeMillis() + "", "control");
    doWriteControl(ftemp);
    tos.putArchiveEntry(new TarArchiveEntry(ftemp, "./control"));
    BuilderUtil.getInstance().copyFileToOutputStream(tos, ftemp);
    tos.closeArchiveEntry();
    ftemp.delete();

    // Write md5sums
    ftemp = File.createTempFile(System.currentTimeMillis() + "", "md5sums");
    doWriteMD5SumsToFile(ftemp);
    tos.putArchiveEntry(new TarArchiveEntry(ftemp, "./md5sums"));
    BuilderUtil.getInstance().copyFileToOutputStream(tos, ftemp);
    tos.closeArchiveEntry();
    ftemp.delete();

    // Add prerm, postrm, preinst, postinst scripts
    for (Entry<String, String> s : m_scriptMap.entrySet()) {
        TarArchiveEntry e = new TarArchiveEntry("./" + s.getKey());
        e.setSize(s.getValue().length());
        tos.putArchiveEntry(e);
        BuilderUtil.getInstance().copyStringToOutputStream(tos, s.getValue());
        tos.closeArchiveEntry();
    }

    // Done
    tos.close();
    gzos.close();
    os.close();
}

From source file:com.netflix.spinnaker.halyard.core.registry.v1.LocalDiskProfileReader.java

public InputStream readArchiveProfileFrom(Path profilePath) throws IOException {

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    TarArchiveOutputStream tarArchive = new TarArchiveOutputStream(os);

    ArrayList<Path> filePathsToAdd = java.nio.file.Files
            .walk(profilePath, Integer.MAX_VALUE, FileVisitOption.FOLLOW_LINKS)
            .filter(path -> path.toFile().isFile()).collect(Collectors.toCollection(ArrayList::new));

    for (Path path : filePathsToAdd) {
        TarArchiveEntry tarEntry = new TarArchiveEntry(path.toFile(), profilePath.relativize(path).toString());
        tarArchive.putArchiveEntry(tarEntry);
        IOUtils.copy(Files.newInputStream(path), tarArchive);
        tarArchive.closeArchiveEntry();/*  ww  w.j  a v a  2  s  .  co  m*/
    }

    tarArchive.finish();
    tarArchive.close();

    return new ByteArrayInputStream(os.toByteArray());
}

From source file:com.puppetlabs.geppetto.forge.v2.api.it.ReleaseTestCreate.java

private void putEntry(TarArchiveOutputStream stream, ModuleName moduleName, Version version, String name,
        String content) throws IOException {
    StringBuilder fullName = new StringBuilder();
    moduleName.toString(fullName);//from   ww  w. j  a va 2 s  .  com
    fullName.append('-');
    version.toString(fullName);
    fullName.append('/');
    fullName.append(name);
    TarArchiveEntry entry = new TarArchiveEntry(fullName.toString());
    byte[] img = content.getBytes(Charsets.UTF_8);
    entry.setSize(img.length);
    stream.putArchiveEntry(entry);
    stream.write(img);
    stream.closeArchiveEntry();
}

From source file:com.moss.simpledeb.core.DebWriter.java

private byte[] buildGzipTar(List<ArchivePath> paths) throws Exception {

    byte[] tarData;
    {/*ww w.j  a v a  2s . c  o m*/
        ByteArrayOutputStream tarOut = new ByteArrayOutputStream();
        TarArchiveOutputStream tar = new TarArchiveOutputStream(tarOut);

        Set<String> writtenPaths = new HashSet<String>();
        for (ArchivePath path : paths) {
            String name = path.entry().getName();

            if (writtenPaths.contains(name)) {
                throw new RuntimeException("Duplicate archive entry: " + name);
            }

            writtenPaths.add(name);

            tar.putArchiveEntry(path.entry());

            if (!path.entry().isDirectory()) {
                InputStream in = path.read();
                byte[] buffer = new byte[1024 * 10];
                for (int numRead = in.read(buffer); numRead != -1; numRead = in.read(buffer)) {
                    tar.write(buffer, 0, numRead);
                }
                in.close();
            }

            tar.closeArchiveEntry();
        }

        tar.close();
        tarData = tarOut.toByteArray();
    }

    byte[] gzipData;
    {
        ByteArrayOutputStream gzipOut = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(gzipOut);
        gzip.write(tarData);
        gzip.close();

        gzipData = gzipOut.toByteArray();
    }

    return gzipData;
}