1
0
mirror of https://github.com/danog/byte-stream.git synced 2024-12-02 17:28:21 +01:00
byte-stream/docs/compression-streams.md

33 lines
1.1 KiB
Markdown
Raw Normal View History

2017-05-28 19:24:13 +02:00
---
title: Compression Streams
permalink: /compression-streams
---
2017-05-25 17:51:25 +02:00
This package implements compression and decompression streams based on Zlib. `ZlibOutputStream` can be used for compression, while `ZlibInputStream` can be used for decompression. Both can simply wrap an existing stream to apply them. Both accept an `$encoding` and `$options` parameter in their constructor.
## Examples
```php
$inputStream = new ResourceInputStream(STDIN);
$gzInputStream = new ZlibInputStream($inputStream, \ZLIB_ENCODING_GZIP);
while (null !== $chunk = yield $gzInputStream) {
print $chunk;
}
```
```php
$outputStream = new ResourceOutputStream(STDOUT);
$gzOutputStream = new ZlibOutputStream($outputStream, \ZLIB_ENCODING_GZIP);
for ($i = 0; $i < 100; $i++) {
yield $gzOutputStream->write(bin2hex(random_bytes(32));
}
yield $gzOutputStream->end();
```
## See also
* [`./examples/gzip-compress.php`](https://github.com/amphp/byte-stream/blob/master/examples/gzip-compress.php)
2017-06-12 23:04:28 +02:00
* [`./examples/gzip-decompress.php`](https://github.com/amphp/byte-stream/blob/master/examples/gzip-decompress.php)