← All tools

LZW Compressor

Compress and decompress text with LZW (Lempel–Ziv–Welch), the dictionary-based algorithm at the heart of GIF, TIFF, and the original UNIX compress. LZW builds its dictionary on the fly: as it scans the input it emits a code for the longest phrase already seen, then adds that phrase plus the next character as a new dictionary entry — so recurring patterns get ever-shorter codes the more they repeat. Watch the emitted code sequence, the packed byte stream (variable-width 9→12-bit codes), the compression ratio, and a live decompression that verifies the round-trip. Everything runs locally in your browser.

Input text

Result

Emitted code sequence

Packed bytes (hex)

Round-trip verification

How LZW works. The dictionary starts with one entry per byte (codes 0–255). The encoder keeps the longest prefix P that is already in the dictionary; when P + c is not found it emits the code for P, assigns P + c the next free code (starting at 256), and restarts P = c. The decoder rebuilds the same dictionary from the codes alone — each new entry is the previous output plus the first character of the current one. The one subtle case is KwKwK: when a code refers to an entry that has not been added yet, the entry is the previous output plus its own first character (this only happens for phrases of the form cωc). Codes are packed into a bit stream that grows from 9 to 12 bits as the dictionary fills (late-change timing, kept in sync between writer and reader). LZW is lossless: decompress(compress(x)) always equals x. Pairs with the Run-Length Encoding, Huffman Coding, and Burrows–Wheeler Transform tools. Everything runs locally — nothing leaves your browser.