java 包含多个文件的 gzip 存档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10653683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:58:47  来源:igfitidea点击:

gzip archive with multiple files inside

javacompressiongzip

提问by ira_ira

I need to create a gzip archive with multiple files inside, how can I do this without ArchiveEntryavailable for java.util.zip.GZIPOutputStreamclass?

我需要创建一个包含多个文件的 gzip 存档,如果没有ArchiveEntry可用的java.util.zip.GZIPOutputStream课程,我怎么能做到这一点?

回答by ArjunShankar

According to the Wikipedia entryon gzip:

按照维基百科条目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.

尽管它的文件格式也允许连接多个这样的流(压缩文件只是简单地解压缩连接,就好像它们最初是一个文件一样),但 gzip 通常只用于压缩单个文件。

This is why GZIPOutputStreamdoesn't support ArchiveEntry.

这就是为什么GZIPOutputStream不支持ArchiveEntry.

Normally, multiple files are archived into one with tar, then compressed with gzipto produce a .tar.gzcompressed archive.

通常,多个文件用 存档为一个tar,然后用 压缩gzip以生成.tar.gz压缩存档。

You could create a tar.gzin this way by using the Apache Commons Compressimplementation for tar:

您可以tar.gz通过使用以下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);

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

Here is a more thorough examplethat compresses entire directories.

这是压缩整个目录的更彻底的示例

回答by ControlAltDel

GZip compresses a stream. Typically, when people use GZip with multiple files, they also use tar to munch them together

GZip 压缩流。通常,当人们将 GZip 与多个文件一起使用时,他们也会使用 tar 将它们一起咀嚼

You can find a java tar libarary here: http://www.trustice.com/java/tar/

你可以在这里找到一个 java tar 库:http: //www.trustice.com/java/tar/

Or you could use Zip instead of GZip

或者你可以使用 Zip 而不是 GZip