The .Z format has no written specification; it is defined by the
compress program. This page follows the source of the
ncompress package, which carries it on
(compress.c,
read 2026-11-12).
Header
| Offset | Size | Description |
|---|---|---|
0 | 2 | Magic 1F 9D. |
2 | 1 | Flags: bits 0 to 4 the largest code width, normally 9 to 16 (compress’s default is 16; ncompress also accepts 0 to 8 and reads them as 9-bit codes); bits 5 and 6 unused (the source calls them free, and suggests 20 might one day mean a fourth header byte); bit 7 (80) block mode. |
3 | … | LZW codes. |
The source notes that the original compress wrote no magic number at all; comments
on the -n and -C options mention headerless files and output
compatible with compress 2.0, but the current code implements neither: the
only stream it reads or writes has the three-byte header. A file with block mode off cannot reset its table.
The -b option sets the largest width, between 9 and 16.
Codes
- Codes are packed least significant bit first: the first code’s low bit is bit 0 of byte 3.
- Codes 0 to 255 are single bytes. In block mode, code 256 is CLEAR and the first new string gets code 257; without block mode new strings start at 256.
- Codes start 9 bits wide. Each new code adds a table entry (the previous string plus the first byte of the current one). When the next free code would not fit in the current width, the width grows by 1, up to the largest width in the header. When the table is full, no more entries are added.
- A code equal to the next free code (not yet in the table) means the previous string plus its own first byte (the “KwKwK” case).
- CLEAR empties the table and sets the width back to 9. compress writes it, in block mode, when the compression ratio falls after the table has filled; it checks the ratio every 10,000 input bytes.
Padding after a width change
compress writes codes in groups of eight, so a group of n-bit codes fills n bytes exactly. When the width changes, or after a CLEAR, the rest of the current group is padded: the decoder must skip forward to the next multiple of n × 8 bits, counted from the start of the codes at the old width. Decoders that ignore this produce garbage after the first width change. The end of the file needs no special marker; the last code ends within the last byte.
The decoding procedure
From decompress() in compress.c (read 2026-11-13):
- Read the 3 header bytes. If fewer than 3 arrive, or the magic is not
1F 9D, the input is “not in compressed format”.maxbits = flags & 1F,block_mode = flags & 80; amaxbitsabove the compiled-in 16 is an error (“can only handle 16 bits”). maxmaxcode = 1 << maxbits. Start withn_bits = 9andmaxcode = (1 << 9) − 1 = 511(ncompress only: ifmaxbitsis 9,maxcode = maxmaxcodefrom the start, so the width never changes; see a largest width of 9). Amaxbitsof 0 to 8 is not an error in ncompress:maxmaxcodeis then 256 or less, no table entry is ever added and every code stays 9 bits wide, so the stream is plain 9-bit codes (and CLEAR). BSDzopen.c(FreeBSD, macOS) refuses anymaxbitsbelow 12 (EFTYPE). No compressor writes these values; the viewer decodes them as ncompress does and labels them. The table has the 256 single-byte entries;free_ent = 257in block mode, otherwise 256.oldcode = −1.- Before each code: if
free_ent > maxcode, skip to the next multiple ofn_bits × 8bits (the padding rule below), increasen_bitsby 1, and setmaxcode = maxmaxcodewhenn_bits == maxbits, else(1 << n_bits) − 1. - Read an
n_bits-bit code, least significant bit first. The first code (whenoldcodeis −1) must be below 256, is output as a byte and becomesoldcodeand the last byte (finchar); a larger first code is corrupt input. - If the code is CLEAR (256) and block mode is on: empty the table’s prefixes, set
free_ent = 256(FIRST − 1, not 257), skip to the next multiple ofn_bits × 8bits and restoren_bits = 9with itsmaxcode.oldcodeis not reset: the first code after the clear makes one table entry at index 256 (using the code from before the clear), after which entries continue from 257. - Otherwise remember
incode = code. Ifcode ≥ free_ent: a code abovefree_entis corrupt input; a code equal tofree_entis the KwKwK case, output string(oldcode) followed byfinchar. Else output the string for the code: follow prefix links down to a code below 256 (the stack gives the bytes in reverse), and setfincharto that first byte. - If
free_ent < maxmaxcode, add the entry (prefixoldcode, suffixfinchar) atfree_entand increment it. Setoldcode = incode.
The padding formula in the source is posbits = (posbits − 1) + ((n_bits << 3) − (posbits − 1 +
(n_bits << 3)) % (n_bits << 3)), with posbits counted in bits from the start of the
current input buffer. The compressor starts with maxbits | 80 as the flag byte (block mode on) and, in block
mode, checks every 10,000 input bytes (CHECK_GAP) whether to clear.
A largest width of 9
The programs disagree here, so the same flag byte (89 or 09) can mean two layouts
(all read 2026-11-14):
- ncompress (
compress.c): whenn_bits == maxbitsat the start, the decompressor setsmaxcode = maxmaxcode(512), so the width stays 9 for ever and, once the table holds 512 entries, the compressor adds no more. - BSD (FreeBSD
usr.bin/compress/zopen.c, which is the macOScompress):maxcode = MAXCODE(n_bits = 9)= 511 whatevermaxbitsis, and the width test is made only aftern_bits++. The table still fills to 512, which is above 511, so the width goes to 10 (the testn_bits == maxbitscan no longer be true, somaxcodebecomes 1023) and stays 10 until a CLEAR sends it back to 9. The padding rule applies at that change as at any other. The same program’s own decompressor refuses a file whosemaxbitsis below 12 (“Inappropriate file type or format”), so it cannot read its own-b 9output.
Until the table first fills the two streams are identical. After that a 9-bit reader and a 10-bit reader go out of step without any code being invalid (a 9-bit code never exceeds 511), so the file alone cannot say which program wrote it. The viewer decodes both ways, keeps the one whose output looks like data rather than noise, prefers the BSD rule when the two cannot be told apart, and says in the flags line which rule it used and whether the choice was a guess.
Example
1F 9D magic
90 flags: block mode (80), largest width 16 (10)
… 9-bit codes, least significant bit first
There is no checksum, file name or original size, so damage can only be seen when decoding fails.