Skip to content

Core\Compress::compressor

Starts a compression you feed in pieces, instead of all at once.

You get back a Core\Compress\Compressor. Call add with each piece of data, as many times as you need, and then call finish once. finish returns the compressed bytes of everything you added, in one block. It is exactly what Core\Compress::compress returns for the same pieces joined together, so how you split them up never changes the result.

You choose the format when you open the stream, and it stays that format until the end.

Good to know: a stream ends once. After finish, both add and finish throw a RuntimeError, and you open a new stream for the next frame. Reach for this when your data arrives in pieces; Core\Compress::compress is the simpler call when you already hold all of it.

Parameters

  • $codecCore\Compress::CodecThe format to write, any Core\Compress::Codec case. It cannot change while the stream is open, because one compressed result has one format.

Return value

Returns Core\Compress\Compressor. A new, open stream with no data in it yet.

Examples

Compress in pieces

<?nvs
// Shows how to compress data that arrives in pieces. You add each piece, then
// call `finish` once to get the compressed bytes.
var $stream = Core\Compress::compressor(Core\Compress::Codec::Gzip);
$stream->add("first line of the report\n");
$stream->add("second line of the report\n");
$stream->add("third line of the report\n");
bytes $frame = $stream->finish();
echo 'compressed to ', Core\Bytes::length($frame), " bytes\n";
// Reading it back gives the three pieces joined together.
bytes $back = Core\Compress::decompress($frame, Core\Compress::Codec::Gzip);
echo Core\Bytes::length($back), " bytes came back\n";
Output
compressed to 61 bytes
76 bytes came back

The pieces do not change the result

<?nvs
// Shows that how you split the data up does not change what you get. A stream
// fed in three pieces writes the same bytes as one whole-buffer call.
string $text = Core\Str::repeat("every order is one line\n", 30);
var $stream = Core\Compress::compressor(Core\Compress::Codec::Zlib);
$stream->add(Core\Str::slice($text, 0, 100));
$stream->add(Core\Str::slice($text, 100, 260));
$stream->add(Core\Str::slice($text, 360, 360));
bytes $inPieces = $stream->finish();
bytes $atOnce = Core\Compress::compress($text, Core\Compress::Codec::Zlib);
echo 'in pieces: ', Core\Bytes::length($inPieces), " bytes\n";
echo 'at once: ', Core\Bytes::length($atOnce), " bytes\n";
echo 'the same bytes: ', Core\Hash::equals($inPieces, $atOnce) ? 'yes' : 'no', "\n";
Output
in pieces: 46 bytes
at once: 46 bytes
the same bytes: yes

Compress rows as you read them

<?nvs
// Shows what a program does when it writes a compressed export: it adds each
// row as it reads it, and finishes once at the end.
array<string> $orders = [
'id,city,total',
'1042,Berlin,39.90',
'1043,Lisbon,12.50',
'1044,Oslo,88.00',
];
var $export = Core\Compress::compressor(Core\Compress::Codec::Gzip);
foreach ($orders as string $row) {
$export->add($row);
$export->add("\n");
}
bytes $file = $export->finish();
echo 'the export is ', Core\Bytes::length($file), " bytes\n";
// A stream ends once. Adding another row after `finish` throws a `RuntimeError`,
// so open a new stream for the next file.
try {
$export->add('1045,Madrid,5.00');
echo "the finished stream took another row\n";
} catch (RuntimeError $finished) {
echo "the finished stream took no more rows\n";
}
Output
the export is 89 bytes
the finished stream took no more rows