- ddn.lib.zstd — Low-level bindings
- Zstandard Homepage
ddn.wrp.zstd
High-level D wrapper for libzstd — idiomatic compression API.
Overview:
This module provides a high-level, idiomatic D interface for working
with Zstandard compression. It wraps the low-level bindings in ddn.lib.zstd with RAII classes, exception-safe error handling, and convenience functions.
Use this module for most applications. The low-level module is available
for advanced use cases requiring direct control over the C API.
Key_Components:
ZstdCompressor— RAII class for compression (reusable context)ZstdDecompressor— RAII class for decompression (reusable context)ZstdCDict/ZstdDDict— RAII structs for dictionary objectsZstdFrameInfo— Frame inspection without decompressionZstdException— Exception class with error code contextzstdCompress/zstdDecompress— One-shot convenience functions
Quick_Start:
One-shot compression and decompression:
import ddn.wrp.zstd;
ubyte[] data = cast(ubyte[])"Hello, Zstandard!";
ubyte[] compressed = zstdCompress(data);
ubyte[] original = zstdDecompress(compressed);
assert(original == data);RAII compressor for repeated operations:
import ddn.wrp.zstd;
auto comp = ZstdCompressor.create(6);
auto decomp = ZstdDecompressor.create();
ubyte[] c = comp.compress(cast(ubyte[])"data");
ubyte[] d = decomp.decompress(c);Error_Handling:
All operations throw ZstdException on failure:
try {
auto result = zstdDecompress(garbageData);
} catch (ZstdException e) {
writeln("Error: ", e.msg, " (code: ", e.errorCode, ")");
}See Also
License
Copyright
Types 6
Exception thrown by high-level Zstandard operations.
Contains the underlying ZSTD_ErrorCode for programmatic error handling.
ZSTD_ErrorCode errorCodeThe underlying ZSTD error code.this(ZSTD_ErrorCode ec, string msg, string file = __FILE__, size_t line = __LINE__)RAII wrapper around a ZSTD_CCtx for compression operations.
Reusing a ZstdCompressor instance across multiple compressions avoids repeated context allocation overhead.
Example:
auto comp = ZstdCompressor.create(6);
ubyte[] c1 = comp.compress(cast(ubyte[])"chunk 1");
ubyte[] c2 = comp.compress(cast(ubyte[])"chunk 2");void close()ZstdCompressor create(int level = ZSTD_CLEVEL_DEFAULT)Creates a new ZstdCompressor with the specified compression level.ubyte[] compress(const(ubyte)[] data)Compresses the given data and returns the compressed result.size_t compressInto(const(ubyte)[] src, ubyte[] dst)Compresses data into the provided output buffer.void setLevel(int level)Sets the compression level for subsequent compressions.void setStrategy(ZSTD_strategy strategy)Sets the compression strategy.void setWindowLog(int windowLog)Sets the window log for subsequent compressions.void setChecksumFlag(bool enabled)Enables or disables the content checksum in the frame header.void setPledgedSrcSize(ulong size)Hints at the uncompressed source size for the next frame.void reset(ZSTD_ResetDirective directive = ZSTD_ResetDirective.ZSTD_reset_session_and_parameters)Resets the compressor context.void loadDictionary(const(ubyte)[] dict)Loads a dictionary for subsequent compressions.RAII wrapper around a ZSTD_DCtx for decompression operations.
Reusing a ZstdDecompressor instance across multiple decompressions avoids repeated context allocation overhead.
Example:
auto decomp = ZstdDecompressor.create();
ubyte[] d1 = decomp.decompress(compressed1);
ubyte[] d2 = decomp.decompress(compressed2);void close()ZstdDecompressor create()Creates a new ZstdDecompressor.ubyte[] decompress(const(ubyte)[] data)Decompresses the given data and returns the decompressed result.size_t decompressInto(const(ubyte)[] src, ubyte[] dst)Decompresses data into the provided output buffer.void reset(ZSTD_ResetDirective directive = ZSTD_ResetDirective.ZSTD_reset_session_and_parameters)Resets the decompressor context.void loadDictionary(const(ubyte)[] dict)Loads a dictionary for subsequent decompressions.RAII wrapper around a ZSTD_CDict (compression dictionary).
The dictionary content is copied at construction time, so the source buffer can be freed afterwards.
Example:
auto cdict = ZstdCDict(cast(const(ubyte)[])"dictionary content", 6);
scope(exit) cdict.close();
auto comp = ZstdCompressor.create(6);
comp.refCDict(cdict);private ZSTD_CDict _dictvoid close()Frees the underlying dictionary.this(const(ubyte)[] dict, int level)Creates a compressed dictionary for efficient repeated compression.RAII wrapper around a ZSTD_DDict (decompression dictionary).
The dictionary content is copied at construction time, so the source buffer can be freed afterwards.
Example:
auto ddict = ZstdDDict(cast(const(ubyte)[])"dictionary content");
scope(exit) ddict.close();
auto decomp = ZstdDecompressor.create();
decomp.refDDict(ddict);private ZSTD_DDict _dictvoid close()Frees the underlying dictionary.this(const(ubyte)[] dict)Creates a decompression dictionary.Information extracted from a Zstandard frame header without decompression.
ulong frameContentSizeThe decompressed content size, or ZSTDCONTENTSIZEUNKNOWN if unknown.uint windowSizeThe window size required for decompression.uint dictIDThe dictionary ID, or 0 if none.bool isFrameWhether the data starts with a valid Zstandard frame.bool contentSizeUnknownWhether the content size is unknown.ZstdFrameInfo inspect(const(ubyte)[] data)Inspects a Zstandard frame header and extracts metadata.Functions 4
void checkError(size_t result, string context = "")Throws a ZstdException if the result code is a ZSTD error.ubyte[] zstdCompress(const(ubyte)[] data, int level = ZSTD_CLEVEL_DEFAULT)One-shot compression of data using Zstandard.size_t zstdCompressBound(size_t srcSize) @safeReturns the maximum compressed size for a given source size.