Format:Gzip
| Developer | Jean-loup Gailly (gzip, 1992); RFC 1952 by Peter Deutsch (1996) |
|---|---|
| Uses | Library:Zlib |
| Magic | 1F 8B 08 |
| Extensions | .gz, .tgz, .svgz, .emz |
| Endianness | little |
| On this wiki | Facts · Code · Notes · Topics |
RFC 1952 file format of gzip (.gz, .tgz): one or more members, each a 10-byte header (1F 8B 08, flags, mtime, extra flags, OS), optional extra/name/comment/header-CRC fields, raw DEFLATE data and an 8-byte little-endian trailer (CRC-32, size mod 2^32).
Layout
| Offset | Size | Field |
|---|---|---|
| 0 | 2 | ID1 ID2 = 1F 8B
|
| 2 | 1 | CM = 8 (deflate) |
| 3 | 1 | FLG: bit 0 FTEXT, 1 FHCRC, 2 FEXTRA, 3 FNAME, 4 FCOMMENT, 5-7 reserved (must be 0) |
| 4 | 4 | MTIME, Unix time, little endian (0 = none) |
| 8 | 1 | XFL: 2 = maximum compression, 4 = fastest |
| 9 | 1 | OS |
| 10 | if FEXTRA: u16 XLEN + XLEN bytes of subfields (SI1 SI2 u16 LEN data); then NUL-terminated FNAME, NUL-terminated FCOMMENT, u16 CRC16 (low 16 bits of the CRC-32 of the header) as flagged | |
| n | Format:DEFLATE data | |
| end-8 | 4 | CRC-32 of the uncompressed data, little endian |
| end-4 | 4 | ISIZE: uncompressed size mod 2^32, little endian |
A .gz file may hold several members back to back; gzip -d and zlib's gzread concatenate them, but zlib's inflate() stops after the first and must be reset.
Identification
The XFL and OS bytes tell which code wrote the file: zlib's deflate (windowBits 16+) writes XFL 0, 2 (level 9) or 4 (level 1 or Huffman-only/RLE) and an OS byte fixed at its compile time (10 on Windows, 3 on Unix, 19 on Apple); Python's gzip module writes its own header with OS 255; GNU gzip writes 3 on Unix. See the enum fact for the OS values.
CRC-32
The CRC is the reflected CRC-32 of ISO-HDLC/PKZIP/PNG: polynomial 0xEDB88320 (reflected), initial value 0xFFFFFFFF, final XOR 0xFFFFFFFF; check value of 123456789 is 0xCBF43926.
Code & repositories
- {{Repo|url=https://github.com/madler/zlib%7Ccommit=767c4c947852e143f582c85f14cf573411df1b35%7Clicense=Zlib (LICENSE file; GitHub's detector reports none)|description=The official zlib source (develop branch, after release 1.3.2): inflate/deflate, the gzip file functions, CRC-32 and Adler-32, plus contrib/ (minizip, infback9 for Deflate64, puff, a small reference inflater). Release tags v1.0.x to v1.3.2 let you diff any version that turns up in a binary.|tested_on=Read at this commit and at every release tag (inftrees.c lext pairs, inflate.h modes, deflate.h states, zlib.h API); behaviour checked with Python 3.11 (zlib 1.3.2) and ctypes calls into a MinGW Win64 zlib1.dll 1.3.1; not built (no C compiler on the test machine).|checked=checked 2026-10-17; last activity 2026-09-21; Note: the latest release ships binaries; the wiki links only the source, never the binaries}}
Pitfalls
- ISIZE is modulo 2^32: it is not the size of files of 4 GiB or more, and multi-member files have one ISIZE per member.
- zlib's OS code 10 means "Windows" in zlib but "TOPS-20" in RFC 1952 (NTFS is 11 there).
Evidence & references
- RFC 1952; zlib deflate.c lines 1066-1082 and zutil.h lines 99-188 at commit 767c4c9; headers reproduced with Python 3.11 (zlib 1.3.2 and the gzip module).