# gzip

Canonical URL: https://encode.wiki/codecs/data/gzip/
Content version: ca42e61ede0a24f789d7287a0001244ad22f4abf
Status: current
License: CC BY-SA 4.0
Checksum (SHA-256): a8fc53196c7251ca17fc1599e513a6a5f99e8a71c05de8bbf5ca95f2fdbced64

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](/codecs/data/zstd/).

## Common commands

```sh
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: parallel gzip

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

```sh
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.

## Directories

gzip compresses one stream. For a tree use [tar](/codecs/data/tar/):

```sh
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.

## HTTP and web delivery

`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](/codecs/data/brotli/) compresses text assets noticeably smaller, but gzip remains the compatibility baseline.

## Pitfalls

- **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](/codecs/data/zip/).
- **Corruption:** the CRC detects damage but gzip has no recovery; a damaged stream is lost from the bad point onward.
