Each member names its own method in a two-byte field in its local and central headers, so one archive can mix methods. In practice almost all members are stored or Deflate.
Method numbers
| Number | Method |
|---|---|
0 | Stored (no compression) |
1 | Shrink |
2 to 5 | Reduce, factors 1 to 4 |
6 | Implode |
7 | Reserved for a tokenizing algorithm, not used by PKZIP |
8 | Deflate |
9 | Enhanced Deflate (Deflate64) |
10 | PKWARE Data Compression Library Imploding (old IBM TERSE) |
11 | Reserved by PKWARE |
12 | bzip2 |
13 | Reserved by PKWARE |
14 | LZMA |
15 | Reserved by PKWARE |
16 | IBM z/OS CMPSC |
17 | Reserved by PKWARE |
18 | IBM TERSE (new) |
19 | IBM LZ77 z Architecture |
20 | Deprecated: use 93 for Zstandard |
93 | Zstandard |
94 | MP3 |
95 | XZ |
96 | JPEG variant |
97 | WavPack |
98 | PPMd version I, revision 1 |
99 | AE-x encryption marker, not a method: the real method is in the 0x9901 extra field |
APPNOTE says methods 1 to 6 are legacy and should not be used to compress new files. A method’s minimum version is in the version table. The explorer shows the method of every member and decompresses Deflate and stored data; it shows other methods’ data as bytes.
What the sizes count
The compressed size is the whole of what is stored for the member. It includes the method’s own header, any end marker, and the encryption header or trailer. A member’s stored bytes start immediately after its local header, with no alignment other than padding in its extra field. The CRC-32 and uncompressed size are of the original data.
Stored (method 0)
The file’s bytes, unchanged. The compressed and uncompressed sizes are equal, unless the member is encrypted.
Deflate (method 8)
Raw Deflate as defined in RFC 1951, with no zlib header or trailer. The bit stream reads the low bits of each byte first. It is a series of blocks, each starting with three bits.
| Bits | Meaning |
|---|---|
| 1 | BFINAL: 1 if this is the last block. |
| 2 | BTYPE: 00 stored, 01 fixed Huffman, 10 dynamic Huffman, 11 an error. |
A stored block skips to the next byte boundary, then has a 16-bit length and its one’s complement, then that many literal bytes. Huffman blocks hold literals and length and distance pairs, ending with code 256. The window is 32 KiB. Since the stream ends at its last block, a reader knows where a Deflate member ends without the sizes.
Fixed Huffman code
| Literal values | Code length in bits |
|---|---|
| 0 to 143 | 8 |
| 144 to 255 | 9 |
| 256 to 279 | 7 |
| 280 to 287 | 8 |
Distance codes 0 to 31 are all 5 bits. Literal codes 286 and 287 and distance codes 30 and 31 are never used.
Dynamic Huffman header
| Bits | Meaning |
|---|---|
| 5 | Number of literal and length codes minus 257 (257 to 286). The value 0 means 257. |
| 5 | Number of distance codes minus 1 (1 to 32). |
| 4 | Number of code length codes minus 4 (4 to 19). |
Then the code length codes, 3 bits each, in the order 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15. These encode the literal and distance code lengths, using 16 for “repeat the previous length 3 to 6 times” (2 extra bits), 17 for a run of 3 to 10 zeros (3 extra bits) and 18 for a run of 11 to 138 zeros (7 extra bits). Codes are assigned from the shortest length up, all zeros first.
Length and distance codes
| Length codes | Extra bits | Lengths |
|---|---|---|
| 257 to 264 | 0 | 3 to 10 |
| 265 to 268 | 1 | 11 to 18 |
| 269 to 272 | 2 | 19 to 34 |
| 273 to 276 | 3 | 35 to 66 |
| 277 to 280 | 4 | 67 to 130 |
| 281 to 284 | 5 | 131 to 257 |
| 285 | 0 | 258 |
| Distance codes | Extra bits | Distances |
|---|---|---|
| 0 to 3 | 0 | 1 to 4 |
| 4 to 5 | 1 | 5 to 8 |
| 6 to 7 | 2 | 9 to 16 |
| 8 to 9 | 3 | 17 to 32 |
| 10 to 11 | 4 | 33 to 64 |
| 12 to 13 | 5 | 65 to 128 |
| 14 to 15 | 6 | 129 to 256 |
| 16 to 17 | 7 | 257 to 512 |
| 18 to 19 | 8 | 513 to 1,024 |
| 20 to 21 | 9 | 1,025 to 2,048 |
| 22 to 23 | 10 | 2,049 to 4,096 |
| 24 to 25 | 11 | 4,097 to 8,192 |
| 26 to 27 | 12 | 8,193 to 16,384 |
| 28 to 29 | 13 | 16,385 to 32,768 |
APPNOTE says flag bits 1 and 2 record the compression level the writer used.
Deflate64 (method 9)
Deflate with a 64 KiB window. It differs from method 8 in the decoding tables only:
- Length code 285 has 16 extra bits and a base of 3, giving lengths 3 to 65,538, instead of the single value 258.
- Distance codes 30 and 31 become valid, with 14 extra bits each and bases of 32,769 and 49,153, giving distances up to 65,536.
APPNOTE does not give the tables. These values are from zlib’s
contrib/infback9. Version needed is 2.1.
Shrink (method 1)
Dynamic LZW with partial clearing. Codes start at 9 bits and can grow to 13. The code 256 introduces control sequences:
256, 1: the code size grows by one. The next code is read at the new size, with no alignment.256, 2: partial clear. Leaf nodes are removed from the table, the code size stays as it is, and freed codes are used again from the lowest up. The compressor can send this at any time.
Reduce (methods 2 to 5)
Two stages: repeated-byte compression, then probabilistic coding of the result. The four methods differ only in compression factor, 1 to 4.
The data starts with 256 follower sets, S(255) first and S(0) last. Each is a 6-bit count N, 0 to 32, followed by N 8-bit values. Then the coded bytes follow. Last-Character starts at 0. For each byte:
- If the set for Last-Character is empty, read 8 bits as a literal.
- Otherwise read 1 bit. A 1 means an 8-bit literal follows. A 0 means read B(N) bits as an index and output that element of S(Last-Character), where B(N) is the minimum number of bits to encode N − 1.
The output of that stage is fed to a small state machine that expands repeated bytes.
The byte 144 is DLE:
| State | Input | Action |
|---|---|---|
| 0 | Any byte other than 144 | Output it. Stay in state 0. |
| 0 | 144 | Go to state 1. |
| 1 | 0 | Output 144 as a literal. Go to state 0. |
| 1 | V, not 0 | Set Len = L(V). If F(Len) = 2, go to state 2; if it is 3, go to state 3. |
| 2 | C | Len = Len + C. Go to state 3. |
| 3 | C | Copy Len + 3 bytes from distance D(V, C). Bytes before the start of output are zeros. |
| Factor | Method | L(X) | F(X) | D(X, Y) |
|---|---|---|---|---|
| 1 | 2 | low 7 bits | 2 if X = 127, else 3 | (upper 1 bit of X) × 256 + Y + 1 |
| 2 | 3 | low 6 bits | 2 if X = 63, else 3 | (upper 2 bits of X) × 256 + Y + 1 |
| 3 | 4 | low 5 bits | 2 if X = 31, else 3 | (upper 3 bits of X) × 256 + Y + 1 |
| 4 | 5 | low 4 bits | 2 if X = 15, else 3 | (upper 4 bits of X) × 256 + Y + 1 |
Implode (method 6)
A sliding dictionary with Shannon-Fano coding, set by flag bits 1 and 2. Bit 1 selects an 8 KiB or 4 KiB dictionary. Bit 2 selects three trees or two. With three trees, a literal tree of 256 entries comes first, followed by a length tree and a distance tree of 64 entries each. With two trees, literals are 8 raw bits.
The minimum match length is 3 with a literal tree, otherwise 2. Each tree is stored
as a byte count minus 1, then bytes each holding a high nibble (the number of codes with
this length, minus 1) and a low nibble (the bit length, minus 1). Codes are then assigned
from the longest length downwards, with their bits reversed, and returned to the original
order. For example, the bytes 02 42 01 13 give lengths 3, 3, 3, 3, 3, 2, 4, 4.
- A 1 bit: a literal, decoded with the literal tree or read as 8 raw bits.
- A 0 bit: a match. Read 7 low distance bits for an 8 KiB dictionary, or 6 for 4 KiB, then decode the upper 6 bits of the distance with the distance tree, then the length with the length tree. Add the minimum match length. If the length decoded equals 63 plus the minimum, read 8 more bits and add them. Copy that many bytes from distance + 1 bytes back.
bzip2 (method 12)
The member’s data is a complete bzip2 stream, with
its BZh header. APPNOTE refers to the external algorithm and gives no
framing of its own. Version needed is 4.6.
LZMA (method 14)
Not the same as a .lzma file. A small header comes first, then
raw LZMA1 data. Version needed is 6.3.
| Offset | Size | Name | Description |
|---|---|---|---|
0 | 1 | SDK major version | The LZMA SDK version of the writer. Readers ignore it. |
1 | 1 | SDK minor version | Readers ignore it. |
2 | 2 | Properties size | The size of the properties that follow: 5 in SDK 4.3. |
4 | 1 | Properties byte | The packed values of lc, lp and pb: (pb × 5 + lp) × 9 + lc. |
5 | 4 | Dictionary size | Little-endian. |
9 | varies | Data | LZMA1 data, without the 8-byte uncompressed size of a .lzma file. |
Flag bit 1 says the data ends with an end-of-stream marker. APPNOTE says one is optional but recommended. When bit 1 is clear, the reader stops at the uncompressed size from the header.
Python’s zipfile writes the version bytes 9 and 4; libzip writes other
values and does not check them on reading. An encryption header, if any, comes before
the LZMA header.
PPMd (method 98)
PPMd version I, revision 1 only. The compressed data follows a little-endian two-byte parameter word.
| Bits | Meaning |
|---|---|
| 0 to 3 | Model order minus 1: order 2 to 16, default 8. |
| 4 to 11 | Sub-allocator size minus 1, in megabytes: 1 to 256, default 50. |
| 12 to 15 | Restoration method: 0 restart (default), 1 cut off, 2 freeze. |
Version needed is 6.3.
Zstandard (method 93)
The member’s data is a standard Zstandard frame, as
libzip reads and writes it. Number 20 was used before 93 was assigned and is now
deprecated. APPNOTE’s version table does not list Zstandard. Python’s
zipfile writes version needed 63. libzip writes 20 because, its comment says,
WinZip refuses other values.
XZ (method 95)
The member’s data is a complete .xz stream. libzip writes it with an LZMA2 filter and a CRC-64 check and no extra header. It also writes version needed 20.
WavPack (method 97)
The data is WavPack. The method field is 97 in both headers and the version fields match Deflate’s. Sample bytes should include unused bits, so a 12-bit sample is passed as 16 bits.
IBM methods (16, 18, 19)
For mainframes. Method 16, z/OS CMPSC, uses hardware compression and needs an expansion
dictionary stored in the member. A header comes first: a version byte (1), a flags and
symbol-size byte (high 4 bits flags, 0x80 expansion dictionary,
0x40 Deflate-compressed; low 4 bits symbol size, 9 to 13), and a 4-byte
dictionary length. Then the dictionary, then the compressed data. If encrypted, these parts
are encrypted as one unit. APPNOTE gives no details for methods 18 and 19, or for method 10.