gzip (software)
Author
DeveloperGNU Project
Released31 October 1992 (age 34)
Operating systemUnix-like, Plan 9, Inferno
GenreData compression
Programming languageC
LicenseGPL-3.0-or-later
Websitegnu.org/software/gzip[1]
gzip.org (obsolete)
Reposavannah.gnu.org/projects/gzip[2]

gzip is a file format and a file compression program. The program uses the Deflate algorithm to compress and decompress a single file using the gzip file format.

gzip was released in 1992 as a free software replacement for the compress program because its compression algorithm, LZW, was covered by patents from Unisys and IBM,[4] which did not expire until 2003 and 2004.[5] Jean-Loup Gailly designed the gzip file format,[6] which was later specified by RFC 1952,[7] and originally wrote the gzip program. Mark Adler wrote the decompression part. gzip is now developed by the GNU project.[8]

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 (in standard pipes).

File format

gzip (file format)
Extension.gz, .tgz, .gzip
Mimeapplication/gzip[3]
Magic
  • 1F 8B (specification)
  • 1F 8B 08 (de facto)
DeveloperJean-Loup Gailly
CompressionLossless
Container forDeflate stream

A gzip file (described in the table below) consists of a 10-byte header, optional extra header fields, Deflate-compressed data, and an 8-byte trailer.

Although multiple streams may be concatenated (gzipped files are simply decompressed concatenated as if they were originally one file),[9] normally only a single file is compressed.[10] Compressed archives are typically created by assembling collections of files into a single tar archive (also called tarball),[11] and then compressing that archive with gzip. The final compressed file usually has the extension .tar.gz, .tgz, .gz, 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 (solid compression). 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.[12]

File structure

Endianness is little-endian.[13]

Offset (bytes)Field[14]Size (bytes)Description
0ID11Magic number. Must be 1F 8B.
1ID21
2CM1Compression method. Must be 8 (Deflate).
3FLG1

Flags. Reserved bits must be zero.

  • Bit 0 (LSb): 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 (MSb): 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.
8XFL1

Extra flags.

  • Deflate-specific flags.
    • 0: None (default value)
    • 2: Best compression (level 9)
    • 4: Fastest compression (level 1)
9OS1

Filesystem on which compression occurred.

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 (a two-byte identifier; typically two ASCII letters with some mnemonic value) 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 XLEN
rowspan=6"File nameNull-terminated name of the file being compressed. Present if the FNAME flag is set. Encoded as ISO 8859-1 (latin-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 (latin-1). Newlines should use a single line feed (LF) character.
CRC160 or 2Two least significant bytes of the CRC-32 (ISO 3309) of all bytes in the gzip file up to (not including) this field. Present if the FHCRC flag is set.
Compressed dataDeflate stream.
CRC324CRC-32 (ISO 3309) of the uncompressed data.
ISIZE4Size (in bytes) of the uncompressed data modulo 2^{32}.

Implementations

NetBSD Gzip / FreeBSD Gzip
DeveloperThe NetBSD Foundation
GenreData compression
Programming languageC
LicenseSimplified BSD License
Websitehide
Repo//cvsweb.netbsd.org/bsdweb.cgi/src/usr.bin/gzip

Various implementations of the program have been written. The most commonly known is the GNU Project's implementation using Lempel-Ziv coding (LZ77). 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.[15] 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.[16] 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.[17]

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.[18]

Derivatives and other uses

The tar utility included in most Linux distributions can extract .tar.gz files by passing the z option, e.g., tar -zxf file.tar.gz, where -z instructs decompression, -x means extraction, and -f specifies the name of the compressed archive file to extract from. Optionally, -v (verbose) lists files as they are being extracted.[19]

The zlib library supports the gzip file format.[20]

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 (called "DEFLATE"), 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.[21]

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 (especially for source code and other structured text), but at the cost of memory and processing time (up to a factor of 4).[22]

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.[citation needed]

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.[23]

References

  1. ^ "On the Potential of V2X Message Compression for Vehicular Networks". IEEE Access. doi:10.1109/ACCESS.2020.3041688
  2. ^ "Gzip - GNU Project". Archived 5 May 2025 at the Wayback Machine. For development sources and other information, please see the Gzip project page at savannah.gnu.org.
  3. ^ "The 'application/zlib' and 'application/gzip' Media Types". Internet Engineering Task Force. Retrieved 2014-03-01.
  4. ^ "Gzip - GNU Project". Archived 5 May 2025 at the Wayback Machine. We developed this program as a replacement for compress because of the Unisys and IBM patents covering the LZW algorithm used by compress.
  5. ^ "Unisys | LZW Patent and Software Information". Archived 26 June 2009 at the Wayback Machine. Unisys U.S. LZW Patent No. 4,558,302 expired on June 20, 2003, the counterpart patents in the United Kingdom, France, Germany and Italy expired on June 18, 2004, the Japanese counterpart patents expired on June 20, 2004 and the counterpart Canadian patent expired on July 7, 2004.
  6. ^ "Acknowledgements". p. 10. Jean-Loup Gailly designed the gzip format and ...
  7. ^ "GZIP file format specification version 4.3"
  8. ^ "Gzip - GNU Project". Archived 5 May 2025 at the Wayback Machine. GNU Gzip is a popular data compression program originally written by Jean-Loup Gailly for the GNU project. Mark Adler wrote the decompression part.
  9. ^ "GNU Gzip: Advanced usage". Gnu.org. Archived 24 December 2012 at the Wayback Machine. Retrieved 2012-11-28.
  10. ^ "Can gzip compress several files into a single archive?". Gnu.org. Archived 2010-07-22 at the Wayback Machine. Retrieved 2010-01-27.
  11. ^ "tarball, The Jargon File, version 4.4.7". Catb.org. Archived 20 March 2017 at the Wayback Machine. Retrieved 2010-01-27.
  12. ^ "GNU Gzip". The GNU Operating System and the Free Software Movement. 2023-02-05. Retrieved 2024-04-03. gunzip can currently decompress files created by gzip, zip, compress or pack. The detection of the input format is automatic.
  13. ^ "Overall conventions". p. 4. All multi-byte numbers in the format described here are stored with the least-significant byte first (at the lower memory address).
  14. ^ "Member format". p. 5.
  15. ^ "OpenBSD gzip(1) manual page". Openbsd.org. OpenBSD. Retrieved 2018-02-04.
  16. ^ "gzip". Man.freebsd.org. 2011-10-09. Archived 17 December 2019 at the Wayback Machine. Retrieved 2014-03-01.
  17. ^ Mark Adler (2017). "pigz: A parallel implementation of gzip for modern multi-processor, multi-core machines". zlib.net. Archived 18 December 2018 at the Wayback Machine. Retrieved 23 December 2018.
  18. ^ Recovering a damaged .gz file – Jean-Loup Gailly, GZip.org
  19. ^ "How To Extract / Unzip tar.gz Files From Linux Command Line". Knowledge Base by phoenixNAP. 14 November 2019. Retrieved 12 January 2022.
  20. ^ "zlib 1.3.1 Manual". Introduction. Archived 8 February 2024 at the Wayback Machine. Retrieved 8 February 2024. The library also supports reading and writing files in gzip (.gz) format ...
  21. ^ Lawrence, Eric (21 November 2014). "Compressing the Web". MSDN Blogs > IEInternals. Microsoft. Archived 28 October 2015 at the Wayback Machine. Retrieved 2 November 2015.
  22. ^ "Comparison Tool: 7-zip vs bzip2 vs gzip". compressionratings.com. Archived from the original on 1 November 2014. Retrieved 1 November 2014.
  23. ^ Jiang, Zhiying; Yang, Matthew; Tsirlin, Mikhail; Tang, Raphael; Dai, Yiqin; Lin, Jimmy (July 2023). ""Low-Resource" Text Classification: A Parameter-Free Classification Method with Compressors". Findings of the Association for Computational Linguistics: ACL 2023. Toronto, Canada: Association for Computational Linguistics. doi:10.18653/v1/2023.findings-acl.426. S2CID 260668487