setting compression level for GZIPOutputStream in java

i’m compressing quite a lot of data on the fly to save storage… until now i’ve been quite sure one cannot control compression level with oracle java’s GZIPOutputStream but apparently i was wrong.

in this article – i’ve found an easy solution.

instead of:

GZIPOutputStream gzipout = new GZIPOutputStream(bos);

use

OutputStream gzipout = new GZIPOutputStream(bos){{def.setLevel(Deflater.BEST_COMPRESSION);}};

to construct your compressor.

but setting compression level to 9 [instead of default 5] made outputs barely ~0.5% smaller for me:

before	after	gain
11350	11327	23
57172	57102	70
83377	83284	93
78015	77803	212
51987	51732	255

[all sizes provided in B]

3 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

 

(Spamcheck Enabled)