Go's standard library supports data compression through the compress/* packages: compress/gzip, compress/zlib, compress/flate, compress/lzw and compress/bzip2 (decompression only). Archive formats are handled by archive/zip and archive/tar. Every compressor is built on io.Writer and every decompressor on io.Reader, so you can chain them with files, network connections and HTTP bodies and process data as a stream.
Zstandard and LZ4 are not in the standard library. If you need them, use a third-party module such as github.com/klauspost/compress.
| Package | Format | Compress | Decompress | Typical use |
|---|---|---|---|---|
compress/gzip | gzip (RFC 1952) | yes | yes | Files, HTTP, log archives |
compress/zlib | zlib (RFC 1950) | yes | yes | Embedded streams, protocols |
compress/flate | raw DEFLATE | yes | yes | Building other formats |
compress/bzip2 | bzip2 | no | yes | Reading .bz2 files |
compress/lzw | LZW | yes | yes | Legacy formats (GIF, TIFF, PDF) |
archive/zip | ZIP container | yes | yes | Bundling many files |
archive/tar | tar container | yes | yes | Bundling files, combined with gzip |
For general use, gzip is the safe default because every platform and tool can read it.
The writer must be closed, because Close flushes the remaining data and writes the checksum trailer. Forgetting it produces a truncated, unreadable stream.
N is far smaller than 3900 here, and the exact value depends on the Go version and compression level. Repetitive input compresses dramatically, while already compressed data such as JPEG, MP4 or ZIP files barely shrinks.
Do not read a whole file into memory to compress it. Copy from the source file into a gzip writer wrapped around the destination file, and Go processes it in small chunks.
Levels range from gzip.BestSpeed (1) to gzip.BestCompression (9), with gzip.DefaultCompression as the balanced default. Use a faster level for logs and network streams, and a higher one for data you write once and read often. The gzip header can also hold the original file name and modification time through zw.Name and zw.ModTime.
A tiny malicious archive can expand to gigabytes. When you decompress data from users or the network, cap the output with io.LimitReader and treat hitting the cap as an error.
Reading one byte more than the limit lets you distinguish "exactly at the limit" from "over the limit". The same concern applies to archive/zip and archive/tar: validate entry names to prevent path traversal (for example ../../etc/passwd) before writing extracted files to disk.
Use archive/zip when you need to bundle several files into one portable archive. Each entry is created with CreateHeader or Create and written like any other io.Writer.
To read an archive, use zip.OpenReader("bundle.zip") and iterate over r.File, calling f.Open() for each entry. For .tar.gz files, wrap a gzip.Reader in a tar.NewReader.
Browsers send Accept-Encoding: gzip, and you can compress responses with a small middleware. Go's http.Client already adds that header and decompresses responses transparently unless you set the header yourself.
This needs the imports compress/gzip, net/http and strings. In production, many teams compress at the reverse proxy or CDN instead, or use a maintained middleware that also handles Content-Length, small bodies and already compressed content types.
Use compress/gzip for files and HTTP because it is universally supported. Use compress/zlib when a protocol specifies zlib, and archive/zip when you need a multi-file container.
No. Use a third-party module such as github.com/klauspost/compress/zstd when you need Zstandard, which usually offers a better speed-to-ratio balance than gzip.
You probably did not call Close() on the gzip.Writer. Close writes the final block and checksum, so defer or call it and check its error.
No. compress/bzip2 only decompresses. Use an external tool or a third-party library to produce bzip2 output.
Go treats compression as streams: wrap an io.Writer to compress and an io.Reader to decompress. Use gzip as the default format, always close writers, stream large files with io.Copy, limit decompressed output from untrusted sources, and reach for archive/zip or archive/tar when you need to bundle files. For Zstandard or LZ4, add a well-maintained third-party module.