Gzip


gzip is a file format and a software application for file compression and decompression. The program was created by Jean-loup Gailly and Mark Adler as a free software replacement for the compress program used in early Unix systems, and intended for use by GNU. Version 0.1 was first publicly released on 31 October 1992, and version 1.0 followed in February 1993.
As the file format can be decompressed via a streaming algorithm, it is commonly used in stream-based technology such as Web protocols, data interchange and ETL.

File format

A gzip file contains a 10-byte header, optional extra header fields, a DEFLATE-compressed payload and an 8-byte trailer.
gzip is based on the DEFLATE algorithm, which is a combination of LZ77 and Huffman coding. DEFLATE was intended as a replacement for LZW and other patent-encumbered data compression algorithms which, at the time, limited the usability of the compress utility and other popular archivers.
Although multiple streams may be concatenated, normally only a single file is compressed. Compressed archives are typically created by assembling collections of files into a single tar archive, and then compressing that archive with gzip. The final compressed file usually has the extension .tar.gz, .tgz or .gzip
gzip is not to be confused with the ZIP archive format, which also uses DEFLATE. The ZIP format can hold collections of files without an external archiver, but is less compact than compressed tarballs holding the same data, because it compresses files individually and cannot take advantage of redundancy between files.
The gzip file format is also not to be confused with that of the compress utility, based on LZW, with extension .Z; however, the gunzip utility is able to decompress .Z files.

File structure

All multi-byte values are encoded in little-endian.
Offset FieldSize Description
0ID11Magic number. Must be 1F 8B.
1ID21Magic number. Must be 1F 8B.
2CM1Compression method. Must be 8 .
3FLG1Flags. Reserved bits must be zero.
  • Bit 0 : FTEXT. Set by the compressor to indicate the file encoding is probably ASCII.
  • Bit 1: FHCRC
  • Bit 2: FEXTRA
  • Bit 3: FNAME
  • Bit 4: FCOMMENT
  • Bit 5: Reserved
  • Bit 6: Reserved
  • Bit 7 : Reserved
4MTIME4Unix time when the file was last modified. If the compressed data did not come from a file, MTIME is the Unix time when compression started. 0 means no timestamp is available.
8XFL1Extra flags.
  • Deflate-specific flags.
  • * 0: None
  • * 2: Best compression
  • * 4: Fastest compression
  • 9OS1Filesystem on which compression occurred.
  • 255: Unknown
  • 0: FAT filesystem
  • 1: Amiga
  • 2: OpenVMS
  • 3: Unix
  • 4: VM/CMS
  • 5: Atari TOS
  • 6: HPFS filesystem
  • 7: Macintosh
  • 8: Z-System
  • 9: CP/M
  • 10: TOPS-20
  • 11: NTFS filesystem
  • 12: QDOS
  • 13: Acorn RISCOS
  • 10XLEN0 or 2Extra field is a sequence of subfields. XLEN is the size in bytes of the extra field. Both are present if the FEXTRA flag is set. Each subfield starts with SI1 SI2 followed by a two-byte LEN value indicating the remaining number of bytes in the subfield. Subfield IDs with SI2 = 0 are reserved for future use.
    12Extra field0 or XLENExtra field is a sequence of subfields. XLEN is the size in bytes of the extra field. Both are present if the FEXTRA flag is set. Each subfield starts with SI1 SI2 followed by a two-byte LEN value indicating the remaining number of bytes in the subfield. Subfield IDs with SI2 = 0 are reserved for future use.
    rowspan=6" File nameNull-terminated name of the file being compressed. Present if the FNAME flag is set. Encoded as ISO 8859-1. Converted to lowercase on case-insensitive filesystems. Empty if the compressed data did not come from a named file.
    File commentNull-terminated file comment intended for human consumption. Present if the FCOMMENT flag is set. Encoded as ISO 8859-1. Newlines should use a single line feed character.-
    CRC160 or 2Two least significant bytes of the CRC-32 of all bytes in the gzip file up to this field. Present if the FHCRC flag is set.-
    Compressed dataThe compressed data.-
    CRC324CRC-32 of the uncompressed data.-
    ISIZE4Size of the uncompressed data modulo.-

    Implementations

    Various implementations of the program have been written. The most commonly known is the GNU Project's implementation using Lempel-Ziv coding. OpenBSD's version of gzip is actually the compress program, to which support for the gzip format was added in OpenBSD 3.4. The "g" in this specific version stands for gratis. FreeBSD, DragonFly BSD and NetBSD use a BSD-licensed implementation instead of the GNU version; it is actually a command-line interface for zlib intended to be compatible with the GNU implementations' options. These implementations originally come from NetBSD, and support decompression of bzip2 and the Unix pack format.
    An alternative compression program achieving 3-8% better compression is Zopfli. It achieves gzip-compatible compression using more exhaustive algorithms, at the expense of compression time required. It does not affect decompression time.
    pigz, written by Mark Adler, is compatible with gzip and speeds up compression by using all available CPU cores and threads.

    Damage recovery

    Data in blocks prior to the first damaged part of the archive is usually fully readable. Data from blocks not demolished by damage that are located afterward may be recoverable through difficult workarounds.

    Derivatives and other uses

    The tar utility included in most Linux distributions can extract.tar.gz files by passing the option, e.g.,, where -z instructs decompression, -x means extraction, and -f specifies the name of the compressed archive file to extract from. Optionally, -v lists files as they are being extracted.
    The zlib library implements the DEFLATE algorithm, and supports both the gzip file format and a lightweight data stream format. The zlib stream format, DEFLATE, and the gzip file format were standardized respectively as RFC 1950, RFC 1951, and RFC 1952.
    The gzip format is used in HTTP compression, a technique used to speed up the sending of HTML and other content on the World Wide Web. It is one of the three standard formats for HTTP compression as specified in RFC 2616. This RFC also specifies a zlib format, which is equal to the gzip format except that gzip adds eleven bytes of overhead in the form of headers and trailers. Still, the gzip format is sometimes recommended over zlib because Internet Explorer does not implement the standard correctly and cannot handle the zlib format as specified in RFC 1950.
    zlib DEFLATE is used internally by the Portable Network Graphics format.
    Since the late 1990s, bzip2, a file compression utility based on a block-sorting algorithm, has gained some popularity as a gzip replacement. It produces considerably smaller files, but at the cost of memory and processing time.
    AdvanceCOMP, Zopfli, libdeflate and 7-Zip can produce gzip-compatible files, using an internal DEFLATE implementation with better compression ratios than gzip itself—at the cost of more processor time compared to the reference implementation.
    Research published in 2023 showed that simple lossless compression techniques such as gzip could be combined with a k-nearest-neighbor classifier to create an attractive alternative to deep neural networks for text classification in natural language processing. This approach has been shown to equal and in some cases outperform conventional approaches such as BERT due to low resource requirements, e.g. no requirement for GPU hardware.