Recursive Gzip Sometimes Helps

Everyone knows that gzip’ing a file will usually make it smaller, but gzip’ing it again will not. In other words, once you’ve compressed a file, it won’t compress further.   But here’s an exception.

If you make a file of identical bytes, it can be gzip’ed several times and still become smaller.  In the example below, we made a 2GB file which consisted of identical bytes, all equal 0.  The first compression reduced the size by a factor of 1024.  The second by a factor of 602.  Next, by a factor of 14.  The next compression, not shown, grew the file by about 20 bytes.  The total compression, over the three generations, was a factor of 9,023,041 !

> dd if=/dev/zero of=2gb.zero bs=1024576 count=2048
> gzip -c 2gb.zero       > 2gb.zero.gz
> gzip -c 2gb.zero.gz    > 2gb.zero.gz.gz
> gzip -c 2gb.zero.gz.gz > 2gb.zero.gz.gz.gz
> ls -lh 2gb.zero
2.0G  2gb.zero
2.0M  2gb.zero.gz
3.4K  2gb.zero.gz.gz
328B  2gb.zero.gz.gz.gz

Comments are closed.