gzip存档包含多个文件 [英] gzip archive with multiple files inside

查看:247
本文介绍了gzip存档包含多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个包含多个文件的gzip存档,我该如何做到 ArchiveEntry 可用于 java.util.zip.GZIPOutputStream gzip :



上的org / wiki / Gzip#File_formatrel =nofollow>维基百科词条 >

虽然它的文件格式也允许多个这样的流
级联(压缩文件简单解压缩连接,就像
他们最初是一个文件),gzip通常用于压缩
单个文件。


这是为什么 GZIPOutputStream 不支持 ArchiveEntry



通常,多个文件归档到一个 tar ,然后使用 gzip 压缩以产生 .tar.gz 压缩归档。 >

您可以使用 tar.gz .org / compress / apidocs / org / apache / commons / compress / archivers / tar / TarArchiveOutputStream.html#putArchiveEntry%28org.apache.commons.compress.archivers.ArchiveEntry%29rel =nofollow> Apache Commons Compress tar 的实现

  file_out = new FileOutputStream(new File(archive.tar.gz)); 
buffer_out = new BufferedOutputStream(file_out);
gzip_out = new GzipCompressorOutputStream(buffer_out);
tar_out = new TarArchiveOutputStream(gzip_out);

// ..然后跟'tar_out'写下东西

这是一个更全面的示例,可压缩整个目录。


I need to create a gzip archive with multiple files inside, how can I do this without ArchiveEntry available for java.util.zip.GZIPOutputStream class?

解决方案

According to the Wikipedia entry on gzip:

Although its file format also allows for multiple such streams to be concatenated (zipped files are simply decompressed concatenated as if they were originally one file), gzip is normally used to compress just single files.

This is why GZIPOutputStream doesn't support ArchiveEntry.

Normally, multiple files are archived into one with tar, then compressed with gzip to produce a .tar.gz compressed archive.

You could create a tar.gz in this way by using the Apache Commons Compress implementation for tar:

file_out = new FileOutputStream (new File ("archive.tar.gz"));
buffer_out = new BufferedOutputStream (file_out);
gzip_out = new GzipCompressorOutputStream (buffer_out);
tar_out = new TarArchiveOutputStream (gzip_out);

// .. and then talk to 'tar_out' to write stuff

Here is a more thorough example that compresses entire directories.

这篇关于gzip存档包含多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆