std.zlib

Compress/decompress data using the zlib library.

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

Types 4

classZlibException : Exception

Errors throw a ZlibException.

Methods
private string getmsg(int errnum) nothrow @nogc pure @safe
Constructors
this(int errnum)

the header format the compressed stream is wrapped in

deflatea standard zlib header
gzipa gzip file format header
determineFromDataused when decompressing. Try to automatically detect the stream format by looking at the data

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

Fields
z_stream zs
int level
int inited
bool gzip
Methods
void error(int err)
const(void)[] compress(const(void)[] buf)Compress the data in buf and return the compressed data. Params: buf = data to compress
void[] flush(int mode = Z_FINISH) Compress and return any remaining data. The returned data should be appended to that returned by compress(). Params: mode = one of the following: * ZSYNCFLUSH Syncs up flushing to the...
Constructors
this(int level, HeaderFormat header = HeaderFormat.deflate)Constructor.
this(HeaderFormat header = HeaderFormat.deflate)ditto
Destructors

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

Fields
z_stream zs
int inited
int done
bool inputEnded
size_t destbufsize
Methods
void error(int err)
const(void)[] uncompress(const(void)[] buf)Decompress the data in buf and return the decompressed data. The buffers returned from successive calls to this should be concatenated together.
void[] flush()Decompress and return any remaining data. The returned data should be appended to that returned by uncompress(). The UnCompress object cannot be used further.
bool empty() @property constReturns true if all input data has been decompressed and no further data can be decompressed (inflate() returned ZSTREAMEND)
Constructors
this(uint destbufsize)Construct. destbufsize is the same as for D.zlib.uncompress().
this(HeaderFormat format = HeaderFormat.determineFromData)ditto
Destructors

Functions 5

fnuint adler32(uint adler, const(void)[] buf)Compute the Adler-32 checksum of a buffer's worth of data.
fnuint crc32(uint crc, const(void)[] buf)Compute the CRC32 checksum of a buffer's worth of data.
fnubyte[] compress(const(void)[] srcbuf, int level)Compress data
fnubyte[] compress(const(void)[] srcbuf)ditto
fnvoid[] uncompress(const(void)[] srcbuf, size_t destlen = 0u, int winbits = 15)Decompresses the data in srcbuf[]. Params: srcbuf = buffer containing the compressed data. destlen = size of the uncompressed data. It need not be accurate, but the decompression will be faster if...