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 thoughts on “setting compression level for GZIPOutputStream in java

  1. Do you need any other special way to read this other than the normal GZIP way?

    1. you can read it using ‘normal gzip way’ – no special parameters needed, i unpack data packed in this way using regular GZIPInputStream. it can also be decompressed using command line gunzip or gzip implementations from other languages/libraries.

Leave a Reply to Ben Cancel reply

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

(Spamcheck Enabled)