std.zlib

Compress/decompress data using the zlib library.

Members

Classes

Compress
class Compress

Used when the data to be compressed is not all in one buffer.

UnCompress
class UnCompress

Used when the data to be decompressed is not all in one buffer.

ZlibException
class ZlibException

Errors throw a ZlibException.

Enums

HeaderFormat
enum HeaderFormat

the header format the compressed stream is wrapped in

Functions

adler32
uint adler32(uint adler, const(void)[] buf)

Compute the Adler-32 checksum of a buffer's worth of data.

compress
ubyte[] compress(const(void)[] srcbuf, int level)
ubyte[] compress(const(void)[] srcbuf)

Compress data

crc32
uint crc32(uint crc, const(void)[] buf)

Compute the CRC32 checksum of a buffer's worth of data.

uncompress
void[] uncompress(const(void)[] srcbuf, size_t destlen, int winbits)

Decompresses the data in srcbuf[].

Examples

If you have a small buffer you can use compress and uncompress directly.

import std.zlib;

auto src =
"the quick brown fox jumps over the lazy dog\r
 the quick brown fox jumps over the lazy dog\r";

ubyte[] dst;
ubyte[] result;

dst = compress(src);
result = cast(ubyte[]) uncompress(dst);
assert(result == src);

When the data to be compressed doesn't fit in one buffer, use Compress and UnCompress.

import std.zlib;
import std.stdio;
import std.conv : to;
import std.algorithm.iteration : map;

UnCompress decmp = new UnCompress;
foreach (chunk; stdin.byChunk(4096).map!(x => decmp.uncompress(x)))
{
    chunk.to!string.write;
}

References: Wikipedia

Meta