Zlib: Difference between revisions

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
imported>The BestEditor4
 
fix "Jean-loup" since the linked article Jean-Loup Gailly also uses the capital L throughout
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
{{Refimprove|date=March 2021}}{{short description|DEFLATE codec library}}
{{Refimprove|date=March 2021}}{{short description|Data format and compression library}}
{{Lowercase title}}
{{Lowercase title}}
{{distinguish|Z-Library}}
{{distinguish|Z-Library}}
{{Infobox software
{{Infobox software
| name                  = zlib
| name                  = zlib (library)
| logo                  = [[Image:Zlib 3D green.svg|300px|zlib logo]]
| logo                  = [[Image:Zlib 3D green.svg|300px|zlib logo]]
| released              = {{Start date|1995|05|01|df=yes}}
| released              = {{Start date|1995|05|01|df=yes}}
Line 12: Line 12:
| genre                  = [[Data compression]]
| genre                  = [[Data compression]]
| license                = [[zlib License]]
| license                = [[zlib License]]
| website                = {{URL|zlib.net}}
| developer = {{ubl|[[Jean-Loup Gailly]]|[[Mark Adler]]}}<ref>{{cite web |title=zlib Home Site |website=zlib.net |url=https://zlib.net |archive-url=https://web.archive.org/web/20060602151730/https://zlib.net/ |archive-date=2 June 2006 |quote=zlib was written by Jean-Loup Gailly (compression) and Mark Adler (decompression).}}</ref>
}}
}}


'''zlib''' ({{IPAc-en|'|z|i:|l|I|b}} or "[[zeta]]-lib", {{IPAc-en|'|z|i:|t|@|,|l|I|b}})<ref>{{Cite book|url=https://books.google.com/books?id=hwJQOwg-MGYC&q=zeta|title=The Linux Cookbook, 2nd Edition: Tips and Techniques for Everyday Use|first=Michael|last=Stutz|date=October 22, 2004|publisher=No Starch Press|isbn=9781593270315|via=Google Books|access-date=October 28, 2021|archive-date=January 25, 2023|archive-url=https://web.archive.org/web/20230125112610/https://books.google.com/books?id=hwJQOwg-MGYC&q=zeta|url-status=live}}</ref><ref>{{Cite book |last2=Hall |first2=John R. |url=https://books.google.com/books?id=7eAPUdU3hRUC&q=zlib+zeelib&pg=PA68 |title=Programming Linux Games |date=October 22, 2001 |publisher=No Starch Press |isbn=9781886411494 |via=Google Books |author1=Loki Software |access-date=October 28, 2021 |archive-date=July 11, 2024 |archive-url=https://web.archive.org/web/20240711145515/https://books.google.com/books?id=7eAPUdU3hRUC&q=zlib+zeelib&pg=PA68#v=snippet&q=zlib%20zeelib&f=false |url-status=live }}</ref> is a [[library (computing)|software library]] used for [[data compression]] as well as a data format.<ref name=RFC1950/> zlib was written by [[Jean-loup Gailly]]<!-- Person prefers lowercase l (ell), see his article --> and [[Mark Adler]] and is an [[abstraction (computer science)|abstraction]] of the [[DEFLATE]] compression algorithm used in their [[gzip]] file compression program. 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]].
'''zlib''' ({{IPAc-en|'|z|i:|l|I|b}} or "[[zeta]]-lib", {{IPAc-en|'|z|i:|t|@|,|l|I|b}})<ref>{{Cite book|url=https://books.google.com/books?id=hwJQOwg-MGYC&q=zeta|title=The Linux Cookbook, 2nd Edition: Tips and Techniques for Everyday Use|first=Michael|last=Stutz|date=October 22, 2004|publisher=No Starch Press|isbn=9781593270315|via=Google Books|access-date=October 28, 2021|archive-date=January 25, 2023|archive-url=https://web.archive.org/web/20230125112610/https://books.google.com/books?id=hwJQOwg-MGYC&q=zeta|url-status=live}}</ref><ref>{{Cite book |last2=Hall |first2=John R. |url=https://books.google.com/books?id=7eAPUdU3hRUC&q=zlib+zeelib&pg=PA68 |title=Programming Linux Games |date=October 22, 2001 |publisher=No Starch Press |isbn=9781886411494 |via=Google Books |author1=Loki Software |access-date=October 28, 2021 |archive-date=July 11, 2024 |archive-url=https://web.archive.org/web/20240711145515/https://books.google.com/books?id=7eAPUdU3hRUC&q=zlib+zeelib&pg=PA68#v=snippet&q=zlib%20zeelib&f=false |url-status=live }}</ref> is a data format and a [[lossless data compression]] [[library (computing)|software library]] created by [[Jean-Loup Gailly]] and [[Mark Adler]]. The library implements the [[Deflate]] algorithm and supports compressing and decompressing data using the [[#Data format|zlib data format]], the [[gzip file format]], or simply as a [[Deflate stream]].<ref>{{cite web |title=zlib 1.3.1 Manual |website=zlib.net |url=https://zlib.net/manual.html |archive-url=https://web.archive.org/web/20240208162415/https://zlib.net/manual.html |archive-date=8 February 2024}}</ref>
 
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]].{{cn|date=February 2026}}


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]].
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]].


== Capabilities ==
RFC 1950 specifies the zlib data format.<ref>{{cite IETF |rfc=1950}}</ref>
 
== Data format ==
{{Infobox file format
| name = zlib (data format)
| developer = {{ubl|[[Jean-Loup Gailly]]|[[Mark Adler]]}}<ref>{{Cite IETF|rfc=1950|title=Acknowledgements|page=8|quote=Jean-Loup Gailly and Mark Adler designed the zlib format and&nbsp;...}}</ref>
| type = [[Data compression]]
| compression = [[Lossless]]
| standard = {{IETF RFC|1950}}
| max_size = Unlimited
| mime = <code>application/zlib</code><ref>{{cite IETF|rfc=6713}}</ref>
| container for = [[Deflate stream]]
}}
 
The zlib data format consists of a 2-[[byte]] [[Header (computing)|header]], an optional extra header field, Deflate-compressed data, and a 4-byte [[Trailer (computing)|trailer]].
 
[[Endianness]] is '''big-endian'''.<ref>{{Cite IETF|rfc=1950|title=Overall conventions|page=4|quote=All multi-byte numbers in the format described here are stored with the MOST-significant byte first (at the lower memory address).}}</ref> '''Bit 0''' is the [[least significant bit]] in a byte.<ref>{{Cite IETF|rfc=1950|title=Overall conventions|page=4|quote=...,&nbsp;we number the bits of a byte so that bit 0 is the least-significant bit,&nbsp;...}}</ref>
 
{| class="wikitable"
![[Offset (computer science)|Offset]] (bytes)
!Field<ref>{{Cite IETF|rfc=1950|title=Data format|page=4}}</ref>
!Size (bytes)
!Description
|-
|0
|CMF
|1
|
This byte is divided into two 4-bit fields:
* '''Bits 0 to 3''': <code>CM</code> ('''compression method'''). Must be 8&nbsp;(Deflate).
* '''Bits 4 to 7''': <code>CINFO</code> ('''compression info'''). Base-2 logarithm of the [[LZ77]] window size, minus eight. The maximum allowed value is 7 (i.e. 32 [[KiB]] window size). 6 indicates 16 KiB, 5 indicates 8 KiB, etc...
|-
|1
|FLG
|1
|
'''[[Flag (programming)|Flags]]'''.
* '''Bits 0 to 4''': <code>FCHECK</code>. This value must be chosen such that <code>CMF*256&nbsp;+&nbsp;FLG</code> is a '''multiple of 31'''.
* '''Bit 5''': <code>FDICT</code>. See <code>DICTID</code> below.
* '''Bits 6 to 7''': <code>FLEVEL</code> ('''compression level'''). Fastest (0), fast (1), default (2) or maximum (3). Not needed for decompression, but indicates if recompression might be worthwhile.
|-
|2
|DICTID
|0 or 4
|'''[[Adler-32]]''' checksum of the '''preset DEFLATE dictionary''' used during compression. Present if the '''FDICT''' flag is set. The decompressor can use this checksum to determine which dictionary has been used by the compressor (e.g. <code>deflateSetDictionary</code> in <code>zlib.h</code>).
|-
|rowspan=2" {{varies}}
|Compressed data
|{{{varies}}
|[[Deflate stream]].
|-
|ADLER32
|4
|'''Adler-32''' checksum of the '''uncompressed data''' (excluding any dictionary data).
|}
 
== <span class="anchor" id="Capabilities"></span> Library ==


=== Encapsulation ===
=== Encapsulation ===
Raw [[DEFLATE]] compressed data (RFC 1951)<ref>{{cite IETF |rfc=1951}}</ref> are typically written with a zlib or gzip wrapper encapsulating the data, by adding a header and footer. This provides stream identification and error detection that are not provided by the raw DEFLATE data.
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 wrapper (RFC 1950)<ref name=RFC1950>{{cite IETF |rfc=1950}}</ref> is smaller than the gzip wrapper (RFC 1952),<ref>{{cite IETF |rfc=1952}}</ref> as the latter stores a file name and other file system information.
The zlib format (at least 6 bytes) is smaller than the gzip format (at least 18 bytes) as the latter stores filesystem metadata.


=== Algorithm ===
=== Algorithm ===
{{As of|2018|9}}, zlib only supports one algorithm, called [[DEFLATE]], which uses a combination of a variation of [[LZ77 and LZ78#LZ77|LZ77]] (Lempel–Ziv 1977) and [[Huffman coding]].<ref>{{cite IETF |rfc=1951}}</ref> 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 (file format)|Zip archive format]]. The header makes allowance for other algorithms, but none are currently implemented.
{{As of|2018|9}}, zlib only supports one algorithm, called [[DEFLATE]], which uses a combination of a variation of LZ77 (Lempel–Ziv 1977) and [[Huffman coding]].<ref>{{cite IETF |rfc=1951}}</ref> 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 (file format)|Zip archive format]]. The header makes allowance for other algorithms, but none are currently implemented.


=== Resource use ===
=== Resource use ===

Latest revision as of 12:08, 26 June 2026

Template:RefimproveTemplate:Short description

Script error: No such module "Distinguish". Script error: No such module "Infobox". Script error: No such module "Check for unknown parameters".Script error: No such module "Check for conflicting parameters".

zlib (Script error: No such module "IPAc-en". or "zeta-lib", Script error: No such module "IPAc-en".)[1][2] 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.[3]

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.Script error: No such module "Unsubst".

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.[4]

Data format

Script error: No such module "Infobox".Script error: No such module "Check for unknown parameters".

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.[5] Bit 0 is the least significant bit in a byte.[6]

Offset (bytes) Field[7] Size (bytes) Description
0 CMF 1

This byte is divided into two 4-bit fields:

  • Bits 0 to 3: CM (compression method). Must be 8 (Deflate).
  • Bits 4 to 7: CINFO (compression info). Base-2 logarithm of the LZ77 window size, minus eight. The maximum allowed value is 7 (i.e. 32 KiB window size). 6 indicates 16 KiB, 5 indicates 8 KiB, etc...
1 FLG 1

Flags.

  • Bits 0 to 4: FCHECK. This value must be chosen such that CMF*256 + FLG is a multiple of 31.
  • Bit 5: FDICT. See DICTID below.
  • Bits 6 to 7: FLEVEL (compression level). Fastest (0), fast (1), default (2) or maximum (3). Not needed for decompression, but indicates if recompression might be worthwhile.
2 DICTID 0 or 4 Adler-32 checksum of the preset DEFLATE dictionary used during compression. Present if the FDICT flag is set. The decompressor can use this checksum to determine which dictionary has been used by the compressor (e.g. deflateSetDictionary in zlib.h).
rowspan=2" Template:Varies Compressed data {Template:Varies Deflate stream.
ADLER32 4 Adler-32 checksum of the uncompressed data (excluding any dictionary data).

Library

Encapsulation

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.

Algorithm

As of September 2018Script error: No such module "Check for unknown parameters"., zlib only supports one algorithm, called DEFLATE, which uses a combination of a variation of LZ77 (Lempel–Ziv 1977) and Huffman coding.[8] 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.

Resource use

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.

Strategy

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.

Error handling

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.

Data length

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.

Applications

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.[9] 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.

Forks

A commonly used library built on an old codebase, zlib is also frequently forked by third-parties that claim improvements to this library:

See also

Script error: No such module "Portal".

References

<templatestyles src="Reflist/styles.css" />

  1. ↑ Script error: No such module "citation/CS1".
  2. ↑ Script error: No such module "citation/CS1".
  3. ↑ Script error: No such module "citation/CS1".
  4. ↑ Script error: No such module "template wrapper".
  5. ↑ Script error: No such module "template wrapper".
  6. ↑ Script error: No such module "template wrapper".
  7. ↑ Script error: No such module "template wrapper".
  8. ↑ Script error: No such module "template wrapper".
  9. ↑ Script error: No such module "citation/CS1".
  10. ↑ Script error: No such module "citation/CS1".
  11. ↑ Script error: No such module "citation/CS1".
  12. ↑ System.IO.Compression.DeflateStream Script error: No such module "webarchive".. MSDN Library.
  13. ↑ zlib-flate(1) – Debian General Commands Manual
  14. ↑ Script error: No such module "citation/CS1".
  15. ↑ Script error: No such module "citation/CS1".
  16. ↑ Script error: No such module "citation/CS1".
  17. ↑ Script error: No such module "citation/CS1".
  18. ↑ Script error: No such module "citation/CS1".

Script error: No such module "Check for unknown parameters".

External links

  • Script error: No such module "Official website".Script error: No such module "Check for unknown parameters".