2019-04-21 11:50:48 +08:00

16 lines
24 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<div class="body" role="main"><div class="section" id="module-gzip"><h1><span class="yiyi-st" id="yiyi-10">13.2。 <a class="reference internal" href="#module-gzip" title="gzip: Interfaces for gzip compression and decompression using file objects."><code class="xref py py-mod docutils literal"><span class="pre">gzip</span></code></a> - 支持<strong class="program">gzip</strong>文件</span></h1><p><span class="yiyi-st" id="yiyi-11"><strong>源代码:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.5/Lib/gzip.py">Lib/gzip.py</a></span></p><p><span class="yiyi-st" id="yiyi-12">此模块提供了一个简单的接口来压缩和解压缩文件就像GNU程序<strong class="program">gzip</strong><strong class="program">gunzip</strong></span></p><p><span class="yiyi-st" id="yiyi-13">数据压缩由<a class="reference internal" href="zlib.html#module-zlib" title="zlib: Low-level interface to compression and decompression routines compatible with gzip."><code class="xref py py-mod docutils literal"><span class="pre">zlib</span></code></a>模块提供。</span></p><p><span class="yiyi-st" id="yiyi-14"><a class="reference internal" href="#module-gzip" title="gzip: Interfaces for gzip compression and decompression using file objects."><code class="xref py py-mod docutils literal"><span class="pre">gzip</span></code></a>模块提供<a class="reference internal" href="#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal"><span class="pre">GzipFile</span></code></a>类以及<a class="reference internal" href="#gzip.open" title="gzip.open"><code class="xref py py-func docutils literal"><span class="pre">open()</span></code></a><a class="reference internal" href="#gzip.compress" title="gzip.compress"><code class="xref py py-func docutils literal"><span class="pre">compress()</span></code></a><a class="reference internal" href="#gzip.decompress" title="gzip.decompress"><code class="xref py py-func docutils literal"><span class="pre"> decompress()</span></code></a>方便功能。</span><span class="yiyi-st" id="yiyi-15"><a class="reference internal" href="#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal"><span class="pre">GzipFile</span></code></a>类读取和写入<strong class="program">gzip</strong> -format文件自动压缩或解压缩数据使其看起来像一个普通的<a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">文件对象</span></a></span></p><p><span class="yiyi-st" id="yiyi-16">请注意,可以通过<strong class="program">gzip</strong><strong class="program">gunzip</strong>程序解压缩的其他文件格式,例如由<strong class="program">compress</strong><strong class="program">封装</strong>,该模块不支持。</span></p><p><span class="yiyi-st" id="yiyi-17">模块定义以下项目:</span></p><dl class="function"><dt id="gzip.open"><span class="yiyi-st" id="yiyi-18"> <code class="descclassname">gzip.</code><code class="descname">open</code><span class="sig-paren">(</span><em>filename</em>, <em>mode='rb'</em>, <em>compresslevel=9</em>, <em>encoding=None</em>, <em>errors=None</em>, <em>newline=None</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-19">在二进制或文本模式下打开gzip压缩文件返回<a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a></span></p><p><span class="yiyi-st" id="yiyi-20"><em>filename</em>参数可以是实际的文件名(<a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal"><span class="pre">str</span></code></a><a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a>对象),也可以是要读取或写入的现有文件对象。</span></p><p><span class="yiyi-st" id="yiyi-21">The <em>mode</em> argument can be any of <code class="docutils literal"><span class="pre">'r'</span></code>, <code class="docutils literal"><span class="pre">'rb'</span></code>, <code class="docutils literal"><span class="pre">'a'</span></code>, <code class="docutils literal"><span class="pre">'ab'</span></code>, <code class="docutils literal"><span class="pre">'w'</span></code>, <code class="docutils literal"><span class="pre">'wb'</span></code>, <code class="docutils literal"><span class="pre">'x'</span></code> or <code class="docutils literal"><span class="pre">'xb'</span></code> for binary mode, or <code class="docutils literal"><span class="pre">'rt'</span></code>, <code class="docutils literal"><span class="pre">'at'</span></code>, <code class="docutils literal"><span class="pre">'wt'</span></code>, or <code class="docutils literal"><span class="pre">'xt'</span></code> for text mode. </span><span class="yiyi-st" id="yiyi-22">默认值为<code class="docutils literal"><span class="pre">'rb'</span></code></span></p><p><span class="yiyi-st" id="yiyi-23"><em>compresslevel</em>参数是一个从0到9的整数对于<a class="reference internal" href="#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal"><span class="pre">GzipFile</span></code></a>构造函数。</span></p><p><span class="yiyi-st" id="yiyi-24">对于二进制模式,此函数等效于<a class="reference internal" href="#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal"><span class="pre">GzipFile</span></code></a>构造函数:<code class="docutils literal"><span class="pre">GzipFilefilename</span> <span class="pre">mode</span> <span class="pre">compresslevel t6&gt;</span></code></span><span class="yiyi-st" id="yiyi-25">在这种情况下,不能提供<em>编码</em><em>错误</em><em>新行</em>参数。</span></p><p><span class="yiyi-st" id="yiyi-26">对于文本模式,将创建一个<a class="reference internal" href="#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal"><span class="pre">GzipFile</span></code></a>对象,并将其包装在具有指定编码,错误处理行和行结尾的<a class="reference internal" href="io.html#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal"><span class="pre">io.TextIOWrapper</span></code></a>实例中。</span></p><div class="versionchanged"><p><span class="yiyi-st" id="yiyi-27"><span class="versionmodified">Changed in version 3.3: </span>Added support for <em>filename</em> being a file object, support for text mode, and the <em>encoding</em>, <em>errors</em> and <em>newline</em> arguments.</span></p></div><div class="versionchanged"><p><span class="yiyi-st" id="yiyi-28"><span class="versionmodified">在版本3.4中更改:</span>添加对<code class="docutils literal"><span class="pre">'x'</span></code><code class="docutils literal"><span class="pre">'xb'</span></code><code class="docutils literal"><span class="pre">'xt'</span></code>模式的支持。</span></p></div></dd></dl><dl class="class"><dt id="gzip.GzipFile"><span class="yiyi-st" id="yiyi-29"> <em class="property">class </em><code class="descclassname">gzip.</code><code class="descname">GzipFile</code><span class="sig-paren">(</span><em>filename=None</em>, <em>mode=None</em>, <em>compresslevel=9</em>, <em>fileobj=None</em>, <em>mtime=None</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-30"><a class="reference internal" href="#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal"><span class="pre">GzipFile</span></code></a>类的构造函数,它模拟<a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a>的大多数方法,但<code class="xref py py-meth docutils literal"><span class="pre">truncate()</span></code>方法除外。</span><span class="yiyi-st" id="yiyi-31">必须为<em>fileobj</em><em>filename</em>中的至少一个赋予一个不重要的值。</span></p><p><span class="yiyi-st" id="yiyi-32">新类实例基于<em>fileobj</em>,它可以是常规文件,<a class="reference internal" href="io.html#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal"><span class="pre">io.BytesIO</span></code></a>对象或任何其他模拟文件的对象。</span><span class="yiyi-st" id="yiyi-33">它默认为<code class="docutils literal"><span class="pre">None</span></code>,在这种情况下,打开<em>filename</em>以提供文件对象。</span></p><p><span class="yiyi-st" id="yiyi-34"><em>fileobj</em>不是<code class="docutils literal"><span class="pre">None</span></code>时,<em>文件名</em>参数仅用于包含在<strong class="program">gzip</strong>可以包括未压缩文件的原始文件名。</span><span class="yiyi-st" id="yiyi-35">如果可辨别,它默认为<em>fileobj</em>的文件名;否则,它默认为空字符串,在这种情况下,原始文件名不包括在标题中。</span></p><p><span class="yiyi-st" id="yiyi-36">The <em>mode</em> argument can be any of <code class="docutils literal"><span class="pre">'r'</span></code>, <code class="docutils literal"><span class="pre">'rb'</span></code>, <code class="docutils literal"><span class="pre">'a'</span></code>, <code class="docutils literal"><span class="pre">'ab'</span></code>, <code class="docutils literal"><span class="pre">'w'</span></code>, <code class="docutils literal"><span class="pre">'wb'</span></code>, <code class="docutils literal"><span class="pre">'x'</span></code>, or <code class="docutils literal"><span class="pre">'xb'</span></code>, depending on whether the file will be read or written. </span><span class="yiyi-st" id="yiyi-37">如果可辨别,默认为<em>fileobj</em>的模式;否则,默认值为<code class="docutils literal"><span class="pre">'rb'</span></code></span></p><p><span class="yiyi-st" id="yiyi-38">请注意,文件始终以二进制模式打开。</span><span class="yiyi-st" id="yiyi-39">要在文本模式下打开压缩文件,请使用<a class="reference internal" href="#gzip.open" title="gzip.open"><code class="xref py py-func docutils literal"><span class="pre">open()</span></code></a>(或用<a class="reference internal" href="io.html#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal"><span class="pre">io.TextIOWrapper</span></code></a>包装您的<a class="reference internal" href="#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal"><span class="pre">GzipFile</span></code></a>)。</span></p><p><span class="yiyi-st" id="yiyi-40"><em>compresslevel</em>参数是从<code class="docutils literal"><span class="pre">0</span></code><code class="docutils literal"><span class="pre">9</span></code>控制压缩级别的整数; <code class="docutils literal"><span class="pre">1</span></code>是最快的并且产生最小压缩,并且<code class="docutils literal"><span class="pre">9</span></code>最慢,并产生最大的压缩。</span><span class="yiyi-st" id="yiyi-41"><code class="docutils literal"><span class="pre">0</span></code>是无压缩。</span><span class="yiyi-st" id="yiyi-42">默认值为<code class="docutils literal"><span class="pre">9</span></code></span></p><p><span class="yiyi-st" id="yiyi-43"><em>mtime</em>参数是要在压缩时写入流中最后一个修改时间字段的可选数字时间戳。</span><span class="yiyi-st" id="yiyi-44">它应该只在压缩模式下提供。</span><span class="yiyi-st" id="yiyi-45">如果省略或<code class="docutils literal"><span class="pre">None</span></code>,则使用当前时间。</span><span class="yiyi-st" id="yiyi-46">有关更多详细信息,请参阅<a class="reference internal" href="#gzip.GzipFile.mtime" title="gzip.GzipFile.mtime"><code class="xref py py-attr docutils literal"><span class="pre">mtime</span></code></a>属性。</span></p><p><span class="yiyi-st" id="yiyi-47">调用<a class="reference internal" href="#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal"><span class="pre">GzipFile</span></code></a>对象的<code class="xref py py-meth docutils literal"><span class="pre">close()</span></code>方法不会关闭<em>fileobj</em>,因为您可能希望在压缩数据之后附加更多材料。</span><span class="yiyi-st" id="yiyi-48">这也允许您传递一个<a class="reference internal" href="io.html#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal"><span class="pre">io.BytesIO</span></code></a>对象,作为<em>fileobj</em>写入,并使用<a class="reference internal" href="io.html#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal"><span class="pre">io.BytesIO</span></code></a> <a class="reference internal" href="io.html#io.BytesIO.getvalue" title="io.BytesIO.getvalue"><code class="xref py py-meth docutils literal"><span class="pre">getvalue()</span></code></a>方法。</span></p><p><span class="yiyi-st" id="yiyi-49"><a class="reference internal" href="#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal"><span class="pre">GzipFile</span></code></a>支持<a class="reference internal" href="io.html#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal"><span class="pre">io.BufferedIOBase</span></code></a>界面,包括迭代和<a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal"><span class="pre">with</span></code></a>语句。</span><span class="yiyi-st" id="yiyi-50">只有<code class="xref py py-meth docutils literal"><span class="pre">truncate()</span></code>方法未实现。</span></p><p><span class="yiyi-st" id="yiyi-51"><a class="reference internal" href="#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal"><span class="pre">GzipFile</span></code></a>还提供以下方法和属性:</span></p><dl class="method"><dt id="gzip.GzipFile.peek"><span class="yiyi-st" id="yiyi-52"> <code class="descname">peek</code><span class="sig-paren">(</span><em>n</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-53">读取<em>n</em>未压缩的字节,而不提前文件位置。</span><span class="yiyi-st" id="yiyi-54">对压缩流最多只能进行一次读取以满足调用。</span><span class="yiyi-st" id="yiyi-55">返回的字节数可能多于或少于请求的字节数。</span></p><div class="admonition note"><p class="first admonition-title"><span class="yiyi-st" id="yiyi-56">注意</span></p><p class="last"><span class="yiyi-st" id="yiyi-57">调用<a class="reference internal" href="#gzip.GzipFile.peek" title="gzip.GzipFile.peek"><code class="xref py py-meth docutils literal"><span class="pre">peek()</span></code></a>不会更改<a class="reference internal" href="#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal"><span class="pre">GzipFile</span></code></a>的文件位置,它可能会更改底层文件对象的位置。</span><span class="yiyi-st" id="yiyi-58">如果<a class="reference internal" href="#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal"><span class="pre">GzipFile</span></code></a>是使用<em>fileobj</em>参数构建的)。</span></p></div><div class="versionadded"><p><span class="yiyi-st" id="yiyi-59"><span class="versionmodified">版本3.2中的新功能。</span></span></p></div></dd></dl><dl class="attribute"><dt id="gzip.GzipFile.mtime"><span class="yiyi-st" id="yiyi-60"> <code class="descname">mtime</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-61">当解压缩时,可以从该属性读取最近读取的报头中的最后修改时间字段的值作为整数。</span><span class="yiyi-st" id="yiyi-62">读取任何标头之前的初始值为<code class="docutils literal"><span class="pre">None</span></code></span></p><p><span class="yiyi-st" id="yiyi-63">所有<strong class="program">gzip</strong>压缩流都需要包含此时间戳字段。</span><span class="yiyi-st" id="yiyi-64">一些程序,如<strong class="program">gunzip</strong>,使用时间戳。</span><span class="yiyi-st" id="yiyi-65">该格式与<a class="reference internal" href="os.html#os.stat" title="os.stat"><code class="xref py py-func docutils literal"><span class="pre">os.stat()</span></code></a>返回的对象的<a class="reference internal" href="time.html#time.time" title="time.time"><code class="xref py py-func docutils literal"><span class="pre">time.time()</span></code></a><a class="reference internal" href="os.html#os.stat_result.st_mtime" title="os.stat_result.st_mtime"><code class="xref py py-attr docutils literal"><span class="pre">st_mtime</span></code></a>属性的返回值相同。 。</span></p></dd></dl><div class="versionchanged"><p><span class="yiyi-st" id="yiyi-66"><span class="versionmodified">在版本3.1中已更改:</span>添加了对<a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal"><span class="pre">with</span></code></a>语句的支持以及<em>mtime</em>构造函数参数和<a class="reference internal" href="#gzip.GzipFile.mtime" title="gzip.GzipFile.mtime"><code class="xref py py-attr docutils literal"><span class="pre">mtime</span></code></a>属性。</span></p></div><div class="versionchanged"><p><span class="yiyi-st" id="yiyi-67"><span class="versionmodified">在版本3.2中更改:</span>添加了对零填充和不可搜索文件的支持。</span></p></div><div class="versionchanged"><p><span class="yiyi-st" id="yiyi-68"><span class="versionmodified">在版本3.3中更改:</span>现在实现了<a class="reference internal" href="io.html#io.BufferedIOBase.read1" title="io.BufferedIOBase.read1"><code class="xref py py-meth docutils literal"><span class="pre">io.BufferedIOBase.read1()</span></code></a>方法。</span></p></div><div class="versionchanged"><p><span class="yiyi-st" id="yiyi-69"><span class="versionmodified">在版本3.4中更改:</span>添加了对<code class="docutils literal"><span class="pre">'x'</span></code><code class="docutils literal"><span class="pre">'xb'</span></code>模式的支持。</span></p></div><div class="versionchanged"><p><span class="yiyi-st" id="yiyi-70"><span class="versionmodified">在3.5版本中进行了更改:</span>添加了对任意<a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like objects</span></a>的编写支持。</span><span class="yiyi-st" id="yiyi-71"><a class="reference internal" href="io.html#io.BufferedIOBase.read" title="io.BufferedIOBase.read"><code class="xref py py-meth docutils literal"><span class="pre">read()</span></code></a>方法现在接受<code class="docutils literal"><span class="pre">None</span></code>的参数。</span></p></div></dd></dl><dl class="function"><dt id="gzip.compress"><span class="yiyi-st" id="yiyi-72"> <code class="descclassname">gzip.</code><code class="descname">compress</code><span class="sig-paren">(</span><em>data</em>, <em>compresslevel=9</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-73">压缩<em> data </em>,返回包含压缩数据的<a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes </span></code></a>对象。</span><span class="yiyi-st" id="yiyi-74"><em>compresslevel</em>的含义与上述<a class="reference internal" href="#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal"><span class="pre">GzipFile</span></code></a>构造函数中的含义相同。</span></p><div class="versionadded"><p><span class="yiyi-st" id="yiyi-75"><span class="versionmodified">版本3.2中的新功能。</span></span></p></div></dd></dl><dl class="function"><dt id="gzip.decompress"><span class="yiyi-st" id="yiyi-76"> <code class="descclassname">gzip.</code><code class="descname">decompress</code><span class="sig-paren">(</span><em>data</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-77">解压缩<em> data </em>,返回包含未压缩数据的<a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes </span></code></a>对象。</span></p><div class="versionadded"><p><span class="yiyi-st" id="yiyi-78"><span class="versionmodified">版本3.2中的新功能。</span></span></p></div></dd></dl><div class="section" id="examples-of-usage"><h2><span class="yiyi-st" id="yiyi-79">13.2.1. </span><span class="yiyi-st" id="yiyi-80">用法示例</span></h2><p><span class="yiyi-st" id="yiyi-81">如何读取压缩文件的示例:</span></p><pre><code class="language-python"><span></span><span class="kn">import</span> <span class="nn">gzip</span>
<span class="k">with</span> <span class="n">gzip</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">'/home/joe/file.txt.gz'</span><span class="p">,</span> <span class="s1">'rb'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="n">file_content</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
</code></pre><p><span class="yiyi-st" id="yiyi-82">如何创建压缩的GZIP文件的示例</span></p><pre><code class="language-python"><span></span><span class="kn">import</span> <span class="nn">gzip</span>
<span class="n">content</span> <span class="o">=</span> <span class="n">b</span><span class="s2">"Lots of content here"</span>
<span class="k">with</span> <span class="n">gzip</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">'/home/joe/file.txt.gz'</span><span class="p">,</span> <span class="s1">'wb'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">content</span><span class="p">)</span>
</code></pre><p><span class="yiyi-st" id="yiyi-83">如何GZIP压缩现有文件的示例</span></p><pre><code class="language-python"><span></span><span class="kn">import</span> <span class="nn">gzip</span>
<span class="kn">import</span> <span class="nn">shutil</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'/home/joe/file.txt'</span><span class="p">,</span> <span class="s1">'rb'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f_in</span><span class="p">:</span>
<span class="k">with</span> <span class="n">gzip</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">'/home/joe/file.txt.gz'</span><span class="p">,</span> <span class="s1">'wb'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f_out</span><span class="p">:</span>
<span class="n">shutil</span><span class="o">.</span><span class="n">copyfileobj</span><span class="p">(</span><span class="n">f_in</span><span class="p">,</span> <span class="n">f_out</span><span class="p">)</span>
</code></pre><p><span class="yiyi-st" id="yiyi-84">示例如何GZIP压缩二进制字符串</span></p><pre><code class="language-python"><span></span><span class="kn">import</span> <span class="nn">gzip</span>
<span class="n">s_in</span> <span class="o">=</span> <span class="n">b</span><span class="s2">"Lots of content here"</span>
<span class="n">s_out</span> <span class="o">=</span> <span class="n">gzip</span><span class="o">.</span><span class="n">compress</span><span class="p">(</span><span class="n">s_in</span><span class="p">)</span>
</code></pre><div class="admonition seealso"><p class="first admonition-title"><span class="yiyi-st" id="yiyi-85">也可以看看</span></p><dl class="last docutils"><dt><span class="yiyi-st" id="yiyi-86">模块<a class="reference internal" href="zlib.html#module-zlib" title="zlib: Low-level interface to compression and decompression routines compatible with gzip."><code class="xref py py-mod docutils literal"><span class="pre">zlib</span></code></a></span></dt><dd><span class="yiyi-st" id="yiyi-87">基本数据压缩模块需要支持<strong class="program">gzip</strong>文件格式。</span></dd></dl></div></div></div></div>