Skip to content

xz.zigNative XZ Container Compression for Zig

Production-ready native Zig XZ container compression library.

Installation ​

xz.zig is production-ready. The recommended method for production applications is pinning to the tagged stable release:

bash
# Recommended: Tagged stable release (0.0.1)
zig fetch --save https://github.com/muhammad-fiaz/xz.zig/archive/refs/tags/0.0.1.tar.gz

# Alternative: Latest development build (main branch)
zig fetch --save git+https://github.com/muhammad-fiaz/xz.zig.git

Then expose the module in your build.zig:

zig
const xz_dep = b.dependency("xz", .{
    .target = target,
    .optimize = optimize,
});
exe.root_module.addImport("xz", xz_dep.module("xz"));

Quick Example ​

Compress and decompress data in just a few lines of idiomatic Zig:

zig
const std = @import("std");
const xz = @import("xz");

pub fn main() !void {
    const allocator = std.heap.page_allocator;

    const payload = "Native XZ container compression in pure Zig 0.17.0! High-ratio dictionary encoding with CRC64.";

    // One-shot heap compression with standard CRC64 integrity check
    const compressed = try xz.compress(allocator, payload, .{ .check = .crc64 });
    defer allocator.free(compressed);

    // One-shot heap decompression
    const restored = try xz.decompress(allocator, compressed, .{});
    defer allocator.free(restored);

    std.debug.print("Original size:   {d} bytes\n", .{payload.len});
    std.debug.print("Compressed size: {d} bytes\n", .{compressed.len});
    std.debug.print("Restored:        {s}\n", .{restored});
}

Released under the MIT License.