zlib ( or "zeta-lib", ) is a data format and a lossless data compression software library created by Jean-loup Gailly and Mark Adler. The library implements the Deflate algorithm and supports compressing and decompressing data using the zlib data format, the gzip file format, or simply as a Deflate stream.
zlib is also a crucial component of many software platforms, including Linux, macOS, and iOS. It has also been used in gaming consoles such as the PlayStation 4, PlayStation 3, Wii U, Wii, Xbox One and Xbox 360.
The first public version of Zlib, 0.9, was released on 1 May 1995 and was originally intended for use with the libpng image library. It is free software, distributed under the zlib License.
RFC 1950 specifies the zlib data format.
The zlib data format consists of a 2-byte header, an optional extra header field, Deflate-compressed data, and a 4-byte trailer.
Endianness is big-endian. Bit 0 is the least significant bit in a byte.
Deflate compression typically uses the zlib data format or the gzip file format, which add a header and a trailer to the compressed data. This provides stream identification and error detection that are not provided by a Deflate stream.
The zlib format (at least 6 bytes) is smaller than the gzip format (at least 18 bytes) as the latter stores filesystem metadata.
, zlib only supports one algorithm, called DEFLATE, which uses a combination of a variation of LZ77 (LempelâÂÂZiv 1977) and Huffman coding. This algorithm provides good compression on a wide variety of data with minimal use of system resources. This is also the algorithm used in the Zip archive format. The header makes allowance for other algorithms, but none are currently implemented.
zlib provides facilities for control of processor and memory use. A compression level value may be supplied that trades speed for compression. There are also facilities for conserving memory, useful in restricted memory environments, such as some embedded systems.
The compression can be optimized for specific types of data. If one is using the library to always compress specific types of data, then using a specific strategy may improve compression and performance. For example, if the data contain long lengths of repeated bytes, the run-length encoding (RLE) strategy may give good results at a higher speed. For general data, the default strategy is preferred.
Errors in compressed data may be detected and skipped. Further, if "full-flush" points are written to the compressed stream, then corrupt data can be skipped, and the decompression will resynchronize at the next flush pointâÂÂalthough no error recovery of the corrupt data is provided. Full-flush points are useful for large data streams on unreliable channels, where some data loss is unimportant, such as in some multimedia applications. However, creating many flush points can affect the speed as well as the amount (ratio) of compression.
There is no limit to the length of data that can be compressed or decompressed. Repeated calls to the library allow an unlimited number of blocks of data to be handled. Some ancillary code (counters) may suffer from overflow for long data streams, but this does not affect the actual compression or decompression.
When compressing a long (or infinite) data stream, it is advisable to write regular full-flush points.
Today, zlib is something of a de facto standard, to the point that zlib and DEFLATE are often used interchangeably in standards documents, with thousands of applications relying on it for compression, either directly or indirectly. These include:
zlib is also used in many embedded devices, such as the Apple iPhone and Sony PlayStation 3, because the code is portable, liberally licensed, and has a relatively small memory footprint.
A commonly used library built on an old codebase, zlib is also frequently forked by third-parties that claim improvements to this library: