Search in sources :

Example 1 with GzipCompressorOutputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream in project pinot by linkedin.

the class TarGzCompressionUtils method createTarGzOfDirectory.

public static String createTarGzOfDirectory(String directoryPath, String tarGzPath, String entryPrefix) throws IOException {
    FileOutputStream fOut = null;
    BufferedOutputStream bOut = null;
    GzipCompressorOutputStream gzOut = null;
    TarArchiveOutputStream tOut = null;
    if (!tarGzPath.endsWith(TAR_GZ_FILE_EXTENTION)) {
        tarGzPath = tarGzPath + TAR_GZ_FILE_EXTENTION;
    }
    try {
        fOut = new FileOutputStream(new File(tarGzPath));
        bOut = new BufferedOutputStream(fOut);
        gzOut = new GzipCompressorOutputStream(bOut);
        tOut = new TarArchiveOutputStream(gzOut);
        tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        addFileToTarGz(tOut, directoryPath, entryPrefix);
    } catch (IOException e) {
        LOGGER.error("Failed to create tar.gz file for {} at path: {}", directoryPath, tarGzPath, e);
        Utils.rethrowException(e);
    } finally {
        if (tOut != null) {
            tOut.finish();
            tOut.close();
        }
        if (gzOut != null) {
            gzOut.close();
        }
        if (bOut != null) {
            bOut.close();
        }
        if (fOut != null) {
            fOut.close();
        }
    }
    return tarGzPath;
}
Also used : GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) FileOutputStream(java.io.FileOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) File(java.io.File)

Example 2 with GzipCompressorOutputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream 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();
    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();
}
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) FileInputStream(java.io.FileInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 3 with GzipCompressorOutputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream 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 4 with GzipCompressorOutputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream 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_GNU);
        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 5 with GzipCompressorOutputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream in project neo4j by neo4j.

the class LoaderTest method shouldGiveAClearErrorMessageIfTheArchiveIsNotInTarFormat.

@Test
void shouldGiveAClearErrorMessageIfTheArchiveIsNotInTarFormat() throws IOException {
    Path archive = testDirectory.file("the-archive.dump");
    try (GzipCompressorOutputStream compressor = new GzipCompressorOutputStream(Files.newOutputStream(archive))) {
        byte[] bytes = new byte[1000];
        new Random().nextBytes(bytes);
        compressor.write(bytes);
    }
    deleteLayoutFolders(databaseLayout);
    IncorrectFormat incorrectFormat = assertThrows(IncorrectFormat.class, () -> new Loader().load(archive, databaseLayout));
    assertEquals(archive.toString(), incorrectFormat.getMessage());
}
Also used : Path(java.nio.file.Path) GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) Random(java.util.Random) Test(org.junit.jupiter.api.Test)

Aggregations

GzipCompressorOutputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream)29 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)23 File (java.io.File)15 Path (java.nio.file.Path)12 FileOutputStream (java.io.FileOutputStream)11 BufferedOutputStream (java.io.BufferedOutputStream)8 OutputStream (java.io.OutputStream)7 FileInputStream (java.io.FileInputStream)6 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)6 ZipOutputStream (java.util.zip.ZipOutputStream)5 IOException (java.io.IOException)4 ArchiveOutputStream (org.apache.commons.compress.archivers.ArchiveOutputStream)4 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)3 Test (org.junit.Test)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ZipEntry (java.util.zip.ZipEntry)2 Test (org.junit.jupiter.api.Test)2 JournalEntry (alluxio.proto.journal.Journal.JournalEntry)1 CloseableIterator (alluxio.resource.CloseableIterator)1 ByteString (com.google.protobuf.ByteString)1