Introduction
Gzip compresses a single file with the DEFLATE method. The format is specified by
RFC 1952 (version 4.3, May 1996, by L. Peter Deutsch;
informational; “there have been no technical changes to the gzip format since version 4.1 of this specification”,
so there is one version of the format). The RFC was written to be compatible with the file format of Jean-loup
Gailly’s GNU gzip utility. In gzip 1.13’s gzip.c the copyright
line of Jean-loup Gailly is 1992-1993, and the inflate code (unzip.c) is by Mark Adler, put in the public domain.
Sources read for this page, in full, this session: RFC 1952 and RFC 1951 (text from rfc-editor.org), the gzip 1.13 source
(gzip.c, gzip.h, unzip.c from ftp.gnu.org/gnu/gzip/gzip-1.13.tar.gz), the SAM specification’s BGZF section
(raw SAMv1.tex) and the dictzip man page.
Gzip holds one file, so a folder is first bundled with
tar and the tar file is then compressed. The result
is named .tar.gz, or .tgz for short.
Structure
A gzip file is one or more members one after another; most files hold one. Numbers are little-endian.
0Magic1F 8B2 bytes2Method 8 for DEFLATE1 byte3Flags which optional fields follow1 byte4Modification time Unix seconds, or 04 bytes8Extra flags and operating system2 bytes10Optional fields extra data, original file name, comment, header CRCvaries…Compressed data a DEFLATE streamvaries…Trailer CRC-32 of the original data, then its size modulo 2328 bytes
The flag bits are 1 for text, 2 for a header CRC, 4 for an extra field, 8 for a file name and 16 for a comment. The name and comment are zero-terminated Latin-1 text. Because the size in the trailer wraps at 4 GiB, it gives the true size only for smaller files. The RFC says nothing about what may come before, between or after members (“no additional information before, between, or after them”).
The member header
| Offset | Size | Name | Description |
|---|---|---|---|
0 | 2 | ID1, ID2 | 1F 8B. |
2 | 1 | CM | Compression method. 8 is Deflate, the only method defined; 0 to 7 are reserved. |
3 | 1 | FLG | Flags: see below. |
4 | 4 | MTIME | The modification time of the original file, in seconds since 1 January 1970 UTC. If the data did not come from a file, it is the time compression started. 0 means no time is stored. |
8 | 1 | XFL | Extra flags, depending on the method: see XFL. |
9 | 1 | OS | The operating system the file was compressed on: see OS. |
10 | varies | Optional fields | In this order, each when its flag is set: XLEN and the extra field, FNAME, FCOMMENT and FHCRC. |
FLG, the flags
| Bit | Name | Meaning |
|---|---|---|
0 | FTEXT | The data is probably ASCII text. A hint for line-ending conversion; a compressor may leave it clear, and a decompressor may ignore it. |
1 | FHCRC | A CRC-16 of the header follows the other optional fields. |
2 | FEXTRA | An extra field follows the fixed header. |
3 | FNAME | The original file name follows: ISO 8859-1 (Latin-1), zero-terminated, with no folders, and in lower case if the file system was not case-sensitive. |
4 | FCOMMENT | A comment follows: ISO 8859-1, line ends as a single LF, zero-terminated. |
5 to 7 | reserved | Must be zero. A decompressor must report an error if any of these is set. (gzip 1.13’s gzip.h names bit 5 ENCRYPTED and bits 6 and 7 RESERVED; see the GNU gzip reader.) |
RFC 1952 says the compressor sets FTEXT by checking a small amount of the input for non-ASCII characters, and when in doubt leaves it clear (binary); the algorithm is deliberately not specified. For systems with different text and binary file formats the decompressor can use it to choose the format.
XFL, the extra flags
For method 8 (Deflate):
| Value | Meaning |
|---|---|
2 | The compressor used maximum compression, the slowest method. |
4 | The compressor used the fastest method. |
| Other | No meaning is defined. 0 is usual. |
OS, the operating system
Says what the file system was, which may tell a reader about line ends and name style.
| Value | System |
|---|---|
0 | FAT file system (MS-DOS, OS/2, NT and Windows) |
1 | Amiga |
2 | VMS (or OpenVMS) |
3 | Unix |
4 | VM/CMS |
5 | Atari TOS |
6 | HPFS file system (OS/2, NT) |
7 | Macintosh |
8 | Z-System |
9 | CP/M |
10 | TOPS-20 |
11 | NTFS file system (NT) |
12 | QDOS |
13 | Acorn RISC OS |
255 | Unknown |
The extra field
XLEN (2 bytes) gives the size of the field that follows. The field is a sequence of subfields:
| Size | Name | Description |
|---|---|---|
| 1 | SI1 | The first letter of the subfield’s ID. |
| 1 | SI2 | The second letter. An ID with SI2 = 0 is reserved for future use. |
| 2 | LEN | The length of the data, little-endian. |
| LEN | Data |
LEN is the length of the subfield data, excluding the 4 initial bytes. SI1 and SI2 are “typically two ASCII letters with some mnemonic value”.
A reader skips subfields whose ID it does not know. RFC 1952 defines one ID itself,
Ap (SI1 0x41 ‘A’, SI2 0x70; the RFC’s table labels the second one ‘P’, but its hex is the lower-case
‘p’), with the data “Apollo file type information” (no layout is given), and says Jean-Loup Gailly maintains a registry of
subfield IDs. No public copy of that registry was found. The two others below are documented by the programs that write them.
| ID | Meaning | Data |
|---|---|---|
Ap | Apollo file type (RFC 1952) | No published layout. |
BC | BGZF block size (SAM specification, “The BGZF compression format”: SI1 = 66, SI2 = 67) | LEN = 2; BSIZE (uint16): the total block size minus 1. A BGZF block is a complete gzip member of at most 64 KiB before and after compression, so a reader can step between members without inflating. |
RA | dictzip random-access table (dictzip man page: SI1 ‘R’, SI2 ‘A’, “Random Access”; files normally named .dz) | VER (2 bytes; for version 1 every value is an unsigned 16-bit integer, least significant byte first), CHLEN (2: the length of an uncompressed chunk), CHCNT (2: the number of chunks), then CHCNT words, the compressed size of each chunk, so a reader can start inflating at any chunk. Each chunk is under 64 kB. |
FHCRC, the header CRC
Two bytes: the low 16 bits of the CRC-32 of every byte of the header before this field, from ID1 on, including any extra field, name and comment. A compliant decompressor must examine FHCRC, but only so it can skip the field; it need not check the value. RFC 1952 adds that versions of gzip up to 1.2.4 never set the FHCRC bit, though gzip 1.2.4’s documentation gave bit 1 a different meaning. gzip 1.13 does check the value (see the GNU gzip reader).
The trailer and CRC-32
| Offset | Size | Name | Description |
|---|---|---|---|
0 | 4 | CRC32 | The CRC-32 of the uncompressed data. |
4 | 4 | ISIZE | The size of the uncompressed data, modulo 232. |
The CRC-32 is that of ISO 3309 and ITU-T V.42: the reflected polynomial
0xEDB88320, a starting value of all ones, and the result inverted. The
value for the ASCII digits 123456789 is CBF43926. The Deflate
data itself is described on the Deflate
page.
Several members
Files are made by appending members, for example by cat a.gz b.gz or by
tools such as bgzip that write many small ones. A decompressor treats them
as one stream, joining their outputs in order. The first bytes after a member’s
trailer decide what follows: another 1F 8B member, zeros (tape padding,
which gunzip ignores) or anything else (which gunzip reports as trailing garbage).
Compliance rules (RFC 1952, 2.3.1.2)
A compressor must write correct ID1, ID2, CM, CRC32 and ISIZE, and must set every reserved bit to zero. It may set all the other fields of the fixed-length part of the header to default values: 255 for OS and 0 for the rest, FLG, MTIME and XFL. A decompressor must check ID1, ID2 and CM and report an error if any of them is wrong, and must report an error if any reserved flag bit is set, since such a bit could mean a new field that would make the rest of the data be read wrongly. It must examine FEXTRA (and XLEN), FNAME, FCOMMENT and FHCRC, at least so that it can skip the optional fields that are present. It need not examine any other part of the header or trailer: it may ignore FTEXT and OS, for instance, and always produce binary output.
What GNU gzip 1.13 does with a member
Read from gzip.c (the function that reads a header), gzip.h and unzip.c. These are the program’s behaviour, not the format’s rules.
- Magic. It accepts
1F 8Band also1F 9E(named “gzip 0.5 = freeze 1.x”) as a gzip header. It also recognises1F 1E(pack),1F 9D(compress,.Z;lzw.h),1F A0(SCO LZH) and a PKZIP file (PK, when it is the first and only content, whose first entry is stored or deflated and not encrypted), and unpacks those. A file with no known magic is “not in gzip format” (and with--force --stdoutis copied unchanged). - CM. Any method other than 8 is “unknown method … not supported”.
- FLG. Bit 5 set: “is encrypted — not supported”. Bits 6 and 7 set: “has flags 0x… — not supported”; the first stops reading the member, the second also stops unless force was given twice.
- MTIME. A non-zero value that is too large for the platform’s time type gets a warning and the largest representable time. By default when decompressing, gzip ignores the stored name and time (the
-ndefault), unless-Nis given. - XFL and OS are read and ignored.
- FEXTRA is skipped by its XLEN (“extra field of N bytes ignored” in verbose mode); the subfields are not read.
- FNAME is read up to its zero byte (and a name too large for the buffer is “corrupted input — file name too large”); FCOMMENT is skipped.
- FHCRC. The CRC-16 is computed over the header bytes read and compared with the stored one; a mismatch is the error “header checksum … != computed checksum”, and the member stops unless force was given twice.
- Trailer. After the Deflate data it reads 8 bytes. A CRC-32 that differs from the output’s is “invalid compressed data — crc error”; an ISIZE that differs from the output length modulo 232 is “invalid compressed data — length error”. A Deflate stream that violates the format is “invalid compressed data — format violated”.
- After a member. The next two bytes are read as a new header. If they are not a known magic: zero bytes to the end of the file are ignored (“trailing zero bytes ignored”, shown only in verbose mode); anything else gives the warning “decompression OK, trailing garbage ignored” and reading stops.
In the explorer
Members and their Deflate data are read whole while the output stays under 64 MiB,
and past that a member’s end is found by decoding without keeping its output (only a
32 KiB window is held), however much output there is: every member’s end, CRC-32 and
size are found and checked when the file is opened, whatever the size of the file.
BGZF members are found by their sizes without decoding. Zeros after the last member
(however many) are shown as padding, which gunzip ignores; other bytes with no member after
them are shown as trailing garbage and named as an issue. A member whose header is
refused, whose Deflate data cannot be read, or whose data ends at a place its trailer
does not confirm (a flipped bit that still decodes, or a cut that runs into the next
member) ends where the next member that reads and checks out starts, found by
searching for the magic 1F 8B and decoding from each place: the bytes
before it stay in the tree as that member’s unread data, the trailer read where the
data ended is not trusted, and the members after it are read and open as usual
(gunzip itself stops at the first damaged member). If the damaged member’s data happens
to hold a complete gzip member of its own (one that reads, inflates and matches its
CRC-32 and size), that inner member is taken for the next real member, since its bytes
are one.
Bytes that are not a member between two valid members are shown as their own node. gunzip stops there (“trailing garbage ignored”), so the joined payload stops there too; the members after the junk are still found, marked “not read by gunzip” in the tree and the Members table, and each opens as an entry of its own without being joined into the payload.
Not described
Unknown
- Reference and viewer: the contents of other extra-field (FEXTRA)
subfields. RFC 1952 lets anyone define them and says Jean-Loup Gailly keeps a registry, which
was not found (RFC 1952, the gzip 1.13 sources and a web search were tried; only
Ap,BCandRAare documented in what was read). The IDsAC,RO,cp,GS,KNandMcthat other write-ups mention are not confirmed by any source read, so they are not listed. Shown as labelled bytes. - Viewer: every problem is kept: the problems list shows the first 500 and pages through the rest, with the true count. The Compression table lists the first 2,000 member streams and its title says how many there are; every member’s stream also opens from its node in the tree, and the Members table pages through all members.
Links
Software
- GNU gzip – the standard command-line compressor (macOS, Linux, Windows)
- 7-Zip – free archiver that opens and makes .gz files (Windows)
- WinRAR – archiver that extracts .gz files (Windows)
- PeaZip – free archiver for many formats (Windows, macOS, Linux)
- Archive Utility – the Mac’s built-in extractor, which opens .gz files (macOS)