<?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 "原始文字長度為 " . strlen($original_text) . " 個字元。\n";
$fp = fopen('test.deflated', 'w');
stream_filter_append($fp, 'zlib.deflate', STREAM_FILTER_WRITE, $params);
fwrite($fp, $original_text);
fclose($fp);
echo "壓縮後的檔案大小為 " . filesize('test.deflated') . " 位元組。\n";
echo "原始文字為:\n";
/* 使用 readfile 和 zlib.inflate 即時解壓縮 */
readfile('php://filter/zlib.inflate/resource=test.deflated');
/* 產生輸出:
原始文字長度為 70 個字元。
壓縮後的檔案大小為 56 位元組。
原始文字為:
This is a test.
This is only a test.
This is not an important string.
*/
?>