Movatterモバイル変換


[0]ホーム

URL:


D Logo
Menu
Search

Library Reference

version 2.112.0

overview

Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.Requires a signed-in GitHub account. This works well for small changes.If you'd like to make larger changes you may want to consider usinga local clone.

std.zlib

Compress/decompress data using thezlib library.
Examples:
If you have a small buffer you can usecompress 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 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

License:
Boost License 1.0.
Authors:
Walter Bright

Sourcestd/zlib.d

classZlibException:object.Exception;
Errors throw a ZlibException.
uintadler32(uintadler, const(void)[]buf);

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

Parameters:
uintadlerthe starting checksum for the computation. Use 1 for a new checksum. Use the output of this function for a cumulative checksum.
const(void)[]bufbuffer containing input data
Returns:
Auint checksum for the provided input data and starting checksum
See Also:
Examples:
staticubyte[] data = [1,2,3,4,5,6,7,8,9,10];uintadler =adler32(0u, data);writeln(adler);// 0xdc0037
uintcrc32(uintcrc, const(void)[]buf);

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

Parameters:
uintcrcthe starting checksum for the computation. Use 0 for a new checksum. Use the output of this function for a cumulative checksum.
const(void)[]bufbuffer containing input data
Returns:
Auint checksum for the provided input data and starting checksum
See Also:
ubyte[]compress(const(void)[]srcbuf, intlevel);

ubyte[]compress(const(void)[]srcbuf);

Compress data

Parameters:
const(void)[]srcbufbuffer containing the data to compress
intlevelcompression 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_tdestlen = 0u, intwinbits = 15);
Decompresses the data in srcbuf[].
Parameters:
const(void)[]srcbufbuffer containing the compressed data.
size_tdestlensize of the uncompressed data. It need not be accurate, but the decompression will be faster if the exact size is supplied.
intwinbitsthe base two logarithm of the maximum window size.
Returns:
the decompressed data.
enumHeaderFormat: int;
the header format the compressed stream is wrapped in
deflate
a standard zlib header
gzip
a gzip file format header
determineFromData
used when decompressing. Try to automatically detect the stream format by looking at the data
classCompress;
Used when the data to be compressed is not all in one buffer.
this(intlevel, HeaderFormatheader = HeaderFormat.deflate);

this(HeaderFormatheader = HeaderFormat.deflate);
Constructor.
Parameters:
intlevelcompression level. Legal values are 1 .. 9, with 1 being the least compression and 9 being the most. The default value is 6.
HeaderFormatheadersets the compression type to one of the options available inHeaderFormat. Defaults to HeaderFormat.deflate.
const(void)[]compress(const(void)[]buf);
Compress the data in buf and return the compressed data.
Parameters:
const(void)[]bufdata to compress
Returns:
the compressed data. The buffers returned from successive calls to this should be concatenated together.
void[]flush(intmode = Z_FINISH);
Compress and return any remaining data. The returned data should be appended to that returned by compress().
Parameters:
intmodeone 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.
classUnCompress;
Used when the data to be decompressed is not all in one buffer.
this(uintdestbufsize);

this(HeaderFormatformat = 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.
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.
@property boolempty() const;
Returns true if all input data has been decompressed and no further datacan be decompressed (inflate() returned Z_STREAM_END)
Examples:
// 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");
Copyright © 1999-2026 by theD Language Foundation | Page generated byDdoc on Fri Feb 20 21:26:01 2026

[8]ページ先頭

©2009-2026 Movatter.jp