Skip to content

std.compress

std.compress groups DEFLATE-based compression formats under format-focused modules. Use std.compress.deflate for raw or zlib-wrapped DEFLATE, and std.compress.gzip for gzip-wrapped DEFLATE. CRC-32/IEEE lives in std.hash.crc32; compression modules do not expose checksum helpers.

How It Works

The *_into functions write into caller-provided buffers and return the number of bytes written. One-shot helpers allocate an io.MemoryStream; decompression requires an explicit maximum output size. Output storage uses the private built-in allocation domain; compression options never select an allocator.

Zynx
import std.bytes;
import std.compress.deflate;
import std.io;
import std.mem.{ AllocError };

fn main() {
	let compressed = try deflate.compress(bytes.Bytes("hello"));
	let plain = try deflate.decompress(compressed.bytes(), 32);

	let view = plain.bytes();
	assert view.length == 5 as usize;
	assert view.at(0) == 104 as u8;
	assert view.at(4) == 111 as u8;
}

Raw DEFLATE

deflate defaults to zlib-wrapped DEFLATE. Pass Format.Raw when the caller needs an RFC 1951 raw DEFLATE stream without a zlib header or trailer.

Zynx
import std.bytes;
import std.compress.deflate;
import std.io;
import std.mem.{ AllocError };

fn main() {
	let compress_options = deflate.Options(deflate.default_compression, deflate.Format.Raw);
	let packed = try deflate.compress(bytes.Bytes("raw"), compress_options);
	let decompress_options = deflate.Options(deflate.default_compression, deflate.Format.Raw);
	let plain = try deflate.decompress(packed.bytes(), 16, decompress_options);

	assert plain.bytes().length == 3 as usize;
}

Gzip

Use std.compress.gzip when the byte stream must use the gzip wrapper.

Zynx
import std.bytes;
import std.compress.gzip;
import std.io;
import std.mem.{ AllocError };

fn main() {
	let packed = try gzip.compress(bytes.Bytes("ok"));
	let plain = try gzip.decompress(packed.bytes(), 16);

	assert plain.bytes().length == 2 as usize;
}

Caller-Owned Buffers

Use compress_into and decompress_into when allocation should stay under the caller’s control.

Zynx
import std.bytes;
import std.compress.deflate;

fn main() {
	var out = [0 as u8; 16];
	let out_view: &[u8] in _ = &out[0..<out.length];
	let packed_len = try deflate.compress_into(bytes.Bytes("ok"),
	                                           out_view);

	var plain = [0 as u8; 2];
	let packed_view: [u8] in _ = out[0..<packed_len];
	let plain_view: &[u8] in _ = &plain[0..<plain.length];
	let plain_len = try deflate.decompress_into(bytes.Bytes(packed_view),
	                                            plain_view);
	assert plain_len == 2;
	assert plain[0] == 111 as u8;
	assert plain[1] == 107 as u8;
}

Ownership, Allocation, Errors, And Unspecified Behavior

compress_into and decompress_into write only into caller-owned buffers and do not allocate output storage. compress and decompress allocate an std.io.MemoryStream in the built-in allocation domain. decompress requires max_output so allocation remains bounded.

Compression failures are reported as CompressError. One-shot helpers can also throw AllocError for output storage and io.IOError from MemoryStream writes. Compressed byte exactness, chunking, zlib internal strategy, and compression ratio are not a Zynx API contract; only successful decompression to the same bytes is the portable behavior.

Use std.bytes for borrowed input, std.io for returned memory streams, and std.hash when callers need standalone CRC helpers.

API Reference

Modules

ModuleDescription
compress.deflateRaw and zlib-wrapped DEFLATE compression and decompression.
compress.gzipgzip-wrapped DEFLATE compression and decompression.

deflate Constants

NameTypeDescription
no_compressioni32Compression level 0.
best_speedi32Compression level 1.
best_compressioni32Compression level 9.
default_compressioni32zlib default compression level.

deflate Format

ValueDescription
Format.RawRaw DEFLATE stream with no wrapper.
Format.Zlibzlib-wrapped DEFLATE stream. This is the default.

deflate Options

FieldTypeDefaultDescription
leveli32default_compressionzlib compression level.
formatFormatFormat.ZlibRaw or zlib-wrapped DEFLATE stream.

Errors

ErrorDescription
CompressError.StreamInvalid stream state or compression level.
CompressError.DataInvalid or corrupt compressed data.
CompressError.Memoryzlib allocation failure.
CompressError.BufferDestination buffer or output cap was too small.
CompressError.VersionLinked zlib version is incompatible.
CompressError.NeedDictCompressed data requires a preset dictionary.
CompressError.ErrorUnknown zlib status code.

deflate Functions

NameSignatureDescription
compress_boundcompress_bound(source_length: usize) -> usizeReturns an upper bound for compressed output length.
compress_intocompress_into(source: Bytes, dest: &[u8], options: Options = Options()) throws(CompressError) -> usizeCompresses into caller-provided storage.
decompress_intodecompress_into(source: Bytes, dest: &[u8], options: Options = Options()) throws(CompressError) -> usizeDecompresses into caller-provided storage.
compresscompress(source: Bytes, options: Options = Options()) throws(CompressError | AllocError | io.IOError) -> io.MemoryStreamAllocates and returns compressed bytes.
decompressdecompress(source: Bytes, max_output: usize, options: Options = Options()) throws(CompressError | AllocError | io.IOError) -> io.MemoryStreamAllocates up to max_output bytes and decompresses input.

gzip Functions

std.compress.gzip exposes the same one-shot and caller-buffer function names as deflate, using gzip.Options(level) without the Format selector. Its compress_bound includes gzip wrapper overhead.

Status

std.compress is the current public compression facade for DEFLATE/zlib and gzip helpers. For the full exported surface, see Standard Library Inventory.

Released under the MIT License.