壓縮過濾器
雖然 壓縮封裝協議 提供了在本地檔案系統中 建立 gzip 和 bz2 相容檔案的方法,但不代表可以在網路的流中提供通用壓縮的意思, 也不代表可以將一個非壓縮的流轉換成一個壓縮流。對此,壓縮過濾器可以在任何時候應用於任何流資源。
注意: 壓縮過濾器不產生命令列工具如
gzip
的頭和尾資訊。只是壓縮和解壓數據流中的有效載荷部分。
zlib.deflate 和 zlib.inflate
zlib.deflate
(壓縮)和
zlib.inflate
(解壓)實現了定義與
» RFC 1951 的壓縮演算法。
deflate
過濾器可以接受以一個關聯陣列傳遞的最多三個參數。
level
定義了壓縮強度(1-9)。
數字更高通常會產生更小的載荷,但要消耗更多的處理時間。
存在兩個特殊壓縮等級:0(完全不壓縮)和 -1(zlib 內部預設值,目前是 6)。
window
是壓縮回溯視窗大小,以二的次方表示。
更高的值(大到 15 —— 32768 位元組)產生更好的壓縮效果但消耗更多記憶體,
低的值(低到 9 —— 512 位元組)產生產生較差的壓縮效果但記憶體消耗低。
目前預設的 window
大小是 15
。
memory
用來指示要分配多少工作記憶體。
合法的數值範圍是從 1(最小分配)到 9(最大分配)。
記憶體分配僅影響速度,不會影響產生的載荷的大小。
注意: 因為最常用的參數是壓縮等級,也可以提供一個整數值作為此參數(而不用陣列)。
在啟用 zlib 的前提下可以使用 zlib.* 壓縮過濾器。
示例 #1
zlib.deflate
和
zlib.inflate
<?php
$params = array('level' => 6, 'window' => 15, 'memory' => 9);
$original_text = "This is a test.\nThis is only a test.\nThis is not an important string.\n";
echo "The original text is " . strlen($original_text) . " characters long.\n";
$fp = fopen('test.deflated', 'w');
stream_filter_append($fp, 'zlib.deflate', STREAM_FILTER_WRITE, $params);
fwrite($fp, $original_text);
fclose($fp);
echo "The compressed file is " . filesize('test.deflated') . " bytes long.\n";
echo "The original text was:\n";
/* 使用 readfile 和 zlib.inflate 即時解壓縮 */
readfile('php://filter/zlib.inflate/resource=test.deflated');
/* 產生的輸出:
The original text is 70 characters long.
The compressed file is 56 bytes long.
The original text was:
This is a test.
This is only a test.
This is not an important string.
*/
?>
示例 #2
zlib.deflate
簡單參數用法
<?php
$original_text = "This is a test.\nThis is only a test.\nThis is not an important string.\n";
echo "The original text is " . strlen($original_text) . " characters long.\n";
$fp = fopen('test.deflated', 'w');
/* 這裡的 「6」 代表壓縮級別為 6 */
stream_filter_append($fp, 'zlib.deflate', STREAM_FILTER_WRITE, 6);
fwrite($fp, $original_text);
fclose($fp);
echo "The compressed file is " . filesize('test.deflated') . " bytes long.\n";
/* 產生的輸出:
The original text is 70 characters long.
The compressed file is 56 bytes long.
*/
?>
bzip2.compress 和 bzip2.decompress
bzip2.compress
和
bzip2.decompress
工作的方式與上面講的 zlib 過濾器相同。
bzip2.compress
過濾器接受以一個關聯陣列給出的最多兩個參數:
blocks
是從 1 到 9 的整數值,指定分配多少個 100K 位元組的記憶體塊作為工作區。
work
是 0 到 250 的整數值,指定在退回到一個慢一些,
但更可靠的演算法之前做多少次常規壓縮演算法的嘗試。調整此參數僅影響到速度,
壓縮輸出和記憶體使用都不受此設定的影響。將此參數設為 0 指示 bzip 庫使用內部預設演算法。
bzip2.decompress
過濾器僅接受一個參數,可以用普通的布爾值傳遞,
或者用一個關聯陣列中的 small
單元傳遞。
當 small
設為 &true; 值時,指示 bzip 庫用最小的記憶體佔用來執行解壓縮,
代價是速度會慢一些。
在啟用 bz2 支援的前提下可以使用 bzip2.* 壓縮過濾器。
示例 #3
bzip2.compress
和
bzip2.decompress
<?php
$param = array('blocks' => 9, 'work' => 0);
echo "The original file is " . filesize('LICENSE') . " bytes long.\n";
$fp = fopen('LICENSE.compressed', 'w');
stream_filter_append($fp, 'bzip2.compress', STREAM_FILTER_WRITE, $param);
fwrite($fp, file_get_contents('LICENSE'));
fclose($fp);
echo "The compressed file is " . filesize('LICENSE.compressed') . " bytes long.\n";
/* 產生的輸出:
The original text is 3288 characters long.
The compressed file is 1488 bytes long.
*/
?>