Skip to content

gzip

currentMigrated from docs/data/gzip.mdx at 37c3317fd058; modified for Encode Wiki · CC BY-SA 4.0

gzip is the oldest and most universally readable general-purpose compressor. It uses DEFLATE (32 KiB window), the same algorithm inside ZIP and PNG, and every Unix system, browser, programming language and CDN reads it. Its ratio is the weakest of the common tools, but it is fast, needs almost no memory, and works everywhere. Use it when compatibility matters more than size; otherwise prefer zstd.

Terminal window
gzip -k file # file.gz, level 6, keep the original
gzip -9 -k file # best ratio; slower, usually a few % smaller
gzip -d file.gz # decompress (or gunzip)
zcat file.gz | less # to stdout (gzip -dc)
gzip -t file.gz # test integrity
gzip -l file.gz # sizes and ratio

Like xz, gzip replaces the input file unless you pass -k. Levels run 1–9, default 6; -9 buys little over 6, and zopfli-style optimisers (pigz -11) can squeeze a few percent more for files that are compressed once and served often.

For reproducible output, add -n, which stops gzip storing the original file name and timestamp in the header: the same input then gives the same bytes. --rsyncable makes the stream friendlier to rsync, at a small cost in size.

pigz is a drop-in multithreaded gzip that writes standard .gz files:

Terminal window
pigz -p 8 -9 -k file
pigz -dc file.gz | tar -x
tar -I pigz -cf backup.tar.gz dir/

Compression scales with cores; decompression of a normal .gz is mostly single-threaded, since DEFLATE blocks depend on each other. It is not part of most base installs.

gzip compresses one stream. For a tree use tar:

Terminal window
tar -czf backup.tar.gz dir/
tar -xzf backup.tar.gz

Concatenating .gz files produces a valid multi-member .gz that decompresses to the concatenated data, which is useful for appending to compressed logs.

gzip is the fallback content coding supported by every client. Servers compress responses on the fly at a low-to-medium level; for static assets, precompress once at -9 (or with pigz -11) and serve it with Content-Encoding: gzip. Brotli compresses text assets noticeably smaller, but gzip remains the compatibility baseline.

  • Already-compressed data (JPEG, MP4, ZIP, video) does not shrink; gzip may make it slightly larger.
  • .gz is not .zip. gzip stores one file stream with no directory structure; ZIP stores many entries. See ZIP.
  • Corruption: the CRC detects damage but gzip has no recovery; a damaged stream is lost from the bad point onward.