Java GZIPOutputStream:提高压缩级别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19138179/
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
GZIPOutputStream: Increase compression level
提问by schnatterer
java.util.zip.GZIPOutputStream
does not provide a constructor argument or a setter for the compression level of its underlying Deflater
.
java.util.zip.GZIPOutputStream
不为其底层Deflater
.
There are ways to work around this issue, as described here, for example:
有办法解决这个问题,如所描述这里,例如:
GZIPOutputStream gzip = new GZIPOutputStream(output) {
{
this.def.setLevel(Deflater.BEST_COMPRESSION);
}
};
I GZIPped a 10G file with this and its size didn't decrease by a single bit compared to using the preset DEFAULT_COMPRESSION.
我用这个 GZIP 压缩了一个 10G 的文件,与使用预设相比,它的大小没有减少一点 DEFAULT_COMPRESSION.
The answerto this questionsays that under certain circumstances setting the level might not work as planned. Just to make sure, I also tried to create a new Deflater
:
的回答到这个问题说,在按计划进行某些情况下设定的水平可能无法正常工作。只是为了确保,我还尝试创建一个新的Deflater
:
this.def = new Deflater(Deflater.BEST_COMPRESSION, true);
But sill no reduction in file size...
但是文件大小并没有减少......
Is there a reason why they did not provide access to the Deflater
level?
他们不提供对Deflater
关卡的访问权限是否有原因?
Or is something wrong with the code sample above?
还是上面的代码示例有问题?
Does the deflater level work at all?
泄压器水平是否有效?
Edit: Thanks for the comments.
编辑:感谢您的意见。
Can the file be compressed any further?
It's a UTF-8 text file that is compressed from 10G to 10M using Default compression. So without knowing details about the compression levels, I reckoned it could be compressed further.
Time difference between
DEFAULT_COMPRESSION
andBEST_COMPRESSION
?I don't have time to create really reliable figures. But I executed the code with each compression level about five times and both take about the same time (2 minutes +/- 5 seconds).
File size with
gzip -v9
? The file created by gzip is about 15KB smaller than the one created by java. So, for my specific use case it's not worth investigating this topic any further.
文件可以进一步压缩吗?
它是一个 UTF-8 文本文件,使用默认压缩从 10G 压缩到 10M。因此,在不知道压缩级别的详细信息的情况下,我认为它可以进一步压缩。
DEFAULT_COMPRESSION
和之间的时差BEST_COMPRESSION
?我没有时间创建真正可靠的数字。但是我用每个压缩级别执行了大约五次代码,并且都花费了大约相同的时间(2 分钟 +/- 5 秒)。
文件大小与
gzip -v9
? gzip创建的文件比java创建的文件小15KB左右。因此,对于我的特定用例,不值得进一步研究该主题。
However, the three fundamental questions stated above still persist. Anyone ever successfully decreased a file using higher compression levels with GZIPOutputStream
?
但是,上述三个基本问题仍然存在。任何人曾经使用更高的压缩级别成功地减少了文件GZIPOutputStream
?
回答by Mark Adler
You could copy the definition of GZIPOutputStream
, which is a simple wrap of Deflater
, and make your own version changing the level when the Deflater
instance is created.
您可以复制 的定义(GZIPOutputStream
它是 的简单包装)Deflater
,并在Deflater
创建实例时使您自己的版本更改级别。
回答by Krishna Sundar
Yes, I increased my data compression ratio slightly using java GZIP util.
是的,我使用 java GZIP util 稍微提高了数据压缩率。
class MyGZIPOutputStream
extends GZIPOutputStream {
public MyGZIPOutputStream( OutputStream out ) throws IOException {
super( out );
}
public void setLevel( int level ) {
def.setLevel(level);
}
}
Just wrap it around your stream and set the level as,
只需将它包裹在您的流中并将级别设置为,
new MyGZIPOutputStream( outputstream ).setLevel( Deflater.BEST_COMPRESSION );
Here are the performance results which I tried over 3.2 GB data,
这是我尝试超过 3.2 GB 数据的性能结果,
Data Compression ratio before ( which used default compression ) : 1.3823362619139712
之前的数据压缩率(使用默认压缩):1.3823362619139712
Data Compression ratio after ( which used best compression ) : 1.3836412922501984
之后的数据压缩率(使用最佳压缩):1.3836412922501984
I know it's not a great improvement but still a progress.
我知道这不是一个很大的改进,但仍然是一个进步。