Archive API ​
The main Archive.zig API provides simple functions for compression and decompression.
Core Functions ​
compress ​
pub fn compress(allocator: Allocator, data: []const u8, algorithm: Algorithm) ![]u8Compresses data using the specified algorithm.
Parameters:
allocator: Memory allocatordata: Input data to compressalgorithm: Compression algorithm to use
Returns: Compressed data (caller owns memory)
Example:
const compressed = try archive.compress(allocator, "Hello, World!", .gzip);
defer allocator.free(compressed);decompress ​
pub fn decompress(allocator: Allocator, data: []const u8, algorithm: Algorithm) ![]u8Decompresses data using the specified algorithm.
Parameters:
allocator: Memory allocatordata: Compressed data to decompressalgorithm: Algorithm used for compression
Returns: Decompressed data (caller owns memory)
Example:
const decompressed = try archive.decompress(allocator, compressed_data, .gzip);
defer allocator.free(decompressed);compressWithConfig ​
pub fn compressWithConfig(allocator: Allocator, data: []const u8, config: CompressionConfig) ![]u8Compresses data with custom configuration.
Parameters:
allocator: Memory allocatordata: Input data to compressconfig: Compression configuration
Returns: Compressed data (caller owns memory)
Example:
const config = archive.CompressionConfig.best();
const compressed = try archive.compressWithConfig(allocator, data, config);
defer allocator.free(compressed);Auto-Detection Functions ​
detectAlgorithm ​
pub fn detectAlgorithm(data: []const u8) ?AlgorithmAutomatically detects the compression algorithm from data headers.
Parameters:
data: Compressed data
Returns: Detected algorithm or null if unknown
Example:
if (archive.detectAlgorithm(compressed_data)) |algo| {
std.debug.print("Detected: {s}\n", .{@tagName(algo)});
}autoDecompress ​
pub fn autoDecompress(allocator: Allocator, data: []const u8) ![]u8Automatically detects algorithm and decompresses data.
Parameters:
allocator: Memory allocatordata: Compressed data
Returns: Decompressed data (caller owns memory)
Example:
const decompressed = try archive.autoDecompress(allocator, compressed_data);
defer allocator.free(decompressed);Error Handling ​
All functions return errors for various failure conditions:
const CompressError = error{
OutOfMemory,
InvalidData,
InvalidMagic,
UnsupportedAlgorithm,
CorruptedStream,
ChecksumMismatch,
InvalidOffset,
InvalidTarArchive,
ZstdError,
UnsupportedCompressionMethod,
ZipUncompressSizeMismatch,
InvalidZipArchive,
};Example with error handling:
const compressed = archive.compress(allocator, data, .gzip) catch |err| switch (err) {
error.OutOfMemory => {
std.debug.print("Not enough memory\n", .{});
return;
},
error.InvalidData => {
std.debug.print("Invalid input data\n", .{});
return;
},
else => return err,
};