📄 filters.compression.html
字号:
<span style="color: #0000BB"><?php<br />$params </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'level' </span><span style="color: #007700">=> </span><span style="color: #0000BB">6</span><span style="color: #007700">, </span><span style="color: #DD0000">'window' </span><span style="color: #007700">=> </span><span style="color: #0000BB">15</span><span style="color: #007700">, </span><span style="color: #DD0000">'memory' </span><span style="color: #007700">=> </span><span style="color: #0000BB">9</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$original_text </span><span style="color: #007700">= </span><span style="color: #DD0000">"This is a test.\nThis is only a test.\nThis is not an important string.\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"The original text is " </span><span style="color: #007700">. </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$original_text</span><span style="color: #007700">) . </span><span style="color: #DD0000">" characters long.\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'test.deflated'</span><span style="color: #007700">, </span><span style="color: #DD0000">'w'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">stream_filter_append</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #DD0000">'zlib.deflate'</span><span style="color: #007700">, </span><span style="color: #0000BB">STREAM_FILTER_WRITE</span><span style="color: #007700">, </span><span style="color: #0000BB">$params</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">$original_text</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"The compressed file is " </span><span style="color: #007700">. </span><span style="color: #0000BB">filesize</span><span style="color: #007700">(</span><span style="color: #DD0000">'test.deflated'</span><span style="color: #007700">) . </span><span style="color: #DD0000">" bytes long.\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"The original text was:\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">/* Use readfile and zlib.inflate to decompress on the fly */<br /></span><span style="color: #0000BB">readfile</span><span style="color: #007700">(</span><span style="color: #DD0000">'php://filter/zlib.inflate/resource=test.deflated'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Generates output:<br /><br />The original text is 70 characters long.<br />The compressed file is 56 bytes long.<br />The original text was:<br />This is a test.<br />This is only a test.<br />This is not an important string.<br /><br /> */<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <div class="example"> <p><b>Example #2 <i>zlib.deflate</i> simple </b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$original_text </span><span style="color: #007700">= </span><span style="color: #DD0000">"This is a test.\nThis is only a test.\nThis is not an important string.\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"The original text is " </span><span style="color: #007700">. </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$original_text</span><span style="color: #007700">) . </span><span style="color: #DD0000">" characters long.\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'test.deflated'</span><span style="color: #007700">, </span><span style="color: #DD0000">'w'</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">/* Here "6" indicates compression level 6 */<br /></span><span style="color: #0000BB">stream_filter_append</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #DD0000">'zlib.deflate'</span><span style="color: #007700">, </span><span style="color: #0000BB">STREAM_FILTER_WRITE</span><span style="color: #007700">, </span><span style="color: #0000BB">6</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">$original_text</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"The compressed file is " </span><span style="color: #007700">. </span><span style="color: #0000BB">filesize</span><span style="color: #007700">(</span><span style="color: #DD0000">'test.deflated'</span><span style="color: #007700">) . </span><span style="color: #DD0000">" bytes long.\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/* Generates output:<br /><br />The original text is 70 characters long.<br />The compressed file is 56 bytes long.<br /><br /> */<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <p class="simpara"> <i>bzip2.compress</i> and <i>bzip2.decompress</i> work in the same manner as the zlib filters described above. The <i>bzip2.compress</i> filter accepts up to two parameters given as elements of an associative array: <i><tt class="parameter">blocks</tt></i> is an integer value from 1 to 9 specifying the number of 100kbyte blocks of memory to allocate for workspace. <i><tt class="parameter">work</tt></i> is also an integer value ranging from 0 to 250 indicating how much effort to expend using the normal compression method before falling back on a slower, but more reliable method. Tuning this parameter effects only compression speed. Neither size of compressed output nor memory usage are changed by this setting. A work factor of 0 instructs the bzip library to use an internal default. The <i>bzip2.decompress</i> filter only accepts one parameter, which can be passed as either an ordinary boolean value, or as the <i><tt class="parameter">small</tt></i> element of an associative array. <i><tt class="parameter">small</tt></i>, when set to a <b><tt>TRUE</tt></b> value, instructs the bzip library to perform decompression in a minimal memory footprint at the cost of speed. </p> <p class="simpara"> bzip2.* compression filters are available with PHP as of version <i>5.1.0</i> if <a href="ref.bzip2.html" class="link">bz2</a> support is enabled. They are also available as a backport in version <i>5.0.x</i> by installing the <a href="http://pecl.php.net/package/bz2_filter" class="link external">» bz2_filter</a> package from <a href="http://pecl.php.net/" class="link external">» PECL</a>. These filters are <em class="emphasis">not</em> available for PHP 4. </p> <div class="example"> <p><b>Example #3 <i>bzip2.compress</i> and <i>bzip2.decompress</i> </b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$param </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'blocks' </span><span style="color: #007700">=> </span><span style="color: #0000BB">9</span><span style="color: #007700">, </span><span style="color: #DD0000">'work' </span><span style="color: #007700">=> </span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"The original file is " </span><span style="color: #007700">. </span><span style="color: #0000BB">filesize</span><span style="color: #007700">(</span><span style="color: #DD0000">'LICENSE'</span><span style="color: #007700">) . </span><span style="color: #DD0000">" bytes long.\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'LICENSE.compressed'</span><span style="color: #007700">, </span><span style="color: #DD0000">'w'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">stream_filter_append</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #DD0000">'bzip2.compress'</span><span style="color: #007700">, </span><span style="color: #0000BB">STREAM_FILTER_WRITE</span><span style="color: #007700">, </span><span style="color: #0000BB">$param</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'LICENSE'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"The compressed file is " </span><span style="color: #007700">. </span><span style="color: #0000BB">filesize</span><span style="color: #007700">(</span><span style="color: #DD0000">'LICENSE.compressed'</span><span style="color: #007700">) . </span><span style="color: #DD0000">" bytes long.\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/* Generates output:<br /><br />The original text is 3288 characters long.<br />The compressed file is 1488 bytes long.<br /><br /> */<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="filters.convert.html">Conversion Filters</a></div> <div class="next" style="text-align: right; float: right;"><a href="filters.encryption.html">Encryption Filters</a></div> <div class="up"><a href="filters.html">List of Available Filters</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -