Introduction
LZ4 is a compression method built for speed rather than
size. Yann Collet released it in 2011. It compresses less than
gzip but decompresses many times faster, often at
several gigabytes a second, so it is used where data must be packed and unpacked on the
fly: in file systems, databases, game assets, the Linux kernel and network protocols.
An .lz4 file uses the LZ4 frame format, which wraps the compressed blocks
with a header and checks.
Structure
Numbers are little-endian. A file can hold several frames one after another.
0Magic04 22 4D 184 bytes4Frame descriptor flag byte, block size byte, optional content size and dictionary ID, header checksum3 to 15 bytes…Data blocks each a 4-byte size, the data, and an optional checksumrepeated…End mark four zero bytes4 bytes…Content checksum xxHash-32 of the original data, if the flags ask for it0 or 4 bytes
The top bit of a block’s size means the block is stored uncompressed. Inside a
compressed block, data is a series of sequences, each some literal bytes followed by a
copy of earlier data, with no entropy coding, which is why decoding is so fast. Old files
from the first lz4 tool use a legacy frame with the magic
02 21 4C 18. The frame and
block format pages give every field.
In the explorer
Drop an .lz4 file on this site. The structure tree shows every frame: LZ4
frames (magic, FLG and BD with every flag, content size, dictionary ID, header checksum),
each data block (size and stored flag, data, block checksum), the end mark and the
content checksum; legacy frames with their blocks; and skippable frames with their magic
nibble and user data. A compressed block opens into its sequences (token, literal length
bytes, literals, offset, match length bytes), and a Sequences table lists every one. All
four checksums that the format has (header, block, content, and the content size) are
checked against the decoded data, and the frames and blocks have tables of their own.
Matches into a dictionary that the file does not hold are labelled with its ID and listed in a
Dictionary matches table with their raw bytes.
The decompressed data of every frame, joined in order, opens as a file of its own, in its
own viewer: .tar.lz4 as a tar archive. A damaged or cut file shows what was
read, with the rest labelled as unread and the reason.
Links
Software
Developers
- liblz4 – the reference library (C)
- python-lz4 – bindings to liblz4 (Python)
Reference
- LZ4 Frame Format Description – the frame specification in the lz4 repository