Zig
Zig
Compressing and Decompressing Files Using Streaming (CompressFile / DecompressFile)
See more Compression Examples
This example demonstrates how to compress a file to a binary format and then restore it using the Chilkat.Compression class. The CompressFile method reads the source file, compresses it using the specified algorithm, and writes the result to a destination file. The DecompressFile method performs the reverse operation, restoring the original file from the compressed data.
Both operations are performed internally in streaming mode, allowing files of any size to be processed efficiently without loading the entire file into memory. The example also includes a simple verification step by comparing file sizes to confirm that the decompressed output matches the original input.
Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example assumes the Chilkat API has already been unlocked.
// See Global Unlock Sample for sample code.
const compress = try chilkat.Compression.init();
defer compress.deinit();
// Use the zlib algorithm (recommended for general use)
compress.setAlgorithm("zlib");
// ------------------------------------------------------------------
// Compress a file
// ------------------------------------------------------------------
const input_file = "c:/temp/example.txt";
const compressed_file = "c:/temp/example.txt.zlib";
compress.compressFile(input_file, compressed_file) catch {
std.debug.print("Compression failed:\n", .{});
std.debug.print("{s}\n", .{try compress.getLastErrorText(alloc)});
return;
};
std.debug.print("File compressed successfully:\n", .{});
std.debug.print(" Input: {s}\n", .{input_file});
std.debug.print(" Compressed: {s}\n", .{compressed_file});
// ------------------------------------------------------------------
// Decompress the file back to its original form
// ------------------------------------------------------------------
const decompressed_file = "c:/temp/example_restored.txt";
compress.decompressFile(compressed_file, decompressed_file) catch {
std.debug.print("Decompression failed:\n", .{});
std.debug.print("{s}\n", .{try compress.getLastErrorText(alloc)});
return;
};
std.debug.print("File decompressed successfully:\n", .{});
std.debug.print(" Output: {s}\n", .{decompressed_file});
// ------------------------------------------------------------------
// Optional: Verify file sizes (basic sanity check)
// ------------------------------------------------------------------
const fac = try chilkat.FileAccess.init();
defer fac.deinit();
const original_size = fac.fileSize(input_file);
const restored_size = fac.fileSize(decompressed_file);
std.debug.print("Original file size: {d}\n", .{original_size});
std.debug.print("Restored file size: {d}\n", .{restored_size});
if (original_size == restored_size) {
std.debug.print("Sizes match (basic verification successful).\n", .{});
} else {
std.debug.print("Warning: File sizes differ.\n", .{});
}
}