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 bs=1024576 count=2048
> gzip -c       >
> gzip -c    >
> gzip -c >
> ls -lh

This entry was posted in linux.