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);
}
}
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);
}
}
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());
}
}
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;
}
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);
}
Aggregations