Compress/decompress data using the zlib library.
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; }
Errors throw a ZlibException.
Compute the Adler-32 checksum of a buffer's worth of data.
uint adler
| 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 |
uint
checksum for the provided input data and starting checksum static ubyte[] data = [1,2,3,4,5,6,7,8,9,10]; uint adler = adler32(0u, data); writeln(adler); // 0xdc0037
Compute the CRC32 checksum of a buffer's worth of data.
uint crc
| 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 |
uint
checksum for the provided input data and starting checksum Compress data
const(void)[] srcbuf
| buffer containing the data to compress |
int level
| 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. |
Decompresses the data in srcbuf[].
const(void)[] srcbuf
| buffer containing the compressed data. |
size_t destlen
| size of the uncompressed data. It need not be accurate, but the decompression will be faster if the exact size is supplied. |
int winbits
| the base two logarithm of the maximum window size. |
the header format the compressed stream is wrapped in
a standard zlib header
a gzip file format header
used 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.
Constructor.
int level
| compression level. Legal values are 1 .. 9, with 1 being the least compression and 9 being the most. The default value is 6. |
HeaderFormat header
| sets the compression type to one of the options available in HeaderFormat . Defaults to HeaderFormat.deflate. |
compress
, HeaderFormat
Compress the data in buf and return the compressed data.
const(void)[] buf
| data to compress |
Compress and return any remaining data. The returned data should be appended to that returned by compress().
int mode
| one of the following:
|
Used when the data to be decompressed is not all in one buffer.
Construct. destbufsize is the same as for D.zlib.uncompress().
Decompress the data in buf and return the decompressed data. The buffers returned from successive calls to this should be concatenated together.
Decompress and return any remaining data. The returned data should be appended to that returned by uncompress(). The UnCompress object cannot be used further.
Returns true if all input data has been decompressed and no further data can be decompressed (inflate() returned Z_STREAM_END)
// some random data ubyte[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");
© 1999–2019 The D Language Foundation
Licensed under the Boost License 1.0.
https://dlang.org/phobos/std_zlib.html