Search in sources :

Example 46 with TarArchiveOutputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project mycore by MyCoRe-Org.

the class MCRTransferPackagePacker method buildTar.

/**
 * Builds a *.tar archive at the path for the given transfer package.
 *
 * @param pathToTar where to store the *.tar
 * @param transferPackage the package to zip
 * @throws IOException something went wrong while packing
 */
private void buildTar(Path pathToTar, MCRTransferPackage transferPackage) throws IOException {
    FileOutputStream fos = new FileOutputStream(pathToTar.toFile());
    try (TarArchiveOutputStream tarOutStream = new TarArchiveOutputStream(fos)) {
        for (Entry<String, MCRContent> contentEntry : transferPackage.getContent().entrySet()) {
            String filePath = contentEntry.getKey();
            byte[] data = contentEntry.getValue().asByteArray();
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Adding '{}' to {}", filePath, pathToTar.toAbsolutePath());
            }
            writeFile(tarOutStream, filePath, data);
            writeMD5(tarOutStream, filePath + ".md5", data);
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) MCRContent(org.mycore.common.content.MCRContent)

Example 47 with TarArchiveOutputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project tycho by eclipse.

the class TarGzArchiver method createArchive.

public void createArchive() throws IOException {
    validate();
    log.info("Building tar: " + destFile);
    TarArchiveOutputStream tarStream = null;
    try {
        destFile.getAbsoluteFile().getParentFile().mkdirs();
        GzipCompressorOutputStream gzipStream = new GzipCompressorOutputStream(new BufferedOutputStream(new FileOutputStream(destFile)));
        tarStream = new TarArchiveOutputStream(gzipStream, "UTF-8");
        // allow "long" file paths (> 100 chars)
        tarStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
        tarStream.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
        for (File sourceDir : sourceDirs) {
            for (File child : sourceDir.listFiles()) {
                addToTarRecursively(sourceDir, child, tarStream);
            }
        }
    } finally {
        if (tarStream != null) {
            tarStream.close();
        }
    }
}
Also used : GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) FileOutputStream(java.io.FileOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) File(java.io.File)

Example 48 with TarArchiveOutputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project fabric-sdk-java by hyperledger.

the class Util method generateTarGzInputStream.

/**
 * Generate a targz inputstream from source folder.
 *
 * @param src        Source location
 * @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());
}
Also used : GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) FileInputStream(java.io.FileInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 49 with TarArchiveOutputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project fabric-sdk-java by hyperledger.

the class LifecycleChaincodePackage method fromSource.

/**
 * @param label                    Any name you like to identify this package.
 * @param chaincodeSource          Chaincode source directory.
 * @param chaincodeType            Chaincode type GO, JAVA.
 * @param chaincodePath            Only valid for GO LANG chaincode. Otherwise, null.
 * @param chaincodeMetaInfLocation MetaInf location. Can be null.
 * @return
 * @throws IOException
 */
public static LifecycleChaincodePackage fromSource(String label, Path chaincodeSource, TransactionRequest.Type chaincodeType, String chaincodePath, Path chaincodeMetaInfLocation) throws IOException, InvalidArgumentException {
    if (Utils.isNullOrEmpty(label)) {
        throw new InvalidArgumentException("The parameter label may not be null or empty.");
    }
    if (null == chaincodeSource) {
        throw new InvalidArgumentException(" The parameter chaincodeSource may not be null.");
    }
    if (null == chaincodeType) {
        throw new InvalidArgumentException(" The parameter chaincodeType may not be null.");
    }
    byte[] mataDataBytes = generatePackageMataDataBytes(label, chaincodePath, chaincodeType);
    byte[] dataBytes = generatePackageDataBytes(chaincodeSource, chaincodeMetaInfLocation, chaincodeType, chaincodePath);
    ByteArrayOutputStream bos = new ByteArrayOutputStream(500000);
    // String sourcePath = sourceDirectory.getAbsolutePath();
    TarArchiveOutputStream archiveOutputStream = new TarArchiveOutputStream(new GzipCompressorOutputStream(bos));
    archiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    TarArchiveEntry archiveEntry = new TarArchiveEntry("metadata.json");
    archiveEntry.setMode(0100644);
    archiveEntry.setSize(mataDataBytes.length);
    archiveOutputStream.putArchiveEntry(archiveEntry);
    archiveOutputStream.write(mataDataBytes);
    archiveOutputStream.closeArchiveEntry();
    archiveEntry = new TarArchiveEntry("code.tar.gz");
    archiveEntry.setMode(0100644);
    archiveEntry.setSize(dataBytes.length);
    archiveOutputStream.putArchiveEntry(archiveEntry);
    archiveOutputStream.write(dataBytes);
    archiveOutputStream.closeArchiveEntry();
    archiveOutputStream.close();
    return fromBytes(bos.toByteArray());
}
Also used : GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 50 with TarArchiveOutputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project fabric-sdk-java by hyperledger.

the class Utils method generateTarGz.

/**
 * 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
 * @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();
    try (TarArchiveOutputStream archiveOutputStream = new TarArchiveOutputStream(new GzipCompressorOutputStream(bos))) {
        archiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        Collection<File> childrenFiles = org.apache.commons.io.FileUtils.listFiles(sourceDirectory, null, true);
        ArchiveEntry archiveEntry;
        for (File childFile : childrenFiles) {
            String childPath = childFile.getAbsolutePath();
            String relativePath = childPath.substring((sourcePath.length() + 1));
            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);
            archiveOutputStream.putArchiveEntry(archiveEntry);
            try (FileInputStream fileInputStream = new FileInputStream(childFile)) {
                IOUtils.copy(fileInputStream, archiveOutputStream);
            } finally {
                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);
                archiveOutputStream.putArchiveEntry(archiveEntry);
                try (FileInputStream fileInputStream = new FileInputStream(childFile)) {
                    IOUtils.copy(fileInputStream, archiveOutputStream);
                } finally {
                    archiveOutputStream.closeArchiveEntry();
                }
            }
        }
    }
    return bos.toByteArray();
}
Also used : GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteString(com.google.protobuf.ByteString) Hex.encodeHexString(org.apache.commons.codec.binary.Hex.encodeHexString) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) File(java.io.File) URI(java.net.URI) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) FileInputStream(java.io.FileInputStream)

Aggregations

TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)101 File (java.io.File)42 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)42 FileOutputStream (java.io.FileOutputStream)37 GzipCompressorOutputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream)30 BufferedOutputStream (java.io.BufferedOutputStream)22 IOException (java.io.IOException)21 ByteArrayOutputStream (java.io.ByteArrayOutputStream)20 FileInputStream (java.io.FileInputStream)19 Path (java.nio.file.Path)17 GZIPOutputStream (java.util.zip.GZIPOutputStream)17 ByteArrayInputStream (java.io.ByteArrayInputStream)15 OutputStream (java.io.OutputStream)13 Test (org.junit.Test)12 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)11 ArchiveOutputStream (org.apache.commons.compress.archivers.ArchiveOutputStream)11 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)9 ZipArchiveOutputStream (org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream)9 ImageArchiveManifest (io.fabric8.maven.docker.model.ImageArchiveManifest)8 InputStream (java.io.InputStream)7