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, useCompress andUnCompress.
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;}
ReferencesWikipedia
Sourcestd/zlib.d
ZlibException:object.Exception;adler32(uintadler, const(void)[]buf);Compute the Adler-32 checksum of a buffer's worth of data.
uintadler | the starting checksum for the computation. Use 1 for a new checksum. Use the output of this function for a cumulative checksum. |
const(void)[]buf | buffer containing input data |
staticubyte[] data = [1,2,3,4,5,6,7,8,9,10];uintadler =adler32(0u, data);writeln(adler);// 0xdc0037
crc32(uintcrc, const(void)[]buf);Compute the CRC32 checksum of a buffer's worth of data.
uintcrc | the starting checksum for the computation. Use 0 for a new checksum. Use the output of this function for a cumulative checksum. |
const(void)[]buf | buffer containing input data |
compress(const(void)[]srcbuf, intlevel);compress(const(void)[]srcbuf);Compress data
const(void)[]srcbuf | buffer containing the data to compress |
intlevel | compression level. Legal values are -1 .. 9, with -1 indicating the default level (6), 0 indicating no compression, 1 being the least compression and 9 being the most. |
uncompress(const(void)[]srcbuf, size_tdestlen = 0u, intwinbits = 15);const(void)[]srcbuf | buffer containing the compressed data. |
size_tdestlen | size of the uncompressed data. It need not be accurate, but the decompression will be faster if the exact size is supplied. |
intwinbits | the base two logarithm of the maximum window size. |
HeaderFormat: int;deflategzipdetermineFromDataCompress;level, HeaderFormatheader = HeaderFormat.deflate);header = HeaderFormat.deflate);intlevel | compression level. Legal values are 1 .. 9, with 1 being the least compression and 9 being the most. The default value is 6. |
HeaderFormatheader | sets the compression type to one of the options available inHeaderFormat. Defaults to HeaderFormat.deflate. |
compress(const(void)[]buf);const(void)[]buf | data to compress |
flush(intmode = Z_FINISH);intmode | one of the following:
|
UnCompress;destbufsize);format = HeaderFormat.determineFromData);uncompress(const(void)[]buf);flush();empty() const;// some random dataubyte[1024] originalData =void;// append garbage data (or don't, this works in both cases)auto compressedData =cast(ubyte[]) compress(originalData) ~cast(ubyte[])"whatever";auto decompressor =new UnCompress();auto uncompressedData = decompressor.uncompress(compressedData);assert(uncompressedData[] == originalData[],"The uncompressed and the original data differ");assert(decompressor.empty,"The UnCompressor reports not being done");