zlib.html
来自「perl教程」· HTML 代码 · 共 956 行 · 第 1/4 页
HTML
956 行
<span class="keyword">use</span> <span class="variable">Compress::Zlib</span> <span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">$x</span> <span class="operator">=</span> <span class="variable">inflateInit</span><span class="operator">()</span>
<span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"Cannot create a inflation stream\n"</span> <span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">$input</span> <span class="operator">=</span> <span class="string">''</span> <span class="operator">;</span>
<span class="keyword">binmode</span> <span class="variable">STDIN</span><span class="operator">;</span>
<span class="keyword">binmode</span> <span class="variable">STDOUT</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="operator">(</span><span class="variable">$output</span><span class="operator">,</span> <span class="variable">$status</span><span class="operator">)</span> <span class="operator">;</span>
<span class="keyword">while</span> <span class="operator">(</span><span class="keyword">read</span><span class="operator">(</span><span class="variable">STDIN</span><span class="operator">,</span> <span class="variable">$input</span><span class="operator">,</span> <span class="number">4096</span><span class="operator">))</span>
<span class="operator">{</span>
<span class="operator">(</span><span class="variable">$output</span><span class="operator">,</span> <span class="variable">$status</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">$x</span><span class="operator">-></span><span class="variable">inflate</span><span class="operator">(\</span><span class="variable">$input</span><span class="operator">)</span> <span class="operator">;</span>
<span class="keyword">print</span> <span class="variable">$output</span>
<span class="keyword">if</span> <span class="variable">$status</span> <span class="operator">==</span> <span class="variable">Z_OK</span> <span class="keyword">or</span> <span class="variable">$status</span> <span class="operator">==</span> <span class="variable">Z_STREAM_END</span> <span class="operator">;</span>
<span class="keyword">last</span> <span class="keyword">if</span> <span class="variable">$status</span> <span class="operator">!=</span> <span class="variable">Z_OK</span> <span class="operator">;</span>
<span class="operator">}</span>
<span class="keyword">die</span> <span class="string">"inflation failed\n"</span>
<span class="keyword">unless</span> <span class="variable">$status</span> <span class="operator">==</span> <span class="variable">Z_STREAM_END</span> <span class="operator">;</span>
</pre>
<p>
</p>
<hr />
<h1><a name="compress_uncompress">COMPRESS/UNCOMPRESS</a></h1>
<p>Two high-level functions are provided by <em>zlib</em> to perform in-memory
compression/uncompression of RFC1950 data streams. They are called
<strong>compress</strong> and <strong>uncompress</strong>.</p>
<p>The two Perl subs defined below provide the equivalent
functionality.</p>
<dl>
<dt><strong><a name="item_compress"><strong>$dest = compress($source [, $level] ) ;</strong></a></strong>
<dd>
<p>Compresses <strong>$source</strong>. If successful it returns the
compressed data. Otherwise it returns <em>undef</em>.</p>
</dd>
<dd>
<p>The source buffer can either be a scalar or a scalar reference.</p>
</dd>
<dd>
<p>The <strong>$level</strong> paramter defines the compression level. Valid values are
0 through 9, <code>Z_NO_COMPRESSION</code>, <code>Z_BEST_SPEED</code>,
<code>Z_BEST_COMPRESSION</code>, and <code>Z_DEFAULT_COMPRESSION</code>.
If <strong>$level</strong> is not specified <code>Z_DEFAULT_COMPRESSION</code> will be used.</p>
</dd>
</li>
<dt><strong><a name="item_uncompress"><strong>$dest = uncompress($source) ;</strong></a></strong>
<dd>
<p>Uncompresses <strong>$source</strong>. If successful it returns the uncompressed
data. Otherwise it returns <em>undef</em>.</p>
</dd>
<dd>
<p>The source buffer can either be a scalar or a scalar reference.</p>
</dd>
</li>
</dl>
<p>Please note: the two functions defined above are <em>not</em> compatible with
the Unix commands of the same name.</p>
<p>
</p>
<hr />
<h1><a name="gzip_interface">GZIP INTERFACE</a></h1>
<p>A number of functions are supplied in <em>zlib</em> for reading and writing
<em>gzip</em> files. This module provides an interface to most of them. In
general the interface provided by this module operates identically to
the functions provided by <em>zlib</em>. Any differences are explained
below.</p>
<dl>
<dt><strong><a name="item_gzopen"><strong>$gz = gzopen(filename or filehandle, mode)</strong></a></strong>
<dd>
<p>This function operates identically to the <em>zlib</em> equivalent except
that it returns an object which is used to access the other <em>gzip</em>
methods.</p>
</dd>
<dd>
<p>As with the <em>zlib</em> equivalent, the <strong>mode</strong> parameter is used to
specify both whether the file is opened for reading or writing and to
optionally specify a a compression level. Refer to the <em>zlib</em>
documentation for the exact format of the <strong>mode</strong> parameter.</p>
</dd>
<dd>
<p>If a reference to an open filehandle is passed in place of the
filename, gzdopen will be called behind the scenes. The third example
at the end of this section, <em>gzstream</em>, uses this feature.</p>
</dd>
</li>
<dt><strong><a name="item_gzread"><strong>$bytesread = $gz->gzread($buffer [, $size]) ;</strong></a></strong>
<dd>
<p>Reads <strong>$size</strong> bytes from the compressed file into <strong>$buffer</strong>. If
<strong>$size</strong> is not specified, it will default to 4096. If the scalar
<strong>$buffer</strong> is not large enough, it will be extended automatically.</p>
</dd>
<dd>
<p>Returns the number of bytes actually read. On EOF it returns 0 and in
the case of an error, -1.</p>
</dd>
</li>
<dt><strong><a name="item_gzreadline"><strong>$bytesread = $gz->gzreadline($line) ;</strong></a></strong>
<dd>
<p>Reads the next line from the compressed file into <strong>$line</strong>.</p>
</dd>
<dd>
<p>Returns the number of bytes actually read. On EOF it returns 0 and in
the case of an error, -1.</p>
</dd>
<dd>
<p>It is legal to intermix calls to <strong>gzread</strong> and <strong>gzreadline</strong>.</p>
</dd>
<dd>
<p>At this time <strong>gzreadline</strong> ignores the variable <a href="../../lib/Pod/perlvar.html#item___"><code>$/</code></a>
(<a href="../../lib/Pod/perlvar.html#item__input_record_separator"><code>$INPUT_RECORD_SEPARATOR</code></a> or <a href="../../lib/Pod/perlvar.html#item__rs"><code>$RS</code></a> when <code>English</code> is in use). The
end of a line is denoted by the C character <code>'\n'</code>.</p>
</dd>
</li>
<dt><strong><a name="item_gzwrite"><strong>$byteswritten = $gz->gzwrite($buffer) ;</strong></a></strong>
<dd>
<p>Writes the contents of <strong>$buffer</strong> to the compressed file. Returns the
number of bytes actually written, or 0 on error.</p>
</dd>
</li>
<dt><strong><a name="item_gzflush"><strong>$status = $gz->gzflush($flush) ;</strong></a></strong>
<dd>
<p>Flushes all pending output to the compressed file.
Works identically to the <em>zlib</em> function it interfaces to. Note that
the use of <strong>gzflush</strong> can degrade compression.</p>
</dd>
<dd>
<p>Returns <code>Z_OK</code> if <strong>$flush</strong> is <code>Z_FINISH</code> and all output could be
flushed. Otherwise the zlib error code is returned.</p>
</dd>
<dd>
<p>Refer to the <em>zlib</em> documentation for the valid values of <strong>$flush</strong>.</p>
</dd>
</li>
<dt><strong><a name="item_gzeof"><strong>$status = $gz->gzeof() ;</strong></a></strong>
<dd>
<p>Returns 1 if the end of file has been detected while reading the input
file, otherwise returns 0.</p>
</dd>
</li>
<dt><strong><a name="item_gzclose"><strong>$gz->gzclose</strong></a></strong>
<dd>
<p>Closes the compressed file. Any pending data is flushed to the file
before it is closed.</p>
</dd>
</li>
<dt><strong><a name="item_gzsetparams"><strong>$gz->gzsetparams($level, $strategy</strong></a></strong>
<dd>
<p>Change settings for the deflate stream <code>$gz</code>.</p>
</dd>
<dd>
<p>The list of the valid options is shown below. Options not specified
will remain unchanged.</p>
</dd>
<dd>
<p>Note: This method is only available if you are running zlib 1.0.6 or better.</p>
</dd>
<dl>
<dt><strong><a name="item__level"><strong>$level</strong></a></strong>
<dd>
<p>Defines the compression level. Valid values are 0 through 9,
<code>Z_NO_COMPRESSION</code>, <code>Z_BEST_SPEED</code>, <code>Z_BEST_COMPRESSION</code>, and
<code>Z_DEFAULT_COMPRESSION</code>.</p>
</dd>
</li>
<dt><strong><a name="item__strategy"><strong>$strategy</strong></a></strong>
<dd>
<p>Defines the strategy used to tune the compression. The valid values are
<code>Z_DEFAULT_STRATEGY</code>, <code>Z_FILTERED</code> and <code>Z_HUFFMAN_ONLY</code>.</p>
</dd>
</li>
</dl>
<dt><strong><a name="item_gzerror"><strong>$gz->gzerror</strong></a></strong>
<dd>
<p>Returns the <em>zlib</em> error message or number for the last operation
associated with <strong>$gz</strong>. The return value will be the <em>zlib</em> error
number when used in a numeric context and the <em>zlib</em> error message
when used in a string context. The <em>zlib</em> error number constants,
shown below, are available for use.</p>
</dd>
<dd>
<pre>
Z_OK
Z_STREAM_END
Z_ERRNO
Z_STREAM_ERROR
Z_DATA_ERROR
Z_MEM_ERROR
Z_BUF_ERROR</pre>
</dd>
</li>
<dt><strong><a name="item__gzerrno"><strong>$gzerrno</strong></a></strong>
<dd>
<p>The <strong>$gzerrno</strong> scalar holds the error code associated with the most
recent <em>gzip</em> routine. Note that unlike <strong>gzerror()</strong>, the error is
<em>not</em> associated with a particular file.</p>
</dd>
<dd>
<p>As with <strong>gzerror()</strong> it returns an error number in numeric context and
an error message in string context. Unlike <strong>gzerror()</strong> though, the
error message will correspond to the <em>zlib</em> message when the error is
associated with <em>zlib</em> itself, or the UNIX error message when it is
not (i.e. <em>zlib</em> returned <code>Z_ERRORNO</code>).</p>
</dd>
<dd>
<p>As there is an overlap between the error numbers used by <em>zlib</em> and
UNIX, <strong>$gzerrno</strong> should only be used to check for the presence of
<em>an</em> error in numeric context. Use <strong>gzerror()</strong> to check for specific
<em>zlib</em> errors. The <em>gzcat</em> example below shows how the variable can
be used safely.</p>
</dd>
</li>
</dl>
<p>
</p>
<h2><a name="examples">Examples</a></h2>
<p>Here is an example script which uses the interface. It implements a
<em>gzcat</em> function.</p>
<pre>
<span class="keyword">use</span> <span class="variable">strict</span> <span class="operator">;</span>
<span class="keyword">use</span> <span class="variable">warnings</span> <span class="operator">;</span>
<span class="keyword">use</span> <span class="variable">Compress::Zlib</span> <span class="operator">;</span>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?