Menu

- Learn
- Documentation
  - Language Reference
  - Library Reference
  - Command-line Reference
  - Feature Overview
  - Articles
- Downloads
- Packages
- Community
  - Blog
  - Orgs using D
  - Twitter
  - Forums
  - IRC
  - Community Discord
  - Wiki
  - GitHub
  - Issues
  - Get involved
  - Contributors
  - Foundation
  - Security Team
  - Donate
  - Sponsors
- Resources
  - Tour
  - Books
  - Tutorials
  - Tools
  - Editors
  - IDEs
  - run.dlang.io
  - Visual D
  - Acknowledgments
  - D Style
  - Glossary
  - Sitemap

Search
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 using a local clone.

# 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

License:
Boost License 1.0.
Authors:
Walter Bright

Source std/zlib.d

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
  See Also:
  http://en.wikipedia.org/wiki/Adler-32
  Examples:

      static ubyte[] data = [1,2,3,4,5,6,7,8,9,10];

      uint adler = adler32(0u, data);
      writeln(adler); // 0xdc0037

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
  See Also:
  http://en.wikipedia.org/wiki/Cyclic_redundancy_check
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.
enum `HeaderFormat`: 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
class `Compress`;
  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.

    See Also:
    compress, HeaderFormat
  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.

class `UnCompress`;
  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.
  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.
  const @property bool `empty`();
    Returns true if all input data has been decompressed and no further data can be decompressed (inflate() returned Z_STREAM_END)
    Examples:

        // 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");

Copyright © 1999-2023 by the D Language Foundation | Page generated by Ddoc on Wed Feb 1 17:27:31 2023