bkataru/comprezz
A single-file gzip/deflate compression library & binary for Zig, based on code from the Zig 0.14 standard library.
A single-file gzip/deflate compression library and CLI binary for Zig, based on code from the Zig 0.14 standard library.
Add to your build.zig.zon with:
$ zig fetch --save git+https://github.com/bkataru/comprezz.git
Which should update it as such:
.{
.name = "your-project",
.version = "0.1.0",
.dependencies = .{
.comprezz = .{
.url = "https://github.com/bkataru/comprezz.git"
.hash = "0a20xh2as0572jdhgb52..."
},
},
}
And in your build.zig:
const comprezz = b.dependency("comprezz", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("comprezz", comprezz.module("comprezz"));
const std = @import("std");
const comprezz = @import("comprezz");
pub fn main() !void {
var compressed_buffer: [1024]u8 = undefined;
var fixed_writer = std.Io.Writer.fixed(&compressed_buffer);
const data = "Hello, World!";
var input_buffer: [1024]u8 = undefined;
@memcpy(input_buffer[0..data.len], data);
var input_reader = std.Io.Reader.fixed(input_buffer[0..data.len]);
try comprezz.compress(&input_reader, &fixed_writer, .{});
// Find the end of compressed data by checking for non-zero bytes
var written: usize = 0;
for (compressed_buffer, 0..) |byte, i| {
if (byte != 0) written = i + 1;
}
const compressed = compressed_buffer[0..written];
_ = compressed;
}
const std = @import("std");
const comprezz = @import("comprezz");
pub fn main() !void {
const input_file = try std.fs.cwd().openFile("input.txt", .{});
defer input_file.close();
var input_reader = input_file.reader();
const output_file = try std.fs.cwd().createFile("output.gz", .{});
defer output_file.close();
var output_writer = output_file.writer();
try comprezz.compress(&input_reader, &output_writer, .{ .level = .best });
}
Available compression levels:
.fast - Fastest compression.default - Good balance (default).best - Best compression ratio.level_4 through .level_9 - Numeric compression levelsFor streaming compression, you can use the compressor API:
const std = @import("std");
const comprezz = @import("comprezz");
pub fn main() !void {
const output_file = try std.fs.cwd().createFile("output.gz", .{});
defer output_file.close();
var output_writer = output_file.writer();
var comp = try comprezz.compressor(&output_writer, .{ .level = .fast });
const input_file = try std.fs.cwd().openFile("input.txt", .{});
defer input_file.close();
var input_reader = input_file.reader();
try comp.compress(&input_reader);
try comp.finish();
}
Build the CLI:
zig build
comprezz input.txt -o output.gz
comprezz -l fast input.txt -o output.gz # Fast compression
comprezz -l best input.txt -o output.gz # Best compression
comprezz -l 9 input.txt -o output.gz # Level 9
cat largefile.txt | comprezz > output.gz
comprezz largefile.txt > compressed.gz
compresspub fn compress(reader: *std.Io.Reader, writer: *std.Io.Writer, options: Options) !void
Compress data from a reader and write to a writer using gzip format.
reader: Pointer to a std.Io.Reader interfacewriter: Pointer to a std.Io.Writer interfaceoptions: Compression options (level)compressorpub fn compressor(writer: *std.Io.Writer, options: Options) !Compressor
Create a compressor instance for streaming compression.
Compressorpub const Compressor = Deflate(.gzip);
The Compressor type for gzip compression.
pub const Level = enum(u4) {
fast, // Fastest compression
default, // Default balance
best, // Best compression
level_4, // Level 4
level_5, // Level 5
level_6, // Level 6
level_7, // Level 7
level_8, // Level 8
level_9, // Level 9
};
pub const Options = struct {
level: Level = .default,
};
zig build
zig build test
zig-out/bin/comprezz --help
Usage: comprezz [OPTIONS] [INPUT_FILE]
Compress files using gzip format.
Options:
-o, --output FILE Output file (default: stdout)
-l, --level LEVEL Compression level: fast, default, best, or 4-9
-h, --help Show this help message
-v, --version Show version
If no INPUT_FILE is specified, reads from stdin.
Examples:
comprezz input.txt -o output.gz
comprezz -l best input.txt > output.gz
cat file.txt | comprezz > file.gz
This code is based on the Zig 0.14 standard library, which is part of the Zig project and uses the MIT license. This implementation maintains the same license.
std.Io.Reader and std.Io.Writer interfacesThis is a single-file library copied from Zig's standard library. For Zig language development, see ziglang.org.
For issues or improvements to this specific package, open an issue on this GitHub repo and/or raise a PR.