- ddn.lib.lzma — Low-level bindings
- XZ Utils Homepage
ddn.wrp.lzma
High-level D wrapper for liblzma — idiomatic compression API.
Overview:
This module provides a high-level, idiomatic D interface for working
with LZMA/XZ compression. It wraps the low-level bindings in ddn.lib.lzma 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:
LzmaCompressor— RAII class for one-shot or repeated compressionLzmaDecompressor— RAII class for one-shot or repeated decompressionLzmaException— Exception class with error code contextlzmaCompress/lzmaDecompress— One-shot convenience functions
Quick_Start:
One-shot compression and decompression:
import ddn.wrp.lzma;
ubyte[] data = cast(ubyte[])"Hello, LZMA!";
ubyte[] compressed = lzmaCompress(data);
ubyte[] original = lzmaDecompress(compressed);
assert(original == data);RAII compressor for repeated operations:
import ddn.wrp.lzma;
auto comp = LzmaCompressor.create(6);
auto decomp = LzmaDecompressor.create();
ubyte[] c = comp.compress(cast(ubyte[])"data");
ubyte[] d = decomp.decompress(c);Error_Handling:
All operations throw LzmaException on failure:
try {
auto result = lzmaDecompress(garbageData);
} catch (LzmaException e) {
writeln("Error: ", e.msg, " (code: ", e.code, ")");
}See Also
License
Copyright
Types 3
Exception thrown by high-level LZMA operations.
Contains the underlying lzma_ret code for programmatic error handling.
lzma_ret codeThe underlying liblzma return code.this(lzma_ret code, string msg, string file = __FILE__, size_t line = __LINE__)RAII wrapper around an LZMA .xz stream encoder.
Each instance manages its own lzma_stream and is initialised with lzma_easy_encoder. Reusing an LzmaCompressor instance across multiple compress calls avoids repeated context allocation overhead.
Example:
auto comp = LzmaCompressor.create(6);
ubyte[] c1 = comp.compress(cast(ubyte[])"chunk 1");
ubyte[] c2 = comp.compress(cast(ubyte[])"chunk 2");private lzma_stream _strmprivate bool _closedprivate int _levelvoid close()LzmaCompressor create(int level = cast(int) LZMA_PRESET_DEFAULT,
lzma_check check = lzma_check.LZMA_CHECK_CRC64) staticCreates a new LzmaCompressor with the specified compression level.ubyte[] compress(const(ubyte)[] data)Compresses the given data into a self-contained `.xz` stream.void reset()Resets the encoder so the instance can compress a new, independent stream.this(int level, lzma_check check)RAII wrapper around an LZMA .xz stream decoder.
The decompressor uses lzma_stream_decoder with the concatenated-streams flag, so it can decompress any number of concatenated .xz frames.
Example:
auto decomp = LzmaDecompressor.create();
ubyte[] d1 = decomp.decompress(compressed1);
ubyte[] d2 = decomp.decompress(compressed2);private lzma_stream _strmprivate bool _closedvoid close()ubyte[] decompress(const(ubyte)[] data)Decompresses a single `.xz` stream and returns the result.void reset()Resets the decoder so the instance can decompress a new stream.this()Functions 4
void checkLzmaError(lzma_ret rc, string context = "")Throws an LzmaException if the given return code is an error.ubyte[] lzmaCompress(const(ubyte)[] data, int level = cast(int) LZMA_PRESET_DEFAULT)One-shot compression of data using LZMA/XZ.size_t lzmaCompressBound(size_t srcSize) @safe @nogc nothrowReturns an upper bound on the compressed size of `srcSize` bytes when encoded in a single call.