Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

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

(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 at 22:40

4 Answers 4

up vote 21 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
    
which package is that utility from? –  mykhal Sep 25 '12 at 12:22
3  
@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

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

$ cat /tmp/data | openssl zlib -d
...

*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
4  
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 at 10:59

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:
    f.write(s)
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
4  
@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
1  
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())
        expanded.write(data)
$ 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

Your Answer

 
discard

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.