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:org.hyperledger.fabric.sdk.helper.SDKUtil.java

/**
 * Compress the given directory src to target tar.gz file
 * @param src The source directory// ww w.j  av a2 s  . c  o m
 * @param target The target tar.gz file
 * @throws IOException
 */
public static void generateTarGz(String src, String target) throws IOException {
    File sourceDirectory = new File(src);
    File destinationArchive = new File(target);

    String sourcePath = sourceDirectory.getAbsolutePath();
    FileOutputStream destinationOutputStream = new FileOutputStream(destinationArchive);

    TarArchiveOutputStream archiveOutputStream = new TarArchiveOutputStream(
            new GzipCompressorOutputStream(new BufferedOutputStream(destinationOutputStream)));
    archiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

    try {
        Collection<File> childrenFiles = org.apache.commons.io.FileUtils.listFiles(sourceDirectory, null, true);
        childrenFiles.remove(destinationArchive);

        ArchiveEntry archiveEntry;
        FileInputStream fileInputStream;
        for (File childFile : childrenFiles) {
            String childPath = childFile.getAbsolutePath();
            String relativePath = childPath.substring((sourcePath.length() + 1), childPath.length());

            relativePath = FilenameUtils.separatorsToUnix(relativePath);
            archiveEntry = new TarArchiveEntry(childFile, relativePath);
            fileInputStream = new FileInputStream(childFile);
            archiveOutputStream.putArchiveEntry(archiveEntry);

            try {
                IOUtils.copy(fileInputStream, archiveOutputStream);
            } finally {
                IOUtils.closeQuietly(fileInputStream);
                archiveOutputStream.closeArchiveEntry();
            }
        }
    } finally {
        IOUtils.closeQuietly(archiveOutputStream);
    }
}

From source file:org.hyperledger.fabric.sdk.helper.Utils.java

/**
 * Compress the contents of given directory using Tar and Gzip to an in-memory byte array.
 *
 * @param sourceDirectory  the source directory.
 * @param pathPrefix       a path to be prepended to every file name in the .tar.gz output, or {@code null} if no prefix is required.
 * @param chaincodeMetaInf// w  w  w  .  j a v  a  2s. c om
 * @return the compressed directory contents.
 * @throws IOException
 */
public static byte[] generateTarGz(File sourceDirectory, String pathPrefix, File chaincodeMetaInf)
        throws IOException {
    logger.trace(format("generateTarGz: sourceDirectory: %s, pathPrefix: %s, chaincodeMetaInf: %s",
            sourceDirectory == null ? "null" : sourceDirectory.getAbsolutePath(), pathPrefix,
            chaincodeMetaInf == null ? "null" : chaincodeMetaInf.getAbsolutePath()));

    ByteArrayOutputStream bos = new ByteArrayOutputStream(500000);

    String sourcePath = sourceDirectory.getAbsolutePath();

    TarArchiveOutputStream archiveOutputStream = new TarArchiveOutputStream(
            new GzipCompressorOutputStream(bos));
    archiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

    try {
        Collection<File> childrenFiles = org.apache.commons.io.FileUtils.listFiles(sourceDirectory, null, true);

        ArchiveEntry archiveEntry;
        FileInputStream fileInputStream;
        for (File childFile : childrenFiles) {
            String childPath = childFile.getAbsolutePath();
            String relativePath = childPath.substring((sourcePath.length() + 1), childPath.length());

            if (pathPrefix != null) {
                relativePath = Utils.combinePaths(pathPrefix, relativePath);
            }

            relativePath = FilenameUtils.separatorsToUnix(relativePath);

            if (TRACE_ENABED) {
                logger.trace(format("generateTarGz: Adding '%s' entry from source '%s' to archive.",
                        relativePath, childFile.getAbsolutePath()));
            }

            archiveEntry = new TarArchiveEntry(childFile, relativePath);
            fileInputStream = new FileInputStream(childFile);
            archiveOutputStream.putArchiveEntry(archiveEntry);

            try {
                IOUtils.copy(fileInputStream, archiveOutputStream);
            } finally {
                IOUtils.closeQuietly(fileInputStream);
                archiveOutputStream.closeArchiveEntry();
            }

        }

        if (null != chaincodeMetaInf) {
            childrenFiles = org.apache.commons.io.FileUtils.listFiles(chaincodeMetaInf, null, true);

            final URI metabase = chaincodeMetaInf.toURI();

            for (File childFile : childrenFiles) {

                final String relativePath = Paths
                        .get("META-INF", metabase.relativize(childFile.toURI()).getPath()).toString();

                if (TRACE_ENABED) {
                    logger.trace(format("generateTarGz: Adding '%s' entry from source '%s' to archive.",
                            relativePath, childFile.getAbsolutePath()));
                }

                archiveEntry = new TarArchiveEntry(childFile, relativePath);
                fileInputStream = new FileInputStream(childFile);
                archiveOutputStream.putArchiveEntry(archiveEntry);

                try {
                    IOUtils.copy(fileInputStream, archiveOutputStream);
                } finally {
                    IOUtils.closeQuietly(fileInputStream);
                    archiveOutputStream.closeArchiveEntry();
                }

            }

        }
    } finally {
        IOUtils.closeQuietly(archiveOutputStream);
    }

    return bos.toByteArray();
}

From source file:org.hyperledger.fabric.sdkintegration.Util.java

/**
 * Generate a targz inputstream from source folder.
 *
 * @param src        Source location/*from  w  ww .  j a v a2  s . co  m*/
 * @param pathPrefix prefix to add to the all files found.
 * @return return inputstream.
 * @throws IOException
 */
public static InputStream generateTarGzInputStream(File src, String pathPrefix) throws IOException {
    File sourceDirectory = src;

    ByteArrayOutputStream bos = new ByteArrayOutputStream(500000);

    String sourcePath = sourceDirectory.getAbsolutePath();

    TarArchiveOutputStream archiveOutputStream = new TarArchiveOutputStream(
            new GzipCompressorOutputStream(new BufferedOutputStream(bos)));
    archiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

    try {
        Collection<File> childrenFiles = org.apache.commons.io.FileUtils.listFiles(sourceDirectory, null, true);

        ArchiveEntry archiveEntry;
        FileInputStream fileInputStream;
        for (File childFile : childrenFiles) {
            String childPath = childFile.getAbsolutePath();
            String relativePath = childPath.substring((sourcePath.length() + 1), childPath.length());

            if (pathPrefix != null) {
                relativePath = Utils.combinePaths(pathPrefix, relativePath);
            }

            relativePath = FilenameUtils.separatorsToUnix(relativePath);

            archiveEntry = new TarArchiveEntry(childFile, relativePath);
            fileInputStream = new FileInputStream(childFile);
            archiveOutputStream.putArchiveEntry(archiveEntry);

            try {
                IOUtils.copy(fileInputStream, archiveOutputStream);
            } finally {
                IOUtils.closeQuietly(fileInputStream);
                archiveOutputStream.closeArchiveEntry();
            }
        }
    } finally {
        IOUtils.closeQuietly(archiveOutputStream);
    }

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

From source file:org.icgc.dcc.download.client.io.ArchiveOutputStream.java

@SneakyThrows
private static void addArchiveEntry(TarArchiveOutputStream os, String filename, long fileSize) {
    TarArchiveEntry entry = new TarArchiveEntry(filename);
    entry.setSize(fileSize);//from w w w  . ja v  a  2s.  c o  m
    os.putArchiveEntry(entry);
}

From source file:org.jclouds.docker.compute.features.internal.Archives.java

public static File tar(File baseDir, String archivePath) throws IOException {
    // Check that the directory is a directory, and get its contents
    checkArgument(baseDir.isDirectory(), "%s is not a directory", baseDir);
    File[] files = baseDir.listFiles();
    File tarFile = new File(archivePath);

    String token = getLast(Splitter.on("/").split(archivePath.substring(0, archivePath.lastIndexOf("/"))));

    byte[] buf = new byte[1024];
    int len;// www.j  av  a 2s  .c  o m
    TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(tarFile));
    tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    for (File file : files) {
        TarArchiveEntry tarEntry = new TarArchiveEntry(file);
        tarEntry.setName("/" + getLast(Splitter.on(token).split(file.toString())));
        tos.putArchiveEntry(tarEntry);
        if (!file.isDirectory()) {
            FileInputStream fin = new FileInputStream(file);
            BufferedInputStream in = new BufferedInputStream(fin);
            while ((len = in.read(buf)) != -1) {
                tos.write(buf, 0, len);
            }
            in.close();
        }
        tos.closeArchiveEntry();
    }
    tos.close();
    return tarFile;
}

From source file:org.jclouds.docker.compute.features.internal.Archives.java

public static File tar(File baseDir, File tarFile) throws IOException {
    // Check that the directory is a directory, and get its contents
    checkArgument(baseDir.isDirectory(), "%s is not a directory", baseDir);
    File[] files = baseDir.listFiles();

    String token = getLast(Splitter.on("/").split(baseDir.getAbsolutePath()));

    byte[] buf = new byte[1024];
    int len;/*from   w  w w . jav  a  2s.  c o  m*/
    TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(tarFile));
    tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    for (File file : files) {
        TarArchiveEntry tarEntry = new TarArchiveEntry(file);
        tarEntry.setName("/" + getLast(Splitter.on(token).split(file.toString())));
        tos.putArchiveEntry(tarEntry);
        if (!file.isDirectory()) {
            FileInputStream fin = new FileInputStream(file);
            BufferedInputStream in = new BufferedInputStream(fin);
            while ((len = in.read(buf)) != -1) {
                tos.write(buf, 0, len);
            }
            in.close();
        }
        tos.closeArchiveEntry();
    }
    tos.close();
    return tarFile;
}

From source file:org.jclouds.docker.features.internal.Archives.java

public static File tar(File baseDir, File tarFile) throws IOException {
    // Check that the directory is a directory, and get its contents
    checkArgument(baseDir.isDirectory(), "%s is not a directory", baseDir);
    File[] files = baseDir.listFiles();
    String token = getLast(Splitter.on("/").split(baseDir.getAbsolutePath()));
    TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(tarFile));
    tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    try {/*  w  w  w. j  a v  a  2  s  .  c o m*/
        for (File file : files) {
            TarArchiveEntry tarEntry = new TarArchiveEntry(file);
            tarEntry.setName("/" + getLast(Splitter.on(token).split(file.toString())));
            tos.putArchiveEntry(tarEntry);
            if (!file.isDirectory()) {
                Files.asByteSource(file).copyTo(tos);
            }
            tos.closeArchiveEntry();
        }
    } finally {
        tos.close();
    }
    return tarFile;
}

From source file:org.jenkinsci.plugins.os_ci.utils.CompressUtils.java

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.putArchiveEntry(tarEntry);

    if (f.isFile()) {
        IOUtils.copy(new FileInputStream(f), tOut);
        tOut.closeArchiveEntry();//  w w w  . ja  v a  2 s . co m
    } else {
        tOut.closeArchiveEntry();
        File[] children = f.listFiles();
        if (children != null) {
            for (File child : children) {
                addFileToTarGz(tOut, child.getAbsolutePath(), entryName + "/");
            }
        }
    }
}

From source file:org.kitesdk.cli.commands.TestTarImportCommand.java

private static void writeToTarFile(TarArchiveOutputStream tos, String name, String content) throws IOException {
    TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(name);
    if (null != content) {
        tarArchiveEntry.setSize(content.length());
    }/*from   w  ww .  j ava  2 s .  c  o  m*/
    tarArchiveEntry.setModTime(System.currentTimeMillis());
    tos.putArchiveEntry(tarArchiveEntry);
    if (null != content) {
        byte[] buf = content.getBytes();
        tos.write(buf, 0, content.length());
        tos.flush();
    }
    tos.closeArchiveEntry();
}

From source file:org.ngrinder.common.util.CompressionUtils.java

/**
 * Add a folder into tar.//from w ww.  j a v  a 2 s  .co m
 *
 * @param tarStream TarArchive outputStream
 * @param path      path to append
 * @throws IOException thrown when having IO problem.
 */
public static void addFolderToTar(TarArchiveOutputStream tarStream, String path) throws IOException {
    TarArchiveEntry archiveEntry = new TarArchiveEntry(path);
    archiveEntry.setMode(TarArchiveEntry.DEFAULT_DIR_MODE);
    tarStream.putArchiveEntry(archiveEntry);
    tarStream.closeArchiveEntry();
}