The Go compress package provides standard interfaces and implementations for compressing and decompressing binary data across several widely used algorithms.
Key Subpackages:
compress/gzip: Implements reading and writing of GZIP format files (.gz). Widely used for web HTTP response compression and log file archival.compress/zlib: Implements reading and writing of RFC 1950 ZLIB format streams, commonly used in graphics formats (like PNG) and network protocols.compress/flate: Implements DEFLATE compressed data format (RFC 1951), serving as the underlying core compression engine for bothgzipandzippackages.compress/lzw: Implements Lempel-Ziv-Welch compression, historically used in GIF images and PDF documents.
Common Patterns & Characteristics:
- Streaming I/O Wrapping: Compressors (
Writer) and decompressors (Reader) wrap existingio.Writerorio.Readerstreams, allowing on-the-fly compression without holding full payloads in memory. - Configurable Compression Levels: Writers allow choosing trade-offs between speed and compression ratio (e.g.,
gzip.BestSpeed,gzip.BestCompression, orgzip.DefaultCompression). - Resource Cleanup: Writers buffer data and append footer checksums, requiring an explicit
.Close()call to flush remaining bytes cleanly to the underlying destination.