Introduction
A .Z file, with a capital Z, was made by compress, the
standard Unix compression program of the 1980s and early 1990s. It uses LZW, the same
method as GIF images. Patent claims on LZW led the GNU
project to write gzip as a free replacement, and
gzip, which also compresses better, took over. Old software archives still hold
.tar.Z files, and gzip -d can still unpack them.
Structure
0Magic1F 9D2 bytes2Flags the low 5 bits give the largest code size, 9 to 16 as written by compress (ncompress also reads 0 to 8 as 9-bit codes); the top bit marks block mode1 byte3LZW codes packed least significant bit firstthe rest
Codes start 9 bits wide and grow by one bit each time the table of strings fills up, up to the largest size in the header. In block mode, which nearly all files use, code 256 clears the table so the compressor can start again when the data changes. There is no checksum and no stored file name or size. The header and LZW codes page explains the codes, including the padding after each width change.
In the explorer
Drop a .Z or .taz file on this site. The structure tree shows the
magic, the flags byte (largest width, block mode, the unused bits) and the LZW codes as runs
of codes of one width, each with the padding that compress writes after a width change or a
CLEAR code. The Runs table lists every run (width, number of codes, what ends it), and the
Codes table lists every code with its bit position, width and meaning: a byte, CLEAR, or a
string number. The decompressed data opens as a file of its own, in its own viewer:
.tar.Z and .taz as a tar archive. Bad flags, a corrupt code or a cut
file stop the decoding at that point, with the reason, and the tree shows what was read.
A file with a largest width of 9 can follow two layouts that differ after the table first fills
(ncompress keeps 9-bit codes; BSD and macOS compress -b 9 go to 10 bits, see
the LZW page). The viewer decodes both ways and picks by the
look of the output; where the file alone cannot decide (for example data that is all one byte) it uses the
BSD rule and says it was a guess. This is a limit of the file format itself, not of the viewer.
Not part of the format: the compressor’s hash table and the compression-ratio test that decides when to
emit CLEAR are encoder choices; a decoder simply obeys whatever codes arrive. Comments in
compress.c mention headerless streams and output compatible with compress 2.0, but the current
source implements neither (the nomagic variable is never read and there is no -C
case), so the only format compress.c reads and writes is the one with the three-byte header
described here.