Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have created zlib-compressed data in Python, like this:

import zlib
s = '...'
z = zlib.compress(s)
with open('/tmp/data', 'w') as f:

(or one-liner in shell: echo -n '...' | python2 -c 'import sys,zlib; sys.stdout.write(zlib.compress(sys.stdin.read()))' > /tmp/data)

Now, I want to uncompress the data in shell. Neither zcat nor uncompress work:

$ cat /tmp/data | gzip -d -
gzip: stdin: not in gzip format

$ zcat /tmp/data 
gzip: /tmp/data.gz: not in gzip format

$ cat /tmp/data | uncompress -
gzip: stdin: not in gzip format

It seems that i have created gzip-like file, but without any headers.. unfortunately i don's see any option to uncompress such raw data in gzip man page.. and zlib package does not contain any executable utility..

Is there an utility to uncompress raw zlib data?

share|improve this question

migrated from stackoverflow.com Oct 18 '11 at 8:48

This question came from our site for professional and enthusiast programmers.

There are many additional answers here: stackoverflow.com/questions/3178566/deflate-command-line-tool – Jack O'Connor Jan 24 '14 at 22:40
up vote 63 down vote accepted

It is also possible to decompress it using standard shell script + gzip. The trick is to prepend the gzip magic number and compress method (see http://www.onicos.com/staff/iz/formats/gzip.html) to the actual data:

printf "\x1f\x8b\x08\x00\x00\x00\x00\x00" |cat - zlib.raw |gzip -dc
share|improve this answer
.. only if one has that zlib.raw :) – mykhal Sep 25 '12 at 11:36
@mykhal I'm pretty sure zlib.raw in that example is your compressed file. – derobert Sep 25 '12 at 16:36
@derobert oops, sure :) i was seeing additional |, which isn't there.. – mykhal Sep 25 '12 at 18:02
nice! this did the trick. I had some logs compressed in python. – Vitaly Kushner Dec 11 '12 at 9:59
Of all the weird Linux shit I've seen over the years, I think this one just about takes the cake! – g33kz0r Jan 26 '15 at 16:33

I have found a solution (one of the possible ones), it's using openssl:

$ openssl zlib -d < /tmp/data


$ openssl zlib -d -in /tmp/data

*NOTE: zlib functionality is apparently available in recent openssl versions >=1.0.0 (OpenSSL has to be configured/built with zlib or zlib-dynamic option, the latter is default)

share|improve this answer
On Debian Squeeze (which has OpenSSL 0.9.8) there is zlib-flate in the qpdf package. It can be used like zlib-flate -uncompress < FILE. – Tino Sep 16 '12 at 14:09
zlib got removed from the latest versions of OpenSSL so this tip is is very helpful @Tino – Alexandr Kurilin Dec 2 '14 at 10:59
Thanks. This solution provides a better experience in decompressing short input files than the answer using "gzip" ("openssl" decompressed as much as it could while "gzip" aborted printing "unexpected end of file"). – Daniel K. Sep 16 '15 at 10:01
@Tino this should be a separate answer – Catskul Nov 1 '15 at 3:16
@Tino, it is also available via the package qpdf on Fedora 23. Alexandr Kurilin, zlib is still available in 1.0.2d-fips. – maxschlepzig Nov 24 '15 at 8:37

User @tino commented below the openssl answer but I think this should be separate:

zlib-flate -uncompress < FILE

I tried this and it worked for me.

zlib-flate can be found in the qpdf package (in Debian Squeeze and Fedora 23, according to comments in other answers)

share|improve this answer
In contrast to the other answers, this one works on OS X. – polym Dec 29 '15 at 18:32

zlib implements the compression used by gzip, but not the file format. Instead, you should use the gzip module, which itself uses zlib.

import gzip
s = '...'
with gzip.open('/tmp/data', 'w') as f:
share|improve this answer
ok, but my situation is that i have tens/hundreds thousands of those files created, so.. :) – mykhal Sep 20 '11 at 22:14
so... your files are incomplete. Perhaps you'll have to uncompress them with zlib and recompress them with gzip, if you don't still have the original data. – Greg Hewgill Sep 20 '11 at 22:18
@mykhal, why did you create ten/hundred thousands of files before checking that you could actually uncompress them? – Harpyon Sep 20 '11 at 22:19
harpyon, i can uncompress them, i just wonder which less or more common urility or zgip settings can be used for that, if i don't want to do it in python again – mykhal Sep 20 '11 at 22:47

This might do it:

import glob
import zlib
import sys

for filename in sys.argv:
    with open(filename, 'r') as compressed:
        with open(filename + '-decompressed', 'w') as expanded:
            data = zlib.decompress(compressed.read())

Then run it like this:

$ python expander.py data/*
share|improve this answer
thanks, i know about zlib.decompress. probably i'd use some walk function. i'm not sure if shell would handle my huge amount of files with glob wildcard :) – mykhal Sep 20 '11 at 22:28

You can use this to compress with zlib:

$openssl enc -z -none -e < /file/to/deflate

And this to deflate:

$openssl enc -z -none -d < /file/to/deflate
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.