xp-forge/compression
Composer 安装命令:
composer require xp-forge/compression
包简介
Compression I/O for the XP Framework
README 文档
README
Compressing output and decompressing input streams including GZip, BZip2, Brotli, Snappy and ZStandard.
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
Compression algorithms might require a specific PHP extension:
- GZip - requires PHP's "zlib" extension
- Bzip2 - requires PHP's "bzip2" extension
- Brotli - requires https://github.com/kjdev/php-ext-brotli
- Snappy - no dependencies, implemented in userland
- ZStandard - requires https://github.com/kjdev/php-ext-zstd
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
Fetching a given URL using HTTP Accept-Encoding and 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
- The PHP 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
xp-forge/compression 适用场景与选型建议
xp-forge/compression 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 70.37k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 02 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「module」 「language」 「xp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 xp-forge/compression 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 xp-forge/compression 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 xp-forge/compression 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
Easy to use i18n translation PHP class for multi-language websites
Package for convenient work with Laravel's localization features
Store your language lines in the database, yaml or other sources
Set Links with a specific language parameter
Assign a language to a module
统计信息
- 总下载量: 70.37k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 9
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2022-02-19

