std.zlib - D Programming Language (original) (raw)
Examples:
If you have a small buffer you can use compress anduncompress 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, useCompress 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; }
class ZlibException: object.Exception;
Errors throw a ZlibException.
uint adler32(uint adler, const(void)[] buf);
Compute the Adler-32 checksum of a buffer's worth of data.
Parameters:
| 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 |
Returns:
A uint checksum for the provided input data and starting checksum
Examples:
static ubyte[] data = [1,2,3,4,5,6,7,8,9,10];
uint adler = adler32(0u, data); writeln(adler);
uint crc32(uint crc, const(void)[] buf);
Compute the CRC32 checksum of a buffer's worth of data.
Parameters:
| 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 |
Returns:
A uint checksum for the provided input data and starting checksum
ubyte[] compress(const(void)[] srcbuf, int level);
ubyte[] compress(const(void)[] srcbuf);
Compress data
Parameters:
| 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. |
Returns:
the compressed data
void[] uncompress(const(void)[] srcbuf, size_t destlen = 0u, int winbits = 15);
Decompresses the data in srcbuf[].
Parameters:
| 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. |
Returns:
the decompressed data.
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.
this(int level, HeaderFormat header = HeaderFormat.deflate);
this(HeaderFormat header = HeaderFormat.deflate);
Constructor.
Parameters:
| 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. |
const(void)[] compress(const(void)[] buf);
Compress the data in buf and return the compressed data.
Parameters:
| const(void)[] buf | data to compress |
|---|
Returns:
the compressed data. The buffers returned from successive calls to this should be concatenated together.
void[] flush(int mode = Z_FINISH);
Compress and return any remaining data. The returned data should be appended to that returned by compress().
Parameters:
| int mode | one of the following: Z_SYNC_FLUSH Syncs up flushing to the next byte boundary. Used when more data is to be compressed later on. Z_FULL_FLUSH Syncs up flushing to the next byte boundary. Used when more data is to be compressed later on, and the decompressor needs to be restartable at this point. Z_FINISH (default) Used when finished compressing the data. |
|---|
Used when the data to be decompressed is not all in one buffer.
this(uint destbufsize);
this(HeaderFormat format = HeaderFormat.determineFromData);
Construct. destbufsize is the same as for D.zlib.uncompress().
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.
Decompress and return any remaining data. The returned data should be appended to that returned by uncompress(). The UnCompress object cannot be used further.
@property bool empty() const;
Returns true if all input data has been decompressed and no further data can be decompressed (inflate() returned Z_STREAM_END)
Examples:
ubyte[1024] originalData = void;
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");
Copyright © 1999-2026 by the D Language Foundation | Page generated byDdoc on Tue Apr 21 19:05:35 2026