Unix & Linux: How to uncompress zlib data in UNIX? (10 Solutions!!)
Channel: Roel Van de Paar
Date: 2020-08-08

Unix & Linux: How to uncompress zlib data in UNIX?


The Question: 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't see any option to uncompress such raw data in gzip man
page, and the zlib package does not contain any executable utility.
Is there a utility to uncompress raw zlib data?

Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful

== This solution helped 11 people ==
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)

== This solution helped 3 people ==
This might do it:
import glob
import zlib
import sys

for filename in sys.argv:
with open(filename, 'rb') as compressed:
with open(filename + '-decompressed', 'wb') as expanded:
data = zlib.decompress(compressed.read())
expanded.write(data)
Then run it like this:
$ python expander.py data/*

== This solution helped 65 people ==
I have found a solution (one of the possible ones), it's using openssl:
$ openssl zlib -d ＜ /tmp/data
or
$ 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)

== This solution helped 152 people ==
It is also possible to decompress it using standard /questions/tagged/shell-
script + gzip, if you don't have, or want to use openssl or other tools.
The trick is to prepend the gzip_magic_number_and_compress_method to the actual
data from zlib.compress:
printf "x1fx8bx08x00x00x00x00x00" |cat - /tmp/data |gzip -dc ＞/tmp/out
Edits:
@d0sboots commented: For RAW Deflate data, you need to add 2 more null bytes:
-＞ "x1fx8bx08x00x00x00x00x00x00x00"
https://stackoverflow.com/questions/9050260/what-does-a-zlib-header-look-like
gives more information about this approach. An answer there suggests that there
is also an 8 byte footer.
Users @Vitali-Kushner and @mark-bessey reported success even with truncated
files, so a gzip footer does not seem strictly required.
@tobias-kienzler suggested this function for the /questions/tagged/bashrc:
zlipd() (printf "x1fx8bx08x00x00x00x00x00" |cat - $@ |gzip -dc)

== This solution helped 88 people ==
zlib-flate -uncompress ＜ FILE
I tried this and it worked for me.
zlib-flate can be found in package qpdf (in Debian Squeeze and Fedora 23,
according to comments in other answers)
(Thanks to user @tino who provided this as a comment below the OpenSSL answer.
Made into propper answer for easy access.)

With thanks & praise to God, and with thanks to the many people who have made this project possible! | Content (except music & images) licensed under cc by-sa 3.0 | Music: https://www.bensound.com/royalty-free-music | Images: https://stocksnap.io/license & others | With thanks to user wkpark (https://unix.stackexchange.com/users/23826), user snodnipper (https://unix.stackexchange.com/users/191915), user mykhal (https://unix.stackexchange.com/users/11649), user muru (https://unix.stackexchange.com/users/70524), user Mecki (https://unix.stackexchange.com/users/296146), user Marek Sebera (https://unix.stackexchange.com/users/33883), user l0b0 (https://unix.stackexchange.com/users/3645), user Jeff Schaller (https://unix.stackexchange.com/users/117549), user Henno Brandsma (https://unix.stackexchange.com/users/3094), user Facebook Staff are Complicit (https://unix.stackexchange.com/users/2710), user Danny R (https://unix.stackexchange.com/users/47937), user Catskul (https://unix.stackexchange.com/users/8231), user Alex Stragies (https://unix.stackexchange.com/users/127903), and the Stack Exchange Network (http://unix.stackexchange.com/questions/22834). Trademarks are property of their respective owners. Disclaimer: All information is provided "AS IS" without warranty of any kind. You are responsible for your own actions. Please contact me if anything is amiss at Roel D.OT VandePaar A.T gmail.com.