https://github.com/xp-forge/compression
Compression streams
https://github.com/xp-forge/compression
brotli bzip2 compression gzip php7 php8 xp-framework
Last synced: 10 months ago
JSON representation
Compression streams
- Host: GitHub
- URL: https://github.com/xp-forge/compression
- Owner: xp-forge
- Created: 2022-02-19T11:31:36.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-05-30T10:17:10.000Z (almost 2 years ago)
- Last Synced: 2025-04-14T13:57:38.966Z (about 1 year ago)
- Topics: brotli, bzip2, compression, gzip, php7, php8, xp-framework
- Language: PHP
- Homepage:
- Size: 55.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: ChangeLog.md
Awesome Lists containing this project
README
Compression streams
===================
[](https://github.com/xp-forge/compression/actions)
[](https://github.com/xp-framework/core)
[](https://github.com/xp-framework/core/blob/master/LICENCE.md)
[](http://php.net/)
[](http://php.net/)
[](https://packagist.org/packages/xp-forge/compression)
Compressing output and decompressing input streams including GZip, BZip2 and Brotli.
Examples
--------
Reading a GZIP-compressed file:
```php
use io\streams\FileInputStream;
use io\streams\compress\GzipInputStream;
$in= new GzipInputStream(new FileInputStream('message.txt.gz'));
while ($in->available()) {
echo $in->read();
}
$in->close();
```
Writing a file, compressing the data on-the-fly with BZIP2:
```php
use io\streams\FileOutputStream;
use io\streams\compress\Bzip2OutputStream;
$out= new Bzip2OutputStream(new FileOutputStream('message.txt.bz2'));
$out->write('Hello World!');
$out->write("\n");
$out->close();
```
Dependencies
------------
Compression algorithms are implemented in C and thus require a specific PHP extension:
* **GZip** - requires PHP's ["zlib" extension](https://www.php.net/zlib)
* **Bzip2** - requires PHP's ["bzip2" extension](https://www.php.net/bzip2)
* **Brotli** - requires https://github.com/kjdev/php-ext-brotli
Accessing these algorithms can be done via the `Compression` API:
```php
use io\streams\{Compression, FileInputStream, FileOutputStream};
// Returns an algorithm instance. Raises a lang.MethodNotImplementedException
// if the required "bzip2" extension is not loaded
$compressed= Compression::named('bzip2');
// Read
$bytes= '';
$in= $compressed->open(new FileInputStream($file));
while ($in->available()) {
$bytes.= $in->read();
}
$in->close();
// Write using strongest compression (other predefined values are FASTEST
// and DEFAULT; alternatively, the level can be passed directly).
$out= $compressed->create(new FileOutputStream($file), Compression::STRONGEST);
$out->write($bytes);
$out->close();
```
Discovering supported algorithms can be done using the `Compression` API:
```php
use io\streams\Compression;
echo "Supported algorithms:\n";
foreach (Compression::algorithms()->supported() as $compression) {
echo '✓ ', $compression->name(), "\n";
}
```
...or as a one-line shell command:
```bash
$ xp -w '\io\streams\Compression::algorithms()'
io.streams.compress.Algorithms@{
io.streams.compress.Gzip(token: gzip, extension: .gz, supported: true, levels: 1..9)
io.streams.compress.Bzip2(token: bzip2, extension: .bz2, supported: false, levels: 1..9)
io.streams.compress.Brotli(token: br, extension: .br, supported: true, levels: 1..11)
}
```
Advanced example
----------------
Fetching a given URL using [HTTP Accept-Encoding and Content-Encoding](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding):
```php
use io\streams\Compression;
use peer\http\HttpConnection;
// Compile list of supported compression algorithms, e.g. "gzip, br"
$accept= Compression::algorithms()->accept();
echo "== Sending {$accept} ==\n";
// Make request, sending supported content encodings via Accept-Encoding
$conn= new HttpConnection($argv[1]);
$res= $conn->get(null, ['Accept-Encoding' => $accept]);
// Handle Content-Encoding header
if ($encoding= $res->header('Content-Encoding')) {
$compression= Compression::named($encoding[0]);
echo "== Using ", $compression->name(), " ==\n";
$in= $compression->open($res->in());
} else {
echo "== Uncompressed ==\n";
$in= $res->in();
}
// Write contents to output
while ($in->available()) {
echo $in->read();
}
$in->close();
```
See also
--------
* The PHP RFC [Modern Compression](https://wiki.php.net/rfc/modern_compression) suggests adding *zstd* and *brotli* into PHP.