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 compression
  • LzmaDecompressor — RAII class for one-shot or repeated decompression
  • LzmaException — Exception class with error code context
  • lzmaCompress / 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

BSD-3-Clause

Types 3

classLzmaException : Exception

Exception thrown by high-level LZMA operations.

Contains the underlying lzma_ret code for programmatic error handling.

Fields
lzma_ret codeThe underlying liblzma return code.
Constructors
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");

Fields
private lzma_stream _strm
private bool _closed
private int _level
Methods
private void 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.
int level() constReturns the compression level this compressor was configured with.
Constructors
this(int level, lzma_check check)
Destructors

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);

Fields
private lzma_stream _strm
private bool _closed
Methods
private void close()
LzmaDecompressor create() staticCreates a new LzmaDecompressor.
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.
Constructors
Destructors

Functions 4

fnvoid checkLzmaError(lzma_ret rc, string context = "")Throws an LzmaException if the given return code is an error.
fnubyte[] lzmaCompress(const(ubyte)[] data, int level = cast(int) LZMA_PRESET_DEFAULT)One-shot compression of data using LZMA/XZ.
fnubyte[] lzmaDecompress(const(ubyte)[] data)One-shot decompression of `.xz` data.
fnsize_t lzmaCompressBound(size_t srcSize) @safe @nogc nothrowReturns an upper bound on the compressed size of `srcSize` bytes when encoded in a single call.