How to Extract and Decompress DEFLATE File in Terminal

Published Jun 14, 2022

How can we extract the contents of a file compressed using DEFLATE?

Suppose we have a file compressed using DEFLATE: file.json.deflate.

At the end, read about how to save the uncompressed, output file and store our deflate command as a useful alias.

1. Using pigz

pigz -d < file.json.deflate

We may have to install pigz to use this command (e.g. on macOS, brew install pigz).

2. Using zlib-deflate

zlib-flate -uncompress < file.json.deflate

We may have to install qpdf to use zlib-deflate (e.g. on macOS, brew install qpdf).

3. Using perl

perl -MCompress::Zlib -e 'undef $/; print uncompress(<>)' < file.json.deflate

4. Using python/python3

python -c "import zlib,sys;print(zlib.decompress(sys.stdin.buffer.read()).decode('utf8'))" < file.json.deflate

6. Using ruby

ruby -rzlib -e 'print Zlib::Inflate.new.inflate(STDIN.read)' < file.json.deflate

Save output file

We can save the contents to a file by redirecting the output to something like file.json.

To do this, simply add > file.json to the end of each command.

Save deflate command as an alias

We might have noticed that all the commands redirect the file contents using the left angle bracket < file.json.deflate.

We can rewrite these commands using cat and a pipe |.

pigz -d < file.json.deflate
# Same as
cat file.json.deflate | pigz -d

With that in mind, we can create a deflate alias for the command we want to use. Then, we can simply use deflate after a cat to decompress the file.

alias deflate="pigz -d"
cat file.json.deflate | deflate
cat file.json.deflate | deflate > file.json