Sophie

Sophie

distrib > Mandriva > 2010.1 > x86_64 > media > contrib-backports > by-pkgid > 3ba3bd1608c672ba2129b098a48e9e4d > files > 867

python3-docs-3.2.2-3mdv2010.2.noarch.rpm



<!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 &mdash; Python v3.2.2 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:     '3.2.2',
        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 v3.2.2 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 v3.2.2 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" />
 

  </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="../index.html">Python v3.2.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="archiving.html" accesskey="U">12. Data Compression and Archiving</a> &raquo;</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> &#8212; 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&#8217;s functions have many options and often need to be used in a particular
order.  This documentation doesn&#8217;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. For
other archive formats, see the <a class="reference internal" href="bz2.html#module-bz2" title="bz2: Interface to compression and decompression routines compatible with bzip2."><tt class="xref py py-mod docutils literal"><span class="pre">bz2</span></tt></a>, <a class="reference internal" href="zipfile.html#module-zipfile" title="zipfile: Read and write ZIP-format archive files."><tt class="xref py py-mod docutils literal"><span class="pre">zipfile</span></tt></a>, and
<a class="reference internal" href="tarfile.html#module-tarfile" title="tarfile: Read and write tar-format archive files."><tt class="xref py py-mod docutils literal"><span class="pre">tarfile</span></tt></a> modules.</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>Always returns an unsigned 32-bit integer.</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) &amp; 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>
<dl class="function">
<dt id="zlib.compress">
<tt class="descclassname">zlib.</tt><tt class="descname">compress</tt><big>(</big><em>data</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 bytes in <em>data</em>, returning a bytes object containing compressed data.
<em>level</em> is an integer from <tt class="docutils literal"><span class="pre">1</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.  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&#8217;t
fit into memory at once.  <em>level</em> is an integer from <tt class="docutils literal"><span class="pre">1</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.  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>Always returns an unsigned 32-bit integer.</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) &amp; 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>
<dl class="function">
<dt id="zlib.decompress">
<tt class="descclassname">zlib.</tt><tt class="descname">decompress</tt><big>(</big><em>data</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 bytes in <em>data</em>, returning a bytes object 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 &#8220;window size&#8221;) 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&#8217;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&#8217;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>data</em><big>)</big><a class="headerlink" href="#zlib.Compress.compress" title="Permalink to this definition">¶</a></dt>
<dd><p>Compress <em>data</em>, returning a bytes object containing compressed data for at least
part of the data in <em>data</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 bytes object 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 bytestrings 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>
</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 bytes object which contains any bytes past the end of the compressed data. That is,
this remains <tt class="docutils literal"><span class="pre">&quot;&quot;</span></tt> until the last byte that contains compression data is
available.  If the whole bytestring turned out to contain compressed data, this is
<tt class="docutils literal"><span class="pre">b&quot;&quot;</span></tt>, an empty bytes object.</p>
<p>The only way to determine where a bytestring 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 bytestring into a decompression object&#8217;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
empty.</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 bytes object 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>data</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>data</em>, returning a bytes object 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 bytestring 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
empty.</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 bytes object 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>
</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&#8217;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 docutils literal docutils literal"><span class="pre">gzip</span></tt> &#8212; 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" size="18" />
      <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="../index.html">Python v3.2.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="archiving.html" >12. Data Compression and Archiving</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2011, 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 Sep 04, 2011.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
    </div>

  </body>
</html>