Skip to content

API Reference ​

The module exposes one-shot functions, streaming types, whole-file helpers, and two introspection helpers. Everything that allocates takes the allocator as its first argument; everything that performs I/O takes a std.Io.Reader, a std.Io.Writer or a std.Io interface.

One-shot functions ​

FunctionSignature
compress(gpa, input, CompressionOptions) StreamError![]u8
decompress(gpa, input, DecompressionOptions) StreamError![]u8
compressStream(gpa, reader, writer, CompressionOptions) StreamError!void
decompressStream(gpa, reader, writer, DecompressionOptions) StreamError!void
compressFile(gpa, io, src_path, dst_path, CompressionOptions) FileStreamError!void
decompressFile(gpa, io, src_path, dst_path, DecompressionOptions) FileStreamError!void

compress and decompress return memory the caller owns. The others write to a writer or a path and allocate only their internal scratch.

See Compress and Decompress.

Streaming types ​

TypePurpose
CompressorIncremental encoder. init, write, finish, reset, deinit.
DecompressorIncremental decoder with caller-owned scratch. init, stream, deinit.
DecoderIncremental decoder that owns its scratch. init, stream, reset, deinit.
ScratchReusable decoder buffers. init, ensure, deinit.
StatsBlocks decoded and bytes produced.

See Compressor and Decompressor.

Options ​

TypeFieldDefault
CompressionOptionslevel: u8 (1..9)9
DecompressionOptionsmax_output_size: ?usizenull
DecompressionOptionsverify_crc: booltrue

See Options.

Introspection ​

FunctionPurpose
blockSizeOfStreamRead the declared block size from a header
maxCompressedSizeUpper bound on the compressed size of an input
versionMajor, minor and patch numbers
version_string"0.0.1"
version_major0

See Constants.

Errors ​

AliasCovers
ErrorEverything describing the codec and its input
IoErrorstd.Io.Writer.Error and std.Io.Reader.Error
StreamErrorError plus IoError
FileErrorstd.Io.File.OpenError, std.Io.Dir.RenameError, and the same-path guard
FileStreamErrorStreamError plus FileError

See Errors.

Version and limits ​

zig
const bzip2 = @import("bzip2");

const version = bzip2.version();          // .{ .major = 0, .minor = 0, .patch = 1 }
const largest_block = bzip2.max_block_size;  // 900_000

version_major lets a caller gate on a feature without depending on the patch number.

Conventions ​

  • Option fields are named in lower case, following Zig practice.
  • Errors that describe bad input are distinguishable from each other and from error.OutOfMemory.
  • Slices passed in are borrowed for the duration of the call and are not retained.
  • Slices returned are owned by the caller.
  • Nothing is cached between calls unless a streaming type is reused explicitly.

Released under the MIT License.