3 The deflation algorithm used by zip and gzip is a variation of
4 Lempel-Ziv 1977 [LZ77]. It finds duplicated strings in
5 the input data. The second occurrence of a string is replaced by a
6 pointer to the previous string, in the form of a pair (distance,
7 length). Distances are limited to 32K bytes, and lengths are limited
8 to 258 bytes. When a string does not occur anywhere in the previous
9 32K bytes, it is emitted as a sequence of literal bytes. (In this
10 description, 'string' must be taken as an arbitrary sequence of bytes,
11 and is not restricted to printable characters.)
13 Literals or match lengths are compressed with one Huffman tree, and
14 match distances are compressed with another tree. The trees are stored
15 in a compact form at the start of each block. The blocks can have any
16 size (except that the compressed data for one block must fit in
17 available memory). A block is terminated when zip determines that it
18 would be useful to start another block with fresh trees. (This is
19 somewhat similar to compress.)
21 Duplicated strings are found using a hash table. All input strings of
22 length 3 are inserted in the hash table. A hash index is computed for
23 the next 3 bytes. If the hash chain for this index is not empty, all
24 strings in the chain are compared with the current input string, and
25 the longest match is selected.
27 The hash chains are searched starting with the most recent strings, to
28 favor small distances and thus take advantage of the Huffman encoding.
29 The hash chains are singly linked. There are no deletions from the
30 hash chains, the algorithm simply discards matches that are too old.
32 To avoid a worst-case situation, very long hash chains are arbitrarily
33 truncated at a certain length, determined by a runtime option (zip -1
34 to -9). So zip does not always find the longest possible match but
35 generally finds a match which is long enough.
37 zip also defers the selection of matches with a lazy evaluation
38 mechanism. After a match of length N has been found, zip searches for a
39 longer match at the next input byte. If a longer match is found, the
40 previous match is truncated to a length of one (thus producing a single
41 literal byte) and the longer match is emitted afterwards. Otherwise,
42 the original match is kept, and the next match search is attempted only
45 The lazy match evaluation is also subject to a runtime parameter. If
46 the current match is long enough, zip reduces the search for a longer
47 match, thus speeding up the whole process. If compression ratio is more
48 important than speed, zip attempts a complete second search even if
49 the first match is already long enough.
51 The lazy match evaluation is not performed for the fastest compression
52 modes (speed options -1 to -3). For these fast modes, new strings
53 are inserted in the hash table only when no match was found, or
54 when the match is not too long. This degrades the compression ratio
55 but saves time since there are both fewer insertions and fewer searches.
60 The gzip file format was standardized in Internet RFC 1952 [RFC1952].
61 This section briefly describes the format and comments on some
62 implementation details.
64 The pkzip format imposes a lot of overhead in various headers, which
65 are useful for an archiver but not necessary when only one file is
66 compressed. gzip uses a much simpler structure. Numbers are in little
67 endian format, and bit 0 is the least significant bit.
68 A gzip file is a sequence of compressed members. Each member has the
71 2 bytes magic header 0x1f, 0x8b (\037 \213)
72 1 byte compression method (0..7 reserved, 8 = deflate)
74 bit 0 set: file probably ascii text
75 bit 1 set: header CRC-16 present
76 bit 2 set: extra field present
77 bit 3 set: original file name present
78 bit 4 set: file comment present
80 4 bytes file modification time in Unix format
81 1 byte extra flags (depend on compression method)
82 1 byte operating system on which compression took place
84 2 bytes optional part number (second part=1)
85 2 bytes optional extra field length
86 ? bytes optional extra field
87 ? bytes optional original file name, zero terminated
88 ? bytes optional file comment, zero terminated
89 2 bytes optional 16-bit header CRC
90 ? bytes compressed data
92 4 bytes uncompressed input size modulo 2^32
94 The format was designed to allow single pass compression without any
95 backwards seek, and without a priori knowledge of the uncompressed
96 input size or the available size on the output media. If input does
97 not come from a regular disk file, the file modification time is set
98 to the time at which compression started.
100 The time stamp is useful mainly when one gzip file is transferred over
101 a network. In this case it would not help to keep ownership
102 attributes. In the local case, the ownership attributes are preserved
103 by gzip when compressing/decompressing the file. A time stamp of zero
106 Bit 0 in the flags is only an optional indication, which can be set by
107 a small lookahead in the input data. In case of doubt, the flag is
108 cleared indicating binary data. For systems which have different
109 file formats for ascii text and binary data, the decompressor can
110 use the flag to choose the appropriate format.
112 The extra field, if present, must consist of one or more subfields,
113 each with the following format:
115 subfield id : 2 bytes
116 subfield size : 2 bytes (little-endian format)
119 The subfield id can consist of two letters with some mnemonic value.
120 Please send any such id to <gzip@gnu.org>. Ids with a zero second
121 byte are reserved for future use. The following ids are defined:
123 Ap (0x41, 0x70) : Apollo file type information
125 The subfield size is the size of the subfield data and does not
126 include the id and the size itself. The field 'extra field length' is
127 the total size of the extra field, including subfield ids and sizes.
129 It must be possible to detect the end of the compressed data with any
130 compression format, regardless of the actual size of the compressed
131 data. If the compressed data cannot fit in one file (in particular for
132 diskettes), each part starts with a header as described above, but
133 only the last part has the crc32 and uncompressed size. A decompressor
134 may prompt for additional data for multi-part compressed files. It is
135 desirable but not mandatory that multiple parts be extractable
136 independently so that partial data can be recovered if one of the
137 parts is damaged. This is possible only if no compression state is
138 kept from one part to the other. The compression-type dependent flags
141 If the file being compressed is on a file system with case insensitive
142 names, the original name field must be forced to lower case. There is
143 no original file name if the data was compressed from standard input.
145 Compression is always performed, even if the compressed file is
146 slightly larger than the original. The worst case expansion is
147 a few bytes for the gzip file header, plus 5 bytes every 32K block,
148 or an expansion ratio of 0.015% for large files. Note that the actual
149 number of used disk blocks almost never increases.
156 [LZ77] Ziv J., Lempel A., "A Universal Algorithm for Sequential Data
157 Compression", IEEE Transactions on Information Theory, Vol. 23, No. 3,
158 May 1977, pp. 337-343.
160 [RFC1952] Deutsch P., "GZIP file format specification version 4.3",
161 Internet RFC 1952, May 1996, <http://www.ietf.org/rfc/rfc1952.txt>.
163 APPNOTE.TXT documentation file in PKZIP 1.93a (October 1991). This
164 version no longer seems to be available online; the latest version is
165 in <http://www.pkware.com/documents/casestudies/APPNOTE.TXT>.