{"id":18550292,"url":"https://github.com/xp-forge/compression","last_synced_at":"2025-07-11T08:11:06.270Z","repository":{"id":43657554,"uuid":"461171971","full_name":"xp-forge/compression","owner":"xp-forge","description":"Compression streams","archived":false,"fork":false,"pushed_at":"2024-05-30T10:17:10.000Z","size":57,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-14T13:57:38.966Z","etag":null,"topics":["brotli","bzip2","compression","gzip","php7","php8","xp-framework"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xp-forge.png","metadata":{"files":{"readme":"README.md","changelog":"ChangeLog.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-02-19T11:31:36.000Z","updated_at":"2022-02-26T11:39:43.000Z","dependencies_parsed_at":"2024-11-06T21:04:12.004Z","dependency_job_id":"66267865-1871-48c4-9cfb-d5ba1058a463","html_url":"https://github.com/xp-forge/compression","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-forge%2Fcompression","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-forge%2Fcompression/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-forge%2Fcompression/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-forge%2Fcompression/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xp-forge","download_url":"https://codeload.github.com/xp-forge/compression/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254319722,"owners_count":22051075,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["brotli","bzip2","compression","gzip","php7","php8","xp-framework"],"created_at":"2024-11-06T21:04:07.685Z","updated_at":"2025-05-15T10:09:01.780Z","avatar_url":"https://github.com/xp-forge.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Compression streams\n===================\n\n[![Build status on GitHub](https://github.com/xp-forge/compression/workflows/Tests/badge.svg)](https://github.com/xp-forge/compression/actions)\n[![XP Framework Module](https://raw.githubusercontent.com/xp-framework/web/master/static/xp-framework-badge.png)](https://github.com/xp-framework/core)\n[![BSD Licence](https://raw.githubusercontent.com/xp-framework/web/master/static/licence-bsd.png)](https://github.com/xp-framework/core/blob/master/LICENCE.md)\n[![Requires PHP 7.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-7_0plus.svg)](http://php.net/)\n[![Supports PHP 8.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-8_0plus.svg)](http://php.net/)\n[![Latest Stable Version](https://poser.pugx.org/xp-forge/compression/version.svg)](https://packagist.org/packages/xp-forge/compression)\n\nCompressing output and decompressing input streams including GZip, BZip2 and Brotli.\n\nExamples\n--------\nReading a GZIP-compressed file:\n\n```php\nuse io\\streams\\FileInputStream;\nuse io\\streams\\compress\\GzipInputStream;\n\n$in= new GzipInputStream(new FileInputStream('message.txt.gz'));\nwhile ($in-\u003eavailable()) {\n  echo $in-\u003eread();\n}\n$in-\u003eclose();\n```\n\nWriting a file, compressing the data on-the-fly with BZIP2:\n\n```php\nuse io\\streams\\FileOutputStream;\nuse io\\streams\\compress\\Bzip2OutputStream;\n\n$out= new Bzip2OutputStream(new FileOutputStream('message.txt.bz2'));\n$out-\u003ewrite('Hello World!');\n$out-\u003ewrite(\"\\n\");\n$out-\u003eclose();\n```\n\nDependencies\n------------\nCompression algorithms are implemented in C and thus require a specific PHP extension:\n\n* **GZip** - requires PHP's [\"zlib\" extension](https://www.php.net/zlib)\n* **Bzip2** - requires PHP's [\"bzip2\" extension](https://www.php.net/bzip2)\n* **Brotli** - requires https://github.com/kjdev/php-ext-brotli\n\nAccessing these algorithms can be done via the `Compression` API:\n\n```php\nuse io\\streams\\{Compression, FileInputStream, FileOutputStream};\n\n// Returns an algorithm instance. Raises a lang.MethodNotImplementedException\n// if the required \"bzip2\" extension is not loaded\n$compressed= Compression::named('bzip2');\n\n// Read\n$bytes= '';\n$in= $compressed-\u003eopen(new FileInputStream($file));\nwhile ($in-\u003eavailable()) {\n  $bytes.= $in-\u003eread();\n}\n$in-\u003eclose();\n\n// Write using strongest compression (other predefined values are FASTEST\n// and DEFAULT; alternatively, the level can be passed directly).\n$out= $compressed-\u003ecreate(new FileOutputStream($file), Compression::STRONGEST);\n$out-\u003ewrite($bytes);\n$out-\u003eclose();\n```\n\nDiscovering supported algorithms can be done using the `Compression` API:\n\n```php\nuse io\\streams\\Compression;\n\necho \"Supported algorithms:\\n\";\nforeach (Compression::algorithms()-\u003esupported() as $compression) {\n  echo '✓ ', $compression-\u003ename(), \"\\n\";\n}\n```\n\n...or as a one-line shell command:\n\n```bash\n$ xp -w '\\io\\streams\\Compression::algorithms()'\nio.streams.compress.Algorithms@{\n  io.streams.compress.Gzip(token: gzip, extension: .gz, supported: true, levels: 1..9)\n  io.streams.compress.Bzip2(token: bzip2, extension: .bz2, supported: false, levels: 1..9)\n  io.streams.compress.Brotli(token: br, extension: .br, supported: true, levels: 1..11)\n}\n```\n\nAdvanced example\n----------------\nFetching a given URL using [HTTP Accept-Encoding and Content-Encoding](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding):\n\n```php\nuse io\\streams\\Compression;\nuse peer\\http\\HttpConnection;\n\n// Compile list of supported compression algorithms, e.g. \"gzip, br\"\n$accept= Compression::algorithms()-\u003eaccept();\necho \"== Sending {$accept} ==\\n\";\n\n// Make request, sending supported content encodings via Accept-Encoding\n$conn= new HttpConnection($argv[1]);\n$res= $conn-\u003eget(null, ['Accept-Encoding' =\u003e $accept]);\n\n// Handle Content-Encoding header\nif ($encoding= $res-\u003eheader('Content-Encoding')) {\n  $compression= Compression::named($encoding[0]);\n\n  echo \"== Using \", $compression-\u003ename(), \" ==\\n\";\n  $in= $compression-\u003eopen($res-\u003ein());\n} else {\n  echo \"== Uncompressed ==\\n\";\n  $in= $res-\u003ein();\n}\n\n// Write contents to output\nwhile ($in-\u003eavailable()) {\n  echo $in-\u003eread();\n}\n$in-\u003eclose();\n```\n\nSee also\n--------\n* The PHP RFC [Modern Compression](https://wiki.php.net/rfc/modern_compression) suggests adding *zstd* and *brotli* into PHP.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxp-forge%2Fcompression","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxp-forge%2Fcompression","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxp-forge%2Fcompression/lists"}