PHPackages                             xp-forge/compression - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. xp-forge/compression

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

xp-forge/compression
====================

Compression I/O for the XP Framework

v2.0.1(9mo ago)069.3k4BSD-3-ClausePHPPHP &gt;=7.4.0CI passing

Since Feb 19Pushed 9mo ago1 watchersCompare

[ Source](https://github.com/xp-forge/compression)[ Packagist](https://packagist.org/packages/xp-forge/compression)[ Docs](http://xp-framework.net/)[ RSS](/packages/xp-forge-compression/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (2)Versions (15)Used By (4)

Compression streams
===================

[](#compression-streams)

[![Build status on GitHub](https://github.com/xp-forge/compression/workflows/Tests/badge.svg)](https://github.com/xp-forge/compression/actions)[![XP Framework Module](https://raw.githubusercontent.com/xp-framework/web/master/static/xp-framework-badge.png)](https://github.com/xp-framework/core)[![BSD Licence](https://raw.githubusercontent.com/xp-framework/web/master/static/licence-bsd.png)](https://github.com/xp-framework/core/blob/master/LICENCE.md)[![Requires PHP 7.4+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-7_4plus.svg)](http://php.net/)[![Supports PHP 8.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-8_0plus.svg)](http://php.net/)[![Latest Stable Version](https://camo.githubusercontent.com/680201f2b24832b2e5ce9b65c46461ffc1b3caaae4e8bc74b367af0d176a535e/68747470733a2f2f706f7365722e707567782e6f72672f78702d666f7267652f636f6d7072657373696f6e2f76657273696f6e2e737667)](https://packagist.org/packages/xp-forge/compression)

Compressing output and decompressing input streams including GZip, BZip2, Brotli, Snappy and ZStandard.

Examples
--------

[](#examples)

Reading a GZIP-compressed file:

```
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:

```
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
------------

[](#dependencies)

Compression algorithms might 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
- **Snappy** - *no dependencies, implemented in userland*
- **ZStandard** - requires

Accessing these algorithms can be done via the `Compression` API:

```
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');

// Compress and decompress
$bytes= $compressed->compress('Test', Compression::STRONGEST);
$test= $compressed->decompress($bytes);
```

Continuing the above example using streams:

```
// Read from a file
$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:

```
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:

```
$ 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)
  io.streams.compress.Snappy(token: snappy, extension: .sn, supported: true, levels: 0..0)
  io.streams.compress.ZStandard(token: zstd, extension: .zstd, supported: true, levels: 1..22)
}
```

Advanced example
----------------

[](#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):

```
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
--------

[](#see-also)

- The PHP RFC [Modern Compression](https://wiki.php.net/rfc/modern_compression) suggests adding *zstd* and *brotli* into PHP.
- Snappy *does not aim for maximum compression, or compatibility with any other compression library; instead, it aims for very high speeds and reasonable compression*, quoting [its Wikipedia page](https://en.wikipedia.org/wiki/Snappy_(compression))

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance58

Moderate activity, may be stable

Popularity28

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~98 days

Recently: every ~13 days

Total

14

Last Release

275d ago

Major Versions

v0.3.0 → v1.0.02022-02-26

v1.4.0 → v2.0.02025-08-15

PHP version history (2 changes)v0.1.0PHP &gt;=7.0.0

v2.0.0PHP &gt;=7.4.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/07d18d882c8b4aaf3466432f64018214f2771eda333202175431ee7233795376?d=identicon)[thekid](/maintainers/thekid)

---

Top Contributors

[![thekid](https://avatars.githubusercontent.com/u/696742?v=4)](https://github.com/thekid "thekid (149 commits)")

---

Tags

brotlibzip2compressiongzipphp7php8xp-frameworklanguagemodulexp

### Embed Badge

![Health badge](/badges/xp-forge-compression/health.svg)

```
[![Health](https://phpackages.com/badges/xp-forge-compression/health.svg)](https://phpackages.com/packages/xp-forge-compression)
```

###  Alternatives

[hiqdev/yii2-language

Yii2 module for language switching

1126.1k1](/packages/hiqdev-yii2-language)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
