This code was shamelessly borrowed from GNU gzip before being hacked at.