How do I know the gzip compression level?

9

1

Given a gzip compressed file, how do I know what compression level (1-9) was used for it?

rabin

Posted 2011-04-12T16:18:21.450

Reputation: 235

Answers

6

There is no way to directly determine gzip level .
The only way to determine it in my opinion is to gunzip the file and compressing it at different levels and then comparing the results with your existing file size.
I believe the default level is 6 so in most cases that should be your answer

Shekhar

Posted 2011-04-12T16:18:21.450

Reputation: 4 815

2

Python also defaults to a compression level on 9: https://docs.python.org/3/library/gzip.html

– RFox – 2016-02-15T14:28:40.270

Is it in the tar source or documented somewhere that -9 is the default? – xref – 2018-04-29T02:22:17.363

I read somewhere that tar -cz defaults to 9. Is that true? – rabin – 2011-04-12T16:37:53.220

2Yes. GNU tar uses level 9 by default when gzipping. – Andrew Lambert – 2011-04-12T17:26:58.187

24

It is stored in the header of file. To see it, use file command. For example:

$ file testfile.gz
testfile.gz: gzip compressed data, from Unix, last modified: Sun Sep 15 14:22:19 2013, max compression

Unfortunately there are only three possible values in the header: max speed (level 1), max compression (level 9) and "normal" (all other levels). But better than nothing!

Zouppen

Posted 2011-04-12T16:18:21.450

Reputation: 341

This is the best answer. – Florin Andrei – 2015-09-11T20:05:21.567

1Looks like the rest of a compressed file isn't any clearer. Test-compressing a 201 bytes file with all levels resulted in only 4 different outputs - partitioned by levels as (1,23,45678,9) - with levels 1 and 9 specifically marked (see XFL in RFC1952; that's why file can recognise those). A 10^7 bytes file still only resulted in 7 unique outputs - partitioned (1,2,3,4,5678,9). While this doesn't mean different levels are useless for bigger files, it shows you can't assume 9 unique outputs. – valid – 2015-11-10T03:38:09.773

2

Every version of Python until 3.5 (maybe even including 3.6) sets the compression level in the header to 9 even when it is not. Just a bug, but FYI: https://bugs.python.org/issue27521

– John Zwinck – 2016-11-16T05:15:05.407

7

gzip -l <filename> will give you the compression ratio, but there's no way of directly finding the compression level used.

Andrew Lambert

Posted 2011-04-12T16:18:21.450

Reputation: 7 136

2While the assertion about elvel is false, the command is useful for comrpession ratio. – mveroone – 2015-10-19T13:04:48.763

1

There is no direct way of knowing it. It most probably 6 (the current default) or 9 (the best compression). you need to try and compare.

See https://stackoverflow.com/questions/16153334/how-to-determine-the-compression-level-of-deflate

kolistivra

Posted 2011-04-12T16:18:21.450

Reputation: 325