A Zig library for Facebook's zstd fast compression algorithm (v1.6.0). This repository provides a Zig build system (build.zig, build.zig.zon) and Zig standard bindings for the zstd C library, allowing easy integration into Zig projects.
You can generate the documentation locally by running:
zig build docsThe generated documentation will be available in:
zig-out/docsFor AI-generated documentation on zstd.zig, visit: https://deepwiki.com/muhammad-fiaz/zstd.zig/
For a detailed reference of the C API, please consult the official Zstandard API Manual: https://facebook.github.io/zstd/doc/api_manual_latest.html
You can fetch the package directly using zig fetch:
zig fetch --save git+https://github.com/muhammad-fiaz/zstd.zig.gitAlternatively, add the dependency manually to your build.zig.zon:
.{
.name = "my-project",
.version = "0.1.0",
.dependencies = .{
.zstd = .{
.url = "git+https://github.com/muhammad-fiaz/zstd.zig.git#COMMIT_HASH",
.hash = "...", // Run `zig build` to find the correct hash
},
},
}Add zstd as a dependency and import the module:
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// Resolve dependencies
const zstd_dep = b.dependency("zstd", .{
.target = target,
.optimize = optimize,
});
const zstd_mod = zstd_dep.module("zstd");
const exe = b.addExecutable(.{
.name = "my-exe",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
// Add zstd module
exe.root_module.addImport("zstd", zstd_mod);
// Link the library
// Note: The module automatically links the library, but if you need strictly C access:
// exe.linkLibrary(zstd_dep.artifact("zstd"));
b.installArtifact(exe);
}const std = @import("std");
const zstd = @import("zstd");
pub fn main() !void {
const input = "Hello, Zstandard from Zig!";
// Compression
const max_dst_size = zstd.c.ZSTD_compressBound(input.len);
const dest_buffer = try std.heap.c_allocator.alloc(u8, max_dst_size);
defer std.heap.c_allocator.free(dest_buffer);
const cSize = zstd.c.ZSTD_compress(dest_buffer.ptr, max_dst_size, input.ptr, input.len, 1);
if (zstd.c.ZSTD_isError(cSize) != 0) {
return error.CompressionFailed;
}
std.debug.print("Compressed size: {d}\n", .{cSize});
}- Build System: Pure Zig build system compatible with Zig 0.15+.
- Direct Source Integration: Uses local C sources (forked from upstream zstd) avoiding external system dependencies.
- Configurable: Supports standard zstd build options (Multithreading, legacy support, etc.).
- Examples: Includes Zig translations of standard Zstd C examples.
- Windows / MacOS / Linux
- x86_64, aarch64, and other targets supported by Zig and Zstd.
This project is double-licensed under BSD and GPLv2. Original Zstd code is Copyright (c) Meta Platforms, Inc. and affiliates. Zig bindings and build system modifications are Copyright (c) Muhammad Fiaz.