Skip to content

Core\Compress\Compressor::add

$compressor->add(bytes|string $data): void$compressor->add(
    bytes|string $data,
): void

Adds one piece of data to a compression stream you opened with Core\Compress::compressor.

You can call add as many times as you need, and each piece is a string or bytes. Nothing is compressed yet. The stream keeps the pieces in the order you added them, and finish compresses all of them together into one block. The result is the same however you split your data up.

add keeps the piece you gave it. It does not copy the data, so adding the same large piece a thousand times uses the memory of one piece.

Good to know: a stream ends once. After finish, add throws a RuntimeError, and you open a new stream for the next block of data.

Parameters

  • $databytes|stringThe next bytes. A string is read as its UTF-8 bytes.

Return value

Returns void. Nothing. finish returns all the compressed data at once.

Errors

  • RuntimeErrorThe stream is already finished. Its result is complete, so start a new stream.

Examples

Add the pieces of a text

<?nvs
// Shows how to build one compressed block from several pieces. `add` keeps the
// pieces in the order you add them, and `finish` compresses all of them.
var $letter = Core\Compress::compressor(Core\Compress::Codec::Gzip);
$letter->add('Dear customer,');
$letter->add(' your order 1042 is on its way.');
$letter->add(' Thank you!');
bytes $frame = $letter->finish();
// The three pieces are 56 bytes together, so this prints 56.
bytes $back = Core\Compress::decompress($frame, Core\Compress::Codec::Gzip);
echo 'the three pieces came back as ', Core\Bytes::length($back), " bytes\n";
// The same text added as one piece gives the same bytes. `finish` compresses
// what you added, and not how you split it.
var $whole = Core\Compress::compressor(Core\Compress::Codec::Gzip);
$whole->add('Dear customer, your order 1042 is on its way. Thank you!');
echo 'the same block: ', Core\Hash::equals($whole->finish(), $frame) ? 'yes' : 'no', "\n";
Output
the three pieces came back as 56 bytes
the same block: yes

Add text and bytes to one stream

<?nvs
// Shows that one stream takes both kinds of data. `add` accepts a `string`,
// and it accepts `bytes`, in whatever order you need them.
// 16 bytes that are not text, standing in for a small image.
bytes $image = Core\Bytes::fill(16, 137);
var $page = Core\Compress::compressor(Core\Compress::Codec::Zlib);
$page->add('<html><body>');
$page->add($image);
$page->add('</body></html>');
bytes $frame = $page->finish();
// The stream was given 42 bytes in total: 12 of text, then 16, then 14.
echo 'the page is ', Core\Bytes::length($frame), " bytes\n";
echo 'it reads back as ', Core\Bytes::length(Core\Compress::decompress($frame, Core\Compress::Codec::Zlib)), " bytes\n";
Output
the page is 30 bytes
it reads back as 42 bytes

Compress a report while you write it

<?nvs
// Shows how a program compresses a report while it writes it. Each line is
// added as soon as it is made, so the whole report is never one string.
var $archive = Core\Compress::compressor(Core\Compress::Codec::Zstd);
int $written = 0;
for (int $day = 1; $day <= 365; $day++) {
string $line = "day {$day}: 120 orders, 3 returns\n";
$archive->add($line);
$written = $written + (Core\Bytes::length($line as bytes) as int);
}
bytes $frame = $archive->finish();
echo 'the report is ', $written, " bytes\n";
echo 'the archive is ', Core\Bytes::length($frame), " bytes\n";
// A file that already exists does not need a compressor. Pass the open file
// to `Core\Compress::compress`, and it reads the file one block at a time.
Output
the report is 11207 bytes
the archive is 1165 bytes