關於使用 deflate 函數分塊寫入 gzip 編碼檔案的範例。
<?php
$handler = fopen('/tmp/test.csv', 'w');
$deflateContext = deflate_init(ZLIB_ENCODING_GZIP, ['level' => 9]);
$strings = [
'Hello, how are you?' . PHP_EOL,
'I am fine thanks' . PHP_EOL,
'Hello, how are you?' . PHP_EOL,
];
foreach ($strings as $string) {
fwrite($handler, deflate_add($deflateContext, $string, ZLIB_NO_FLUSH));
}
fwrite($handler, deflate_add($deflateContext, '', ZLIB_FINISH));
fclose($handler);
echo gzdecode(file_get_contents('/tmp/test.csv'));