Sophie

Sophie

distrib > Mandriva > current > i586 > media > main-updates > by-pkgid > 8f1462e52e1797a02c97073eed0b7f92 > files > 960

python-docs-2.6.5-2.5mdv2010.2.i586.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>13.1. zlib — Compression compatible with gzip &mdash; Python v2.6.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.6.5',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.6.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 v2.6.5 documentation" href="../index.html" />
    <link rel="up" title="13. Data Compression and Archiving" href="archiving.html" />
    <link rel="next" title="13.2. gzip — Support for gzip files" href="gzip.html" />
    <link rel="prev" title="13. 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="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="gzip.html" title="13.2. gzip — Support for gzip files"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="archiving.html" title="13. 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 v2.6.5 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="archiving.html" accesskey="U">13. 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">
<h1>13.1. <tt class="xref docutils literal"><span class="pre">zlib</span></tt> &#8212; Compression compatible with <strong>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 title="Interfaces for gzip compression and decompression using file objects." class="reference external" href="gzip.html#module-gzip"><tt class="xref docutils literal"><span class="pre">gzip</span></tt></a> module. For
other archive formats, see the <a title="Interface to compression and decompression routines compatible with bzip2." class="reference external" href="bz2.html#module-bz2"><tt class="xref docutils literal"><span class="pre">bz2</span></tt></a>, <a title="Read and write ZIP-format archive files." class="reference external" href="zipfile.html#module-zipfile"><tt class="xref docutils literal"><span class="pre">zipfile</span></tt></a>, and
<a title="Read and write tar-format archive files." class="reference external" href="tarfile.html#module-tarfile"><tt class="xref 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>Exception raised on compression and decompression errors.</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) &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>
<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>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">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 title="zlib.error" class="reference internal" href="#zlib.error"><tt class="xref docutils literal"><span class="pre">error</span></tt></a>
exception if any error occurs.</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>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>.</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-762">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) &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>
<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.  If <em>bufsize</em> is given, it is used as the initial size of the output
buffer.  Raises the <a title="zlib.error" class="reference internal" href="#zlib.error"><tt class="xref 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.  The default value is 15.  When <em>wbits</em> is negative, the standard
<strong>gzip</strong> header is suppressed; this is an undocumented feature of the
zlib library, used for compatibility with <strong>unzip</strong>&#8216;s compression file
format.</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 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>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.</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>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 title="zlib.compress" class="reference internal" href="#zlib.compress"><tt class="xref docutils literal"><span class="pre">compress()</span></tt></a> method.  Some input may
be kept in internal buffers for later processing.</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>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 docutils literal"><span class="pre">Z_SYNC_FLUSH</span></tt>,  <tt class="xref docutils literal"><span class="pre">Z_FULL_FLUSH</span></tt>,  or  <tt class="xref docutils literal"><span class="pre">Z_FINISH</span></tt>,
defaulting to <tt class="xref docutils literal"><span class="pre">Z_FINISH</span></tt>.  <tt class="xref docutils literal"><span class="pre">Z_SYNC_FLUSH</span></tt> and
<tt class="xref docutils literal"><span class="pre">Z_FULL_FLUSH</span></tt> allow compressing further strings of data, while
<tt class="xref docutils literal"><span class="pre">Z_FINISH</span></tt> finishes the compressed stream and  prevents compressing any
more data.  After calling <a title="zlib.Compress.flush" class="reference internal" href="#zlib.Compress.flush"><tt class="xref docutils literal"><span class="pre">flush()</span></tt></a> with <em>mode</em> set to <tt class="xref docutils literal"><span class="pre">Z_FINISH</span></tt>,
the <a title="zlib.compress" class="reference internal" href="#zlib.compress"><tt class="xref docutils literal"><span class="pre">compress()</span></tt></a> method cannot be called again; the only realistic action is
to delete the object.</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">&quot;&quot;</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">&quot;&quot;</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&#8217;s
<a title="zlib.decompress" class="reference internal" href="#zlib.decompress"><tt class="xref docutils literal"><span class="pre">decompress()</span></tt></a> method until the <a title="zlib.Decompress.unused_data" class="reference internal" href="#zlib.Decompress.unused_data"><tt class="xref 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>A string that contains any data that was not consumed by the last
<a title="zlib.decompress" class="reference internal" href="#zlib.decompress"><tt class="xref 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 title="zlib.decompress" class="reference internal" href="#zlib.decompress"><tt class="xref docutils literal"><span class="pre">decompress()</span></tt></a> method call in order to get correct output.</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 title="zlib.decompress" class="reference internal" href="#zlib.decompress"><tt class="xref 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 title="zlib.Decompress.unconsumed_tail" class="reference internal" href="#zlib.Decompress.unconsumed_tail"><tt class="xref docutils literal"><span class="pre">unconsumed_tail</span></tt></a>. This string must be passed to a subsequent call to
<a title="zlib.decompress" class="reference internal" href="#zlib.decompress"><tt class="xref 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 title="zlib.Decompress.unconsumed_tail" class="reference internal" href="#zlib.Decompress.unconsumed_tail"><tt class="xref 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 title="zlib.Decompress.flush" class="reference internal" href="#zlib.Decompress.flush"><tt class="xref docutils literal"><span class="pre">flush()</span></tt></a>, the
<a title="zlib.decompress" class="reference internal" href="#zlib.decompress"><tt class="xref 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 title="Interfaces for gzip compression and decompression using file objects." class="reference external" href="gzip.html#module-gzip"><tt class="xref docutils literal"><span class="pre">gzip</span></tt></a></dt>
<dd>Reading and writing <strong>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">13. Data Compression and Archiving</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="gzip.html"
                                  title="next chapter">13.2. <tt class="docutils literal docutils literal docutils literal"><span class="pre">gzip</span></tt> &#8212; Support for <strong>gzip</strong> files</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <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="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="gzip.html" title="13.2. gzip — Support for gzip files"
             >next</a> |</li>
        <li class="right" >
          <a href="archiving.html" title="13. 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 v2.6.5 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="archiving.html" >13. Data Compression and Archiving</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2010, 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 Mar 19, 2010.
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.5.
    </div>

  </body>
</html>