Search in sources :

Example 6 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 7 with GzipCompressorOutputStream

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

the class Dumper method openArchiveOut.

private static ArchiveOutputStream openArchiveOut(Path archive) throws IOException {
    // StandardOpenOption.CREATE_NEW is important here because it atomically asserts that the file doesn't
    // exist as it is opened, avoiding a TOCTOU race condition which results in a security vulnerability. I
    // can't see a way to write a test to verify that we are using this option rather than just implementing
    // the check ourselves non-atomically.
    TarArchiveOutputStream tarball = new TarArchiveOutputStream(new GzipCompressorOutputStream(Files.newOutputStream(archive, StandardOpenOption.CREATE_NEW)));
    tarball.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
    tarball.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
    return tarball;
}
Also used : GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)

Example 8 with GzipCompressorOutputStream

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

the class ArchiveMojo method archive.

public //ArchiverException,
File archive(//ArchiverException,
File source, //ArchiverException,
File dest, //ArchiverException,
Artifact artifact) throws IOException {
    String serverName = null;
    if (targetFile != null) {
        serverName = targetFile.getName();
    } else {
        serverName = artifact.getArtifactId() + "-" + artifact.getVersion();
    }
    dest = new File(dest, serverName + "." + artifact.getType());
    String prefix = "";
    if (usePathPrefix) {
        prefix = pathPrefix.trim();
        if (prefix.length() > 0 && !prefix.endsWith("/")) {
            prefix += "/";
        }
    }
    if ("tar.gz".equals(artifact.getType())) {
        try (OutputStream fOut = Files.newOutputStream(dest.toPath());
            OutputStream bOut = new BufferedOutputStream(fOut);
            OutputStream gzOut = new GzipCompressorOutputStream(bOut);
            TarArchiveOutputStream tOut = new TarArchiveOutputStream(gzOut);
            DirectoryStream<Path> children = Files.newDirectoryStream(source.toPath())) {
            tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
            tOut.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
            for (Path child : children) {
                addFileToTarGz(tOut, child, prefix);
            }
        }
    } else if ("zip".equals(artifact.getType())) {
        try (OutputStream fOut = Files.newOutputStream(dest.toPath());
            OutputStream bOut = new BufferedOutputStream(fOut);
            ZipArchiveOutputStream tOut = new ZipArchiveOutputStream(bOut);
            DirectoryStream<Path> children = Files.newDirectoryStream(source.toPath())) {
            for (Path child : children) {
                addFileToZip(tOut, child, prefix);
            }
        }
    } else {
        throw new IllegalArgumentException("Unknown target type: " + artifact.getType());
    }
    return dest;
}
Also used : Path(java.nio.file.Path) GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) DirectoryStream(java.nio.file.DirectoryStream) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

GzipCompressorOutputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream)8 FileOutputStream (java.io.FileOutputStream)4 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)4 BufferedOutputStream (java.io.BufferedOutputStream)3 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 BufferedWriter (java.io.BufferedWriter)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 DirectoryStream (java.nio.file.DirectoryStream)1 Random (java.util.Random)1 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)1 ZipArchiveOutputStream (org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream)1 CompressorOutputStream (org.apache.commons.compress.compressors.CompressorOutputStream)1 BZip2CompressorOutputStream (org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream)1 Test (org.junit.Test)1