Search in sources :

Example 21 with GzipCompressorOutputStream

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

the class FileUtil method tar.

/**
 * Recursively tar file
 *
 * @param inputPath file path can be directory
 * @param outputPath where to put the archived file
 * @param childrenOnly if inputPath is directory and if childrenOnly is true, the archive will contain all of its children, else the archive contains unique
 *            entry which is the inputPath itself
 * @param gZipped compress with gzip algorithm
 */
public static void tar(Path inputPath, Path outputPath, boolean gZipped, boolean childrenOnly) throws IOException {
    if (!Files.exists(inputPath)) {
        throw new FileNotFoundException("File not found " + inputPath);
    }
    touch(outputPath);
    OutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(outputPath));
    if (gZipped) {
        outputStream = new GzipCompressorOutputStream(outputStream);
    }
    TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream(outputStream);
    tarArchiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
    try {
        if (!Files.isDirectory(inputPath)) {
            putTarEntry(tarArchiveOutputStream, new TarArchiveEntry(inputPath.getFileName().toString()), inputPath);
        } else {
            Path sourcePath = inputPath;
            if (!childrenOnly) {
                // In order to have the dossier as the root entry
                sourcePath = inputPath.getParent();
            }
            Files.walkFileTree(inputPath, new TarDirWalker(sourcePath, tarArchiveOutputStream));
        }
        tarArchiveOutputStream.flush();
    } finally {
        Closeables.close(tarArchiveOutputStream, true);
    }
}
Also used : GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 22 with GzipCompressorOutputStream

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

the class UnstructuredStorageWriterUtil method writeToStream.

public static void writeToStream(RecordReceiver lineReceiver, OutputStream outputStream, Configuration config, String context, TaskPluginCollector taskPluginCollector) {
    String encoding = config.getString(Key.ENCODING, Constant.DEFAULT_ENCODING);
    // handle blank encoding
    if (StringUtils.isBlank(encoding)) {
        LOG.warn(String.format("您配置的encoding为[%s], 使用默认值[%s]", encoding, Constant.DEFAULT_ENCODING));
        encoding = Constant.DEFAULT_ENCODING;
    }
    String compress = config.getString(Key.COMPRESS);
    BufferedWriter writer = null;
    // compress logic
    try {
        if (null == compress) {
            writer = new BufferedWriter(new OutputStreamWriter(outputStream, encoding));
        } else {
            // TODO more compress
            if ("gzip".equalsIgnoreCase(compress)) {
                CompressorOutputStream compressorOutputStream = new GzipCompressorOutputStream(outputStream);
                writer = new BufferedWriter(new OutputStreamWriter(compressorOutputStream, encoding));
            } else if ("bzip2".equalsIgnoreCase(compress)) {
                CompressorOutputStream compressorOutputStream = new BZip2CompressorOutputStream(outputStream);
                writer = new BufferedWriter(new OutputStreamWriter(compressorOutputStream, encoding));
            } else {
                throw DataXException.asDataXException(UnstructuredStorageWriterErrorCode.ILLEGAL_VALUE, String.format("仅支持 gzip, bzip2 文件压缩格式 , 不支持您配置的文件压缩格式: [%s]", compress));
            }
        }
        UnstructuredStorageWriterUtil.doWriteToStream(lineReceiver, writer, context, config, taskPluginCollector);
    } catch (UnsupportedEncodingException uee) {
        throw DataXException.asDataXException(UnstructuredStorageWriterErrorCode.Write_FILE_WITH_CHARSET_ERROR, String.format("不支持的编码格式 : [%s]", encoding), uee);
    } catch (NullPointerException e) {
        throw DataXException.asDataXException(UnstructuredStorageWriterErrorCode.RUNTIME_EXCEPTION, "运行时错误, 请联系我们", e);
    } catch (IOException e) {
        throw DataXException.asDataXException(UnstructuredStorageWriterErrorCode.Write_FILE_IO_ERROR, String.format("流写入错误 : [%s]", context), e);
    } finally {
        IOUtils.closeQuietly(writer);
    }
}
Also used : GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) BZip2CompressorOutputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream) BZip2CompressorOutputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream) GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) CompressorOutputStream(org.apache.commons.compress.compressors.CompressorOutputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Example 23 with GzipCompressorOutputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream in project spring-boot by spring-projects.

the class BootBuildImageIntegrationTests method tarGzipBuildpackContent.

private void tarGzipBuildpackContent() throws IOException {
    Path tarGzipPath = Paths.get(this.gradleBuild.getProjectDir().getAbsolutePath(), "hello-world.tgz");
    try (TarArchiveOutputStream tar = new TarArchiveOutputStream(new GzipCompressorOutputStream(Files.newOutputStream(Files.createFile(tarGzipPath))))) {
        File buildpackDir = new File(this.gradleBuild.getProjectDir(), "buildpack/hello-world");
        writeDirectoryToTar(tar, buildpackDir, buildpackDir.getAbsolutePath());
    }
}
Also used : Path(java.nio.file.Path) GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) File(java.io.File)

Example 24 with GzipCompressorOutputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream in project spring-boot by spring-projects.

the class TestTarGzip method compressBuildpackArchive.

private Path compressBuildpackArchive(Path archive) throws Exception {
    Path tgzPath = Paths.get(this.buildpackDir.getAbsolutePath(), "buildpack.tgz");
    FileCopyUtils.copy(Files.newInputStream(archive), new GzipCompressorOutputStream(Files.newOutputStream(tgzPath)));
    return tgzPath;
}
Also used : Path(java.nio.file.Path) GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream)

Example 25 with GzipCompressorOutputStream

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

the class CommonCrawlDataDumper method constructNewStream.

private void constructNewStream(File outputDir) throws IOException {
    String archiveName = new SimpleDateFormat("yyyyMMddhhmm'.tar.gz'").format(new Date());
    LOG.info("Creating a new gzip archive: " + archiveName);
    fileOutput = new FileOutputStream(new File(outputDir + File.separator + archiveName));
    bufOutput = new BufferedOutputStream(fileOutput);
    gzipOutput = new GzipCompressorOutputStream(bufOutput);
    tarOutput = new TarArchiveOutputStream(gzipOutput);
    tarOutput.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
}
Also used : GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) FileOutputStream(java.io.FileOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) SimpleDateFormat(com.ibm.icu.text.SimpleDateFormat) SequenceFile(org.apache.hadoop.io.SequenceFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Date(java.util.Date)

Aggregations

GzipCompressorOutputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream)33 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)24 File (java.io.File)16 FileOutputStream (java.io.FileOutputStream)13 Path (java.nio.file.Path)13 BufferedOutputStream (java.io.BufferedOutputStream)8 OutputStream (java.io.OutputStream)8 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)7 FileInputStream (java.io.FileInputStream)6 IOException (java.io.IOException)6 ZipOutputStream (java.util.zip.ZipOutputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ArchiveOutputStream (org.apache.commons.compress.archivers.ArchiveOutputStream)4 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)3 Test (org.junit.Test)3 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