Example about to use deflate functions to write a gzip encoded file in chunks.<?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'));