GZIPOutputStream:增加压缩级别 [英] GZIPOutputStream: Increase compression level

查看:55
本文介绍了GZIPOutputStream:增加压缩级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我用它压缩了一个10G的文件,与使用相比,它的大小没有减少一点预设 DEFAULT_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.

的回答此问题说,在某些情况下,设置水平可能会不能按计划工作。为了确保这一点,我还尝试创建一个新的 Deflater

The answer to this question says that under certain circumstances setting the level might not work as planned. Just to make sure, I also tried to create a new Deflater:

this.def = new Deflater(Deflater.BEST_COMPRESSION, true);

但是文件大小不能减少...

But sill no reduction in file size...

他们是否有理由不提供对 Deflater 级别的访问权限?

Is there a reason why they did not provide access to the Deflater level?

还是某些原因上面的代码示例有误?

Or is something wrong with the code sample above?

放气水平完全可以工作吗?

Does the deflater level work at all?

编辑:感谢您的评论


  1. 可以进一步压缩文件吗?

  1. Can the file be compressed any further?

这是一个UTF-8文本文件,使用
默认压缩从10G压缩到10M。因此,在不了解有关
压缩级别的详细信息的情况下,我认为它可以进一步压缩。

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.

DEFAULT_COMPRESSION BEST_COMPRESSION

我没有时间来创建可靠的数字。但是我以每个压缩级别执行了大约五次代码,并且都花费了大约相同的时间(2分钟+/- 5秒)。

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).

文件大小为 gzip -v9
gzip创建的文件比java创建的文件小15KB。因此,对于我的特定用例,不值得进一步研究此主题。

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.

但是,这三个基本问题以上仍然坚持。是否有人通过 GZIPOutputStream 使用更高的压缩级别成功减少了文件?

However, the three fundamental questions stated above still persist. Anyone ever successfully decreased a file using higher compression levels with GZIPOutputStream?

推荐答案

Yes, I increased my data compression ratio slightly using 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 );

以下是我尝试过3.2 GB数据的性能结果,

Here are the performance results which I tried over 3.2 GB data,

之前的数据压缩率(使用默认压缩):1.3823362619139712

Data Compression ratio before ( which used default compression ) : 1.3823362619139712

之后的数据压缩率(使用最佳压缩):1.3836412922501984

Data Compression ratio after ( which used best compression ) : 1.3836412922501984

我知道这不是很大的进步,但仍然是一个进步。

I know it's not a great improvement but still a progress.

这篇关于GZIPOutputStream:增加压缩级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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