korsygfhrtzangaiide
Elepffwdsff
/
usr
/
share
/
doc
/
python-docs-2.7.5
/
html
/
library
/
Upload FileeE
HOME
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>12.1. zlib — Compression compatible with gzip — Python 2.7.5 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '2.7.5', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python 2.7.5 documentation" href="../index.html" /> <link rel="up" title="12. Data Compression and Archiving" href="archiving.html" /> <link rel="next" title="12.2. gzip — Support for gzip files" href="gzip.html" /> <link rel="prev" title="12. Data Compression and Archiving" href="archiving.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="gzip.html" title="12.2. gzip — Support for gzip files" accesskey="N">next</a> |</li> <li class="right" > <a href="archiving.html" title="12. Data Compression and Archiving" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="archiving.html" accesskey="U">12. Data Compression and Archiving</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="module-zlib"> <span id="zlib-compression-compatible-with-gzip"></span><h1>12.1. <a class="reference internal" href="#module-zlib" title="zlib: Low-level interface to compression and decompression routines compatible with gzip."><tt class="xref py py-mod docutils literal"><span class="pre">zlib</span></tt></a> — Compression compatible with <strong class="program">gzip</strong><a class="headerlink" href="#module-zlib" title="Permalink to this headline">¶</a></h1> <p>For applications that require data compression, the functions in this module allow compression and decompression, using the zlib library. The zlib library has its own home page at <a class="reference external" href="http://www.zlib.net">http://www.zlib.net</a>. There are known incompatibilities between the Python module and versions of the zlib library earlier than 1.1.3; 1.1.3 has a security vulnerability, so we recommend using 1.1.4 or later.</p> <p>zlib’s functions have many options and often need to be used in a particular order. This documentation doesn’t attempt to cover all of the permutations; consult the zlib manual at <a class="reference external" href="http://www.zlib.net/manual.html">http://www.zlib.net/manual.html</a> for authoritative information.</p> <p>For reading and writing <tt class="docutils literal"><span class="pre">.gz</span></tt> files see the <a class="reference internal" href="gzip.html#module-gzip" title="gzip: Interfaces for gzip compression and decompression using file objects."><tt class="xref py py-mod docutils literal"><span class="pre">gzip</span></tt></a> module.</p> <p>The available exception and functions in this module are:</p> <dl class="exception"> <dt id="zlib.error"> <em class="property">exception </em><tt class="descclassname">zlib.</tt><tt class="descname">error</tt><a class="headerlink" href="#zlib.error" title="Permalink to this definition">¶</a></dt> <dd><p>Exception raised on compression and decompression errors.</p> </dd></dl> <dl class="function"> <dt id="zlib.adler32"> <tt class="descclassname">zlib.</tt><tt class="descname">adler32</tt><big>(</big><em>data</em><span class="optional">[</span>, <em>value</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#zlib.adler32" title="Permalink to this definition">¶</a></dt> <dd><p>Computes a Adler-32 checksum of <em>data</em>. (An Adler-32 checksum is almost as reliable as a CRC32 but can be computed much more quickly.) If <em>value</em> is present, it is used as the starting value of the checksum; otherwise, a fixed default value is used. This allows computing a running checksum over the concatenation of several inputs. The algorithm is not cryptographically strong, and should not be used for authentication or digital signatures. Since the algorithm is designed for use as a checksum algorithm, it is not suitable for use as a general hash algorithm.</p> <p>This function always returns an integer object.</p> </dd></dl> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">To generate the same numeric value across all Python versions and platforms use adler32(data) & 0xffffffff. If you are only using the checksum in packed binary format this is not necessary as the return value is the correct 32bit binary representation regardless of sign.</p> </div> <p class="versionchanged"> <span class="versionmodified">Changed in version 2.6: </span>The return value is in the range [-2**31, 2**31-1] regardless of platform. In older versions the value is signed on some platforms and unsigned on others.</p> <p class="versionchanged"> <span class="versionmodified">Changed in version 3.0: </span>The return value is unsigned and in the range [0, 2**32-1] regardless of platform.</p> <dl class="function"> <dt id="zlib.compress"> <tt class="descclassname">zlib.</tt><tt class="descname">compress</tt><big>(</big><em>string</em><span class="optional">[</span>, <em>level</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#zlib.compress" title="Permalink to this definition">¶</a></dt> <dd><p>Compresses the data in <em>string</em>, returning a string contained compressed data. <em>level</em> is an integer from <tt class="docutils literal"><span class="pre">0</span></tt> to <tt class="docutils literal"><span class="pre">9</span></tt> controlling the level of compression; <tt class="docutils literal"><span class="pre">1</span></tt> is fastest and produces the least compression, <tt class="docutils literal"><span class="pre">9</span></tt> is slowest and produces the most. <tt class="docutils literal"><span class="pre">0</span></tt> is no compression. The default value is <tt class="docutils literal"><span class="pre">6</span></tt>. Raises the <a class="reference internal" href="#zlib.error" title="zlib.error"><tt class="xref py py-exc docutils literal"><span class="pre">error</span></tt></a> exception if any error occurs.</p> </dd></dl> <dl class="function"> <dt id="zlib.compressobj"> <tt class="descclassname">zlib.</tt><tt class="descname">compressobj</tt><big>(</big><span class="optional">[</span><em>level</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#zlib.compressobj" title="Permalink to this definition">¶</a></dt> <dd><p>Returns a compression object, to be used for compressing data streams that won’t fit into memory at once. <em>level</em> is an integer from <tt class="docutils literal"><span class="pre">0</span></tt> to <tt class="docutils literal"><span class="pre">9</span></tt> controlling the level of compression; <tt class="docutils literal"><span class="pre">1</span></tt> is fastest and produces the least compression, <tt class="docutils literal"><span class="pre">9</span></tt> is slowest and produces the most. <tt class="docutils literal"><span class="pre">0</span></tt> is no compression. The default value is <tt class="docutils literal"><span class="pre">6</span></tt>.</p> </dd></dl> <dl class="function"> <dt id="zlib.crc32"> <tt class="descclassname">zlib.</tt><tt class="descname">crc32</tt><big>(</big><em>data</em><span class="optional">[</span>, <em>value</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#zlib.crc32" title="Permalink to this definition">¶</a></dt> <dd><p id="index-0">Computes a CRC (Cyclic Redundancy Check) checksum of <em>data</em>. If <em>value</em> is present, it is used as the starting value of the checksum; otherwise, a fixed default value is used. This allows computing a running checksum over the concatenation of several inputs. The algorithm is not cryptographically strong, and should not be used for authentication or digital signatures. Since the algorithm is designed for use as a checksum algorithm, it is not suitable for use as a general hash algorithm.</p> <p>This function always returns an integer object.</p> </dd></dl> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">To generate the same numeric value across all Python versions and platforms use crc32(data) & 0xffffffff. If you are only using the checksum in packed binary format this is not necessary as the return value is the correct 32bit binary representation regardless of sign.</p> </div> <p class="versionchanged"> <span class="versionmodified">Changed in version 2.6: </span>The return value is in the range [-2**31, 2**31-1] regardless of platform. In older versions the value would be signed on some platforms and unsigned on others.</p> <p class="versionchanged"> <span class="versionmodified">Changed in version 3.0: </span>The return value is unsigned and in the range [0, 2**32-1] regardless of platform.</p> <dl class="function"> <dt id="zlib.decompress"> <tt class="descclassname">zlib.</tt><tt class="descname">decompress</tt><big>(</big><em>string</em><span class="optional">[</span>, <em>wbits</em><span class="optional">[</span>, <em>bufsize</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#zlib.decompress" title="Permalink to this definition">¶</a></dt> <dd><p>Decompresses the data in <em>string</em>, returning a string containing the uncompressed data. The <em>wbits</em> parameter controls the size of the window buffer, and is discussed further below. If <em>bufsize</em> is given, it is used as the initial size of the output buffer. Raises the <a class="reference internal" href="#zlib.error" title="zlib.error"><tt class="xref py py-exc docutils literal"><span class="pre">error</span></tt></a> exception if any error occurs.</p> <p>The absolute value of <em>wbits</em> is the base two logarithm of the size of the history buffer (the “window size”) used when compressing data. Its absolute value should be between 8 and 15 for the most recent versions of the zlib library, larger values resulting in better compression at the expense of greater memory usage. When decompressing a stream, <em>wbits</em> must not be smaller than the size originally used to compress the stream; using a too-small value will result in an exception. The default value is therefore the highest value, 15. When <em>wbits</em> is negative, the standard <strong class="program">gzip</strong> header is suppressed.</p> <p><em>bufsize</em> is the initial size of the buffer used to hold decompressed data. If more space is required, the buffer size will be increased as needed, so you don’t have to get this value exactly right; tuning it will only save a few calls to <tt class="xref c c-func docutils literal"><span class="pre">malloc()</span></tt>. The default size is 16384.</p> </dd></dl> <dl class="function"> <dt id="zlib.decompressobj"> <tt class="descclassname">zlib.</tt><tt class="descname">decompressobj</tt><big>(</big><span class="optional">[</span><em>wbits</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#zlib.decompressobj" title="Permalink to this definition">¶</a></dt> <dd><p>Returns a decompression object, to be used for decompressing data streams that won’t fit into memory at once. The <em>wbits</em> parameter controls the size of the window buffer.</p> </dd></dl> <p>Compression objects support the following methods:</p> <dl class="method"> <dt id="zlib.Compress.compress"> <tt class="descclassname">Compress.</tt><tt class="descname">compress</tt><big>(</big><em>string</em><big>)</big><a class="headerlink" href="#zlib.Compress.compress" title="Permalink to this definition">¶</a></dt> <dd><p>Compress <em>string</em>, returning a string containing compressed data for at least part of the data in <em>string</em>. This data should be concatenated to the output produced by any preceding calls to the <a class="reference internal" href="#zlib.compress" title="zlib.compress"><tt class="xref py py-meth docutils literal"><span class="pre">compress()</span></tt></a> method. Some input may be kept in internal buffers for later processing.</p> </dd></dl> <dl class="method"> <dt id="zlib.Compress.flush"> <tt class="descclassname">Compress.</tt><tt class="descname">flush</tt><big>(</big><span class="optional">[</span><em>mode</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#zlib.Compress.flush" title="Permalink to this definition">¶</a></dt> <dd><p>All pending input is processed, and a string containing the remaining compressed output is returned. <em>mode</em> can be selected from the constants <tt class="xref py py-const docutils literal"><span class="pre">Z_SYNC_FLUSH</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">Z_FULL_FLUSH</span></tt>, or <tt class="xref py py-const docutils literal"><span class="pre">Z_FINISH</span></tt>, defaulting to <tt class="xref py py-const docutils literal"><span class="pre">Z_FINISH</span></tt>. <tt class="xref py py-const docutils literal"><span class="pre">Z_SYNC_FLUSH</span></tt> and <tt class="xref py py-const docutils literal"><span class="pre">Z_FULL_FLUSH</span></tt> allow compressing further strings of data, while <tt class="xref py py-const docutils literal"><span class="pre">Z_FINISH</span></tt> finishes the compressed stream and prevents compressing any more data. After calling <a class="reference internal" href="#zlib.Compress.flush" title="zlib.Compress.flush"><tt class="xref py py-meth docutils literal"><span class="pre">flush()</span></tt></a> with <em>mode</em> set to <tt class="xref py py-const docutils literal"><span class="pre">Z_FINISH</span></tt>, the <a class="reference internal" href="#zlib.compress" title="zlib.compress"><tt class="xref py py-meth docutils literal"><span class="pre">compress()</span></tt></a> method cannot be called again; the only realistic action is to delete the object.</p> </dd></dl> <dl class="method"> <dt id="zlib.Compress.copy"> <tt class="descclassname">Compress.</tt><tt class="descname">copy</tt><big>(</big><big>)</big><a class="headerlink" href="#zlib.Compress.copy" title="Permalink to this definition">¶</a></dt> <dd><p>Returns a copy of the compression object. This can be used to efficiently compress a set of data that share a common initial prefix.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.5.</span></p> </dd></dl> <p>Decompression objects support the following methods, and two attributes:</p> <dl class="attribute"> <dt id="zlib.Decompress.unused_data"> <tt class="descclassname">Decompress.</tt><tt class="descname">unused_data</tt><a class="headerlink" href="#zlib.Decompress.unused_data" title="Permalink to this definition">¶</a></dt> <dd><p>A string which contains any bytes past the end of the compressed data. That is, this remains <tt class="docutils literal"><span class="pre">""</span></tt> until the last byte that contains compression data is available. If the whole string turned out to contain compressed data, this is <tt class="docutils literal"><span class="pre">""</span></tt>, the empty string.</p> <p>The only way to determine where a string of compressed data ends is by actually decompressing it. This means that when compressed data is contained part of a larger file, you can only find the end of it by reading data and feeding it followed by some non-empty string into a decompression object’s <a class="reference internal" href="#zlib.decompress" title="zlib.decompress"><tt class="xref py py-meth docutils literal"><span class="pre">decompress()</span></tt></a> method until the <a class="reference internal" href="#zlib.Decompress.unused_data" title="zlib.Decompress.unused_data"><tt class="xref py py-attr docutils literal"><span class="pre">unused_data</span></tt></a> attribute is no longer the empty string.</p> </dd></dl> <dl class="attribute"> <dt id="zlib.Decompress.unconsumed_tail"> <tt class="descclassname">Decompress.</tt><tt class="descname">unconsumed_tail</tt><a class="headerlink" href="#zlib.Decompress.unconsumed_tail" title="Permalink to this definition">¶</a></dt> <dd><p>A string that contains any data that was not consumed by the last <a class="reference internal" href="#zlib.decompress" title="zlib.decompress"><tt class="xref py py-meth docutils literal"><span class="pre">decompress()</span></tt></a> call because it exceeded the limit for the uncompressed data buffer. This data has not yet been seen by the zlib machinery, so you must feed it (possibly with further data concatenated to it) back to a subsequent <a class="reference internal" href="#zlib.decompress" title="zlib.decompress"><tt class="xref py py-meth docutils literal"><span class="pre">decompress()</span></tt></a> method call in order to get correct output.</p> </dd></dl> <dl class="method"> <dt id="zlib.Decompress.decompress"> <tt class="descclassname">Decompress.</tt><tt class="descname">decompress</tt><big>(</big><em>string</em><span class="optional">[</span>, <em>max_length</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#zlib.Decompress.decompress" title="Permalink to this definition">¶</a></dt> <dd><p>Decompress <em>string</em>, returning a string containing the uncompressed data corresponding to at least part of the data in <em>string</em>. This data should be concatenated to the output produced by any preceding calls to the <a class="reference internal" href="#zlib.decompress" title="zlib.decompress"><tt class="xref py py-meth docutils literal"><span class="pre">decompress()</span></tt></a> method. Some of the input data may be preserved in internal buffers for later processing.</p> <p>If the optional parameter <em>max_length</em> is supplied then the return value will be no longer than <em>max_length</em>. This may mean that not all of the compressed input can be processed; and unconsumed data will be stored in the attribute <a class="reference internal" href="#zlib.Decompress.unconsumed_tail" title="zlib.Decompress.unconsumed_tail"><tt class="xref py py-attr docutils literal"><span class="pre">unconsumed_tail</span></tt></a>. This string must be passed to a subsequent call to <a class="reference internal" href="#zlib.decompress" title="zlib.decompress"><tt class="xref py py-meth docutils literal"><span class="pre">decompress()</span></tt></a> if decompression is to continue. If <em>max_length</em> is not supplied then the whole input is decompressed, and <a class="reference internal" href="#zlib.Decompress.unconsumed_tail" title="zlib.Decompress.unconsumed_tail"><tt class="xref py py-attr docutils literal"><span class="pre">unconsumed_tail</span></tt></a> is an empty string.</p> </dd></dl> <dl class="method"> <dt id="zlib.Decompress.flush"> <tt class="descclassname">Decompress.</tt><tt class="descname">flush</tt><big>(</big><span class="optional">[</span><em>length</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#zlib.Decompress.flush" title="Permalink to this definition">¶</a></dt> <dd><p>All pending input is processed, and a string containing the remaining uncompressed output is returned. After calling <a class="reference internal" href="#zlib.Decompress.flush" title="zlib.Decompress.flush"><tt class="xref py py-meth docutils literal"><span class="pre">flush()</span></tt></a>, the <a class="reference internal" href="#zlib.decompress" title="zlib.decompress"><tt class="xref py py-meth docutils literal"><span class="pre">decompress()</span></tt></a> method cannot be called again; the only realistic action is to delete the object.</p> <p>The optional parameter <em>length</em> sets the initial size of the output buffer.</p> </dd></dl> <dl class="method"> <dt id="zlib.Decompress.copy"> <tt class="descclassname">Decompress.</tt><tt class="descname">copy</tt><big>(</big><big>)</big><a class="headerlink" href="#zlib.Decompress.copy" title="Permalink to this definition">¶</a></dt> <dd><p>Returns a copy of the decompression object. This can be used to save the state of the decompressor midway through the data stream in order to speed up random seeks into the stream at a future point.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.5.</span></p> </dd></dl> <div class="admonition-see-also admonition seealso"> <p class="first admonition-title">See also</p> <dl class="last docutils"> <dt>Module <a class="reference internal" href="gzip.html#module-gzip" title="gzip: Interfaces for gzip compression and decompression using file objects."><tt class="xref py py-mod docutils literal"><span class="pre">gzip</span></tt></a></dt> <dd>Reading and writing <strong class="program">gzip</strong>-format files.</dd> <dt><a class="reference external" href="http://www.zlib.net">http://www.zlib.net</a></dt> <dd>The zlib library home page.</dd> <dt><a class="reference external" href="http://www.zlib.net/manual.html">http://www.zlib.net/manual.html</a></dt> <dd>The zlib manual explains the semantics and usage of the library’s many functions.</dd> </dl> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h4>Previous topic</h4> <p class="topless"><a href="archiving.html" title="previous chapter">12. Data Compression and Archiving</a></p> <h4>Next topic</h4> <p class="topless"><a href="gzip.html" title="next chapter">12.2. <tt class="docutils literal"><span class="pre">gzip</span></tt> — Support for <strong class="program">gzip</strong> files</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/library/zlib.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="gzip.html" title="12.2. gzip — Support for gzip files" >next</a> |</li> <li class="right" > <a href="archiving.html" title="12. Data Compression and Archiving" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="archiving.html" >12. Data Compression and Archiving</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2019, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Jul 03, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>