1. Tuntau
  2. Learn
  3. Specification

Horizon file format: .hz and .hzn

How Tuntau Horizon's compressed files (.hz) and archives (.hzn) are laid out, so other archivers, file managers and tools can recognise and read them.

Format 1.0 (frozen) Archive container .hzn v1 Last change 2026-11-16 English only, so there is one exact text to implement against.
On this page
  1. 1. Overview
  2. 2. Recognising the files
  3. 3. Conventions
  4. 4. Frames
    1. 4.1 Versioning
    2. 4.2 Main frame
    3. 4.3 Stored frame
    4. 4.4 Periodic frame
    5. 4.5 Web frame
    6. 4.6 Chunked stream
  5. 5. Inner streams
    1. 5.1 Leaf streams
    2. 5.2 BWT header
    3. 5.3 Transforms
    4. 5.4 Retired magics
  6. 6. Levels and streams
  7. 7. Floating point
  8. 8. The .hzn archive
    1. 8.1 Header
    2. 8.2 Index
    3. 8.3 Data model
    4. 8.4 Level byte
    5. 8.5 Paths and safety
    6. 8.6 Reading, step by step
  9. 9. Untrusted input
  10. 10. Conformance
  11. 11. Adding support

1. Overview

Tuntau Horizon writes two kinds of file:

  • .hz: a compressed file. One input in, one compressed output out, like .gz or .xz. The horizon command writes it (report.pdf → report.pdf.hz). .thz and .tar.hz are tar archives compressed this way.
  • .hzn: an archive. Many files and folders in one file, like .zip or .7z. The Horizon app and its hzn tool write it. Its data blocks are .hz streams (or stored bytes) and a small index lists the files.

So a reader for .hzn needs a reader for .hz. Sections 3 to 7 describe the .hz stream format, section 8 the .hzn container.

What is normative. This page specifies the container and stream framing: how a decoder finds out what a file holds, which stream types exist and how they nest. The entropy coders inside the leaf streams (context mixing, rANS, Huffman and LZ token coders) are defined by the reference implementation named next to each stream type (section 5.1), built with -ffp-contract=off. Where this page and the reference decoder disagree, the reference decoder's output is correct and this page is wrong: please tell us.

Stability. Format 1.0 is frozen. Every 1.x decoder decodes every file any 1.x encoder wrote, byte-exact. Newer 1.x minors may only add stream types or flag values (section 4.1). The product name may change; magics never do.

2. Recognising the files

Use the first bytes, not the extension. Every .hz stream starts with one of five first bytes (section 4); a .hzn archive starts with HZN\x1A.

2. Recognising the files
FileExtensionsFirst bytesMIME typemacOS UTI
Archive.hzn48 5A 4E 1A ("HZN\x1A")application/x-horizon-archivecom.tuntau.horizon.hzn
Compressed file.hz33 4B 57 42 10 (main frame), or 77, F0, F1, F3application/x-horizoncom.tuntau.horizon.hz-stream
Compressed tar.thz, .tar.hzas .hzapplication/x-horizon-compressed-tar(as .hz)

The compact first bytes (77, F0, F1, F3) are too common to identify a file by themselves. Sniff on 3KWB\x10 and HZN\x1A, and trust the extension for the rest, as the Linux shared-mime-info file the app installs does.

3. Conventions

  • u8, u16, u32, u64: unsigned integers, big-endian in .hz streams unless a stream says otherwise. (The .hzn container is little-endian: section 8.)
  • varint: unsigned LEB128. 7 bits per byte, least significant group first, high bit set on every byte but the last, at most 10 bytes.
  • CRC-32: the IEEE / zlib polynomial (0xEDB88320 reflected), initial value 0xFFFFFFFF, final XOR. It is the value zlib's crc32() returns.
  • Magic names. Inner stream types are identified by a u32 magic, written big-endian. The names used here (BWK3, BWZ1, …) read the constant from its low byte up, so the bytes on disk are the name reversed: magic BWK3 = constant 0x334B5742 = bytes 33 4B 57 42 = ASCII "3KWB". All magics share the low half 0x5742, so bytes 3 and 4 on disk are always "WB".

4. Frames

A .hz file is exactly one frame. Its first byte selects the frame type:

4. Frames
First byteFrameWritten for
0x77 ('w')web / small-file framesmall inputs at every level; -web up to 64 MiB
0xF0stored frameincompressible input
0xF1periodic frameinput that is one pattern of 1–255 bytes, repeated
0x33 ("3KWB")main frame BWK3everything else
0xF3chunked streaminput larger than the encoder's memory budget, or of unknown length (a pipe) and larger than one chunk

Frames do not nest, and no inner stream starts a file. Any other first byte means "not a Horizon file". Pre-release main frames (BWK1 / BWK2, first byte 0x31 / 0x32) must be refused with their own message ("made by a pre-release build").

4.1 Versioning

  • The main frame and the chunked stream carry a format version byte, (major << 4) | minor. 1.0 is 0x10. Accept a file if major equals yours and minor is not newer.
  • A newer minor may only add inner stream types (new magics) or new flag values. The layout of everything on this page never changes within major 1.
  • The three compact frames (0x77, 0xF0, 0xF1) have no version byte, because in them every byte counts (a 300-byte web response). Their first byte is the version: a changed layout gets a new first byte. Reserved for that: 0x78–0x7F (web frame successors) and 0xF2, 0xF4–0xFF (compact frames).
  • The web frame names its dictionary by id (section 4.5), so a new built-in dictionary is a new id, not a new frame.

4.2 Main frame BWK3

u32     magic 0x334B5742 ("3KWB" on disk)
u8      format version (0x10)
varint  n           original size in bytes
u32     CRC-32 of the original n bytes
...     one inner stream (section 5) that decodes to exactly n bytes

Check both the decoded size and the CRC-32; a mismatch is corruption.

4.3 Stored frame 0xF0

u8      0xF0
n bytes the original (n = file size - 5)
u32     CRC-32 of the original

4.4 Periodic frame 0xF1

u8      0xF1
varint  n           original size
u8      p           pattern length, 1..255 (0 is invalid)
p bytes pattern
u32     CRC-32 of the original

The original is the pattern repeated to n bytes, the last copy cut short. Verify the CRC before writing (it can be computed in O(log n) by CRC combination), so a corrupt n cannot make you write a huge output.

4.5 Web / small-file frame 0x77

u8      0x77
u8      flags   bit 7:    stored (payload = the original bytes; all other bits 0)
                bit 6:    payload is not lzr; then bits 0-2 pick 0 = lzf tokens, 1 = hzh
                bits 0-1: (lzr) literal-counter rate pair
                bit 2:    (lzr) mixed literals
                bits 3-5: dictionary id (0 = none, 1 = webdict1, 2 = webdict)
varint  n       original size, at most 64 MiB
...     payload
u32     CRC-32 of the original

Payload coders (reference sources in brackets):

  • hzh (hzh.h): Huffman-coded LZ with trained default tables. Matches may reach into the dictionary placed before the data. Used by -web and the small path of most levels.
  • lzr (lzr.h): range-coded LZ tokens with a trained prior. The small path of the dense and ultra levels.
  • lzf (lzf.h): copy-only LZ tokens.

Two built-in dictionaries are part of the format, each with its own lzr prior and hzh default tables. A decoder only has to unpack the dictionaries it meets. A stream naming an unknown id must be refused with "needs a newer version".

4.5 Web / small-file frame 0x77
IdSourceSizeDictionary CRC-32Prior CRC-32
1webdict1.h523,776 bytes0x8eaaf93f0x7e059920
2webdict.h7,915,008 bytes0x1b082f670x43e6e94a

Both dictionaries are built only from license-cleared sources.

4.6 Chunked stream 0xF3

Written when the input does not fit the encoder's memory budget, or arrives through a pipe and is longer than one chunk. One pass, no seeking, bounded memory on both sides: the decoder holds one chunk and its output.

u8      0xF3
u8      format version (0x10)
repeat:
  varint len        > 0: length of the next piece
  len bytes         one complete frame (any frame of section 4 except a chunked stream)
                    holding the next piece of the original
varint  0           end of the pieces
varint  n           original size (sum of the pieces)
u32     CRC-32 of the whole original
  • Each piece checks its own size and CRC; the trailer checks the whole.
  • A stream cut anywhere, including exactly between pieces, is corrupt (no trailer, or a wrong total). Bytes after the trailer are an error.
  • Pieces are independent (no shared model or window).
  • Refuse a piece larger than the memory you may use ("chunk larger than the memory budget").

5. Inner streams

An inner stream starts with its u32 magic. Transform streams (5.3) hold further inner streams. Nesting is at most 32 deep: refuse deeper nesting as corrupt.

5.1 Leaf streams (coders)

5.1 Leaf streams (coders)
MagicOn diskLevelLayout / reference
BWZ11ZWBspeedvarint n; per 4 MiB block a varint (len << 1) | stored; then the block data in order. Blocks are lzf.h v2 blocks (Huffman-coded literal, token and offset streams) or stored bytes.
BWF88FWBdefaultBWT stream, compact header (5.2); segments coded by fastrc.h frc2 (adaptive); bit-packed aux indexes. Inputs up to 128 MiB.
BWF66FWBdefaultBWT stream, fixed-width header with a u32 aux spacing; segments coded by frc2. Written for empty BWT input.
BWF77FWBdefaultAs BWF6; segments coded by static rANS tables delta-coded against the previous block (fastrc.h). Inputs over 128 MiB.
BWE11EWBbalancedBWT stream, compact header; segments in interleaved pairs coded by bal.h.
BWH11HWBdenseBWT stream, compact header; segments coded by dense.h.
BWCFFCWBultraBWT stream, fixed-width header; CM parameter block; segments coded by context-mixing model 4 (bwtcm.c, encode_segment_p with MV_CUR = 4). Floating point per section 7.
BWCGGCWBultraAs BWCF with the compact header. Blocks of 64 MiB and more compressed with 8 or more threads.
BWCDDCWB(read only)As BWCF with context-mixing model 3. Written by 1.0 release candidates.
BWCEECWB(read only)As BWCD with the compact header.
BWL33LWBdefault–ultraIndependent LZ blocks (lzr.h), decodable in parallel: u64 n, u32 block size, u32 block count, u32 length per block (bit 31: stored), block data. Chosen when LZ beats BWT (binaries, already-compressed data).
BWG11GWB(HTTP modes)BWL3 layout; blocks may match into all earlier blocks (global window).
BWR00RWBanyu64 n, then n stored bytes.

5.2 BWT stream header

Fields in order. H is a varint in the compact header (BWF8, BWE1, BWH1, BWCE, BWCG) and a u32 (or u64 where marked wide) in the fixed-width header (BWF6, BWF7, BWCD, BWCF).

u32      magic
[BWCD/BWCE/BWCF/BWCG only] CM parameter block:
         u8 count (19), u32 x 3 (float bits of the mixer learning rates),
         then count - 3 values H (rates, shifts)
H wide   original size (before the word transform)
         word-transform header (wrt.h): H on; if on: H prefix count, H escape byte,
         H per prefix, H word count, H dictionary length, H chunk count,
         then per chunk H wide encoded length, H wide decoded length
H wide   BWT input size n (fixed-width: u32 high, u32 low)
H        block size (compact: 0 = one block of n); at most 1,000,000,000
H        segment count
         aux spacing: compact: u8 log2(spacing), 1..30
                      BWF6/BWF7: u32 spacing
                      BWCD/BWCF: fixed 2^23 (8 MiB), not stored
H        per segment, its length (compact: all but the last, which is implied)
         per block, the libsais aux indexes (one per spacing step):
           compact: bit-packed, each in ceil(log2(block length + 1)) bits,
                    MSB first, byte-aligned per block
           fixed-width: u32 each
H        per segment, its coded length (bit 31 set: stored segment of raw BWT bytes)
         the segment data, in order

To decode: decode each segment with the level's coder into the BWT data; invert each block with its aux indexes (libsais unbwt_aux semantics); if the word transform was on, decode the result with wrt.h to the original size.

5.3 Transform streams

These wrap an inner stream with a reversible transform. Undo the inner stream first, then the transform.

5.3 Transform streams
MagicOn diskWritten forLayout
BWX11XWBexecutablesu32 filter flags (1 ARM64 BL, 2 x86 CALL/JMP, 4 ARM64 ADRP, 8 x86 Jcc, 16 applied per 1 MiB chunk; above 31 is invalid), then the inner stream of the filtered data (exef.h).
BWQ11QWBstructured binary data (tables, samples)varint n, varint header length, region list (rel.h), then the inner stream of the transformed data.
BWP44PWBembedded deflate (gzip, zlib, PNG, zip), Go flate and xz streams that can be rebuilt bit-exactlyu32 record count, records (type, positions, encoder settings, PNG pixel-model blobs: pz.h, pzgo.h, xz.h, pxm.h), then the inner stream of the data with those streams expanded.
BWV11VWBH.264 videou64 blob size, blob (avc.h: CABAC/CAVLC slices re-coded), then the inner stream of the rest.
BWC55CWBlong-range repeatsu64 n, u32 flags (0), u32 minimum copy length, then three streams (residual, diff, copy list), each a u64 length and a complete inner stream (lrm.h).
BWS11SWBdense / ultra inputs over 1.25 GiBPer segment: u64 length and a complete inner stream; a u64 0 ends the list.
BWD11DWBdisk images from 16 MiB (VHD, VHDX, qcow2, VMDK, VDI, raw images with many zero blocks)u64 n, u32 block size, u32 flags (0), map stream (u64 length + stream), u32 chunk count, per chunk a u8 residual-stream id and a u64 length, then the chunk streams (dsk.h). Only directly inside BWK3; its n must equal the frame's.

5.4 Retired magics

BWC3, BWC4, BWC6–BWCC, BWF4, BWF5, BWL1, BWL2, BWP1–BWP3, BWK1, BWK2 come from pre-release builds. A 1.x encoder never writes them and a 1.x decoder refuses them ("pre-release"). They will never be reused.

6. Levels and the streams they write

A decoder does not need to know the level: every stream describes itself. For reference, what the 1.0 encoder writes (outermost stream first, [ ] = only when it helps):

6. Levels and the streams they write
LevelSmall / text pathMain pipeline
-speedw (hzh, lazy parse) up to 256 KiBBWK3 [BWD1] [BWX1] [BWQ1] BWZ1
defaultw (hzh) for text up to 256 KiB; both paths tried up to 16 MiBBWK3 [BWD1] [BWV1] [BWP4] [BWX1] [BWC5] [BWQ1] BWF8 / BWF7 / BWL3 / BWR0
-balancedas defaultas default, BWT coder BWE1
-densew (lzr, mixed literals) up to 256 KiB; both tried up to 16 MiBas default plus [BWS1] after BWD1, BWT coder BWH1
-ultraas denseas dense, BWT coder BWCF / BWCG
-webw (hzh, optimal parse) for text up to 64 MiB and binaries up to 256 KiBlarger inputs: the default pipeline
-webfast, -webfastplus, -webturbow (hzh against dictionary id 1, quicker parses)larger inputs: the default pipeline

At every level, input that is one repeated pattern of up to 255 bytes becomes 0xF1, and input whose compressed form is not smaller becomes 0xF0. Output may depend on the thread count, never on the machine: the same command with the same thread count gives the same bytes on macOS, Linux and Windows, x86-64 and ARM64.

7. Floating point

Only the ultra level's context-mixing coder uses floating point: IEEE-754 single precision. Encoder and decoder must evaluate every expression exactly as written, in source order, without fused multiply-add contraction (-ffp-contract=off) and without fast-math. All other coders use integer arithmetic only.

8. The .hzn archive

A .hzn archive stores many files and folders. All its integers are little-endian, unlike the .hz streams inside it.

+--------------------+
| header (32 bytes)  |
+--------------------+
| block 0            |  each block: a complete .hz frame, or stored bytes
| block 1 ...        |
+--------------------+
| index ("HIDX")     |
+--------------------+
(after an append: ... [old index] [new blocks] [new index])

8.1 Header

offset size
 0     4    magic "HZN\x1A" (48 5A 4E 1A)
 4     1    container version (1)
 5     1    flags (0)
 6     2    0
 8     8    u64 index_offset
16     8    u64 index_length
24     4    u32 index_crc     CRC-32 of the index bytes
28     4    u32 header_crc    CRC-32 of header bytes 0..27

Refuse any other magic ("not a Horizon archive") or version ("made by a newer version"). The header is the only part rewritten in place; it always points at the latest complete index.

8.2 Index

str = u32 byte length followed by that many UTF-8 bytes.

4 bytes "HIDX"
str     creator           e.g. "hzn 1.3.0; horizon 41f51c643265" (informational)
i64     created           unix seconds
u32     n_blocks
  per block:
    u64 offset            absolute file offset of the block's bytes
    u64 csize             bytes stored in the file
    u64 usize             bytes after decoding
    u32 ccrc              CRC-32 of the csize stored bytes
    u32 ucrc              CRC-32 of the usize decoded bytes
    u8  codec             0 = stored, 1 = Horizon .hz frame
    u8  level             section 8.4
u32     n_entries
  per entry:
    u8  type              0 = file, 1 = directory, 2 = symlink
    str path
    u32 mode              Unix mode bits
    i64 mtime_sec
    u32 mtime_nsec
    u64 size
    u64 stream_offset
    u32 crc               CRC-32 of the file's contents
    str link_target       empty unless type = 2

8.3 Data model

  • The blocks' decoded contents, concatenated in index order, form one logical stream. A file is size bytes at stream_offset in that stream, and may span blocks.
  • A block with codec 1 is a complete .hz frame (section 4): decode it as a whole. The container never looks inside it. Blocks Horizon cannot shrink are stored (codec 0).
  • Small files are packed together into solid blocks of up to 64 MiB; files of 8 MiB or more start and end their own blocks and are cut into chunks of up to 256 MiB. Readers must not rely on these sizes: use the index.
  • Directories are explicit entries, so empty folders and their modes and times survive. Symlinks are stored, not followed. Extended attributes, ACLs, resource forks and hard links are not stored.
  • Appending writes new blocks and a complete new index after the old end of the file, then rewrites the header. Replaced entries and old indexes remain as unreferenced bytes. A reader only ever follows the header.

8.4 Level byte

Each block records the level it was compressed with. It is informational (the block decodes from its own frame header) but part of container version 1: refuse a block whose level byte is not in this table.

8.4 Level byte
ByteLevelhorizon option
0store(none)
1speed-speed
2normaldefault
3ultra-ultra
4balanced-balanced
5dense-dense
6web-web
7web fast-webfast or -webturbo (same stream format)

Apps that show levels list them fastest first: store, speed, web fast, web, normal, balanced, dense, ultra.

8.5 Paths and safety

  • Paths are relative, /-separated UTF-8, exactly as the file system returned them (no Unicode normalisation, so NFC and NFD names round-trip byte for byte).
  • Reject the whole archive if any path is empty, absolute, longer than 4096 bytes, contains a NUL, or has an empty, . or .. component.
  • \ is an ordinary character in a name, never a separator. On Windows, map it (and <>:"|?*, control characters, trailing dots and spaces, and device names such as CON or COM1) to _, so ..\..\x can never escape.
  • Do not create a symlink that points outside the destination. Drop setuid and setgid bits when restoring modes.

8.6 Reading an archive, step by step

read 32 bytes; check magic, version == 1, header_crc
seek index_offset; read index_length bytes; check index_crc; check "HIDX"
parse blocks; for each: offset + csize <= index_offset, level byte known
parse entries; validate every path (8.5) before extracting anything
stream_start[i] = sum of usize of blocks 0..i-1

to extract a file (stream_offset, size):
  for each block overlapping [stream_offset, stream_offset + size):
    read csize bytes at offset; check ccrc
    codec 0: the bytes are the data;  codec 1: decode the .hz frame
    check usize and ucrc
    copy the overlapping part
  check the file's crc

To list an archive you only need the header and the index: no block has to be decoded.

9. Untrusted input

  • Treat every size field as untrusted. A size the remaining input or the decoded result cannot satisfy is corruption.
  • Refuse, up front, a single buffer larger than you can afford (the reference decoder's limit is twice the machine's physical memory) instead of letting the system kill the process later.
  • Check every CRC the format gives you, and write nothing for a stream or file that fails.
  • Ask before an archive expands to more than 100 times its size and more than 1 GB.

The reference command line reports these cases with exit status 2 ("corrupt, truncated, not a Horizon file, or from an incompatible version") and removes any partial output.

10. Conformance

The reference repository keeps a compatibility corpus: .hz files written by the 1.0 encoder at every level, covering every frame type and the transforms small inputs reach, with the expected size and SHA-256 of each decoded file. Every Horizon build must decode all of them byte-exact. Files in the corpus are never regenerated: a change that breaks one is a new major format, not a test update. A second list holds damaged files a decoder must refuse (chunked streams cut between pieces, without totals, with trailing bytes, with a wrong CRC).

If you are writing a decoder and want the corpus to test against, ask us (section 11).

11. Adding Horizon support to your app

Listing and extracting .hzn

Sections 8.1 to 8.3 are all you need to list an archive and to extract stored blocks. To extract compressed blocks you also need to decode .hz frames.

Decoding .hz with the horizon command

The free Tuntau Horizon download includes the horizon command line tool. The Horizon app itself uses it this way, as a black box:

horizon d file.hz out           # decode one frame to a file
horizon -d -q - - < in.hz > out  # stdin to stdout, any length, bounded memory
horizon t *.hz                  # test: decode and verify, write nothing
horizon l *.hz                  # list: sizes, ratio, CRC, stream type
horizon -V                      # program and format version

Exit status 0 means the whole output was written; 1 is a usage or I/O error; 2 means the input is corrupt, truncated, not a Horizon file or from an incompatible version. On any status but 0, discard the output. Shipping horizon inside your own product is covered by the Horizon licence agreement; write to us if your case is not clearly allowed there.

Writing your own decoder

Start with the frames that need no coder: stored (0xF0), periodic (0xF1), chunked (0xF3) and BWR0. Then add the leaf coders your users meet most: the web frame with hzh (web content and small files), BWZ1 (speed) and BWF8 (default). The coded bits of each leaf stream are defined by the reference sources named in section 5.1.

Questions, the reference sources or the test corpus: write to hello@tuntau.com. I read every message myself, and I'd like to hear about any app that adds Horizon support.