Coder Social home page Coder Social logo

paulrotmann / reactphp-zlib Goto Github PK

View Code? Open in Web Editor NEW

This project forked from clue/reactphp-zlib

0.0 0.0 0.0 97 KB

Streaming zlib compressor and decompressor for ReactPHP, supporting compression and decompression of GZIP, ZLIB and raw DEFLATE formats.

Home Page: https://clue.engineering/2020/introducing-reactphp-zlib

License: MIT License

PHP 100.00%

reactphp-zlib's Introduction

clue/reactphp-zlib

CI status installs on Packagist

Streaming zlib compressor and decompressor for ReactPHP, supporting compression and decompression of GZIP, ZLIB and raw DEFLATE formats.

Table of contents

Support us

We invest a lot of time developing, maintaining and updating our awesome open-source projects. You can help us sustain this high-quality of our work by becoming a sponsor on GitHub. Sponsors get numerous benefits in return, see our sponsoring page for details.

Let's take these projects to the next level together! ๐Ÿš€

Quickstart example

Once installed, you can use the following code to pipe a readable gzip file stream into an decompressor which emits decompressed data events for each individual log file chunk:

$loop = React\EventLoop\Factory::create();
$stream = new React\Stream\ReadableResourceStream(fopen('access.log.gz', 'r'), $loop);

$decompressor = new Clue\React\Zlib\Decompressor(ZLIB_ENCODING_GZIP);
$stream->pipe($decompressor);

$decompressor->on('data', function ($data) {
    echo $data; // chunk of decompressed log data
});

$loop->run();

See also the examples.

Formats

This library is a lightweight wrapper around the underlying zlib library. The zlib library offers a number of different formats (sometimes referred to as encodings) detailled below.

GZIP format

This library supports the GZIP compression format as defined in RFC 1952. This is one of the more common compression formats and is used in several places:

  • PHP: ZLIB_ENCODING_GZIP (PHP 5.4+ only)
  • PHP: gzdecode() (PHP 5.4+ only) and gzencode()
  • Files with .gz file extension, e.g. .tar.gz or .tgz archives (also known as "tarballs")
  • gzip and gunzip (and family) command line tools
  • HTTP compression with Content-Encoding: gzip header
  • Java: GZIPOutputStream

Technically, this format uses raw DEFLATE compression wrapped in a GZIP header and footer:

10 bytes header (+ optional headers) + raw DEFLATE body + 8 bytes footer

Raw DEFLATE format

This library supports the raw DEFLATE compression format as defined in RFC 1951. The DEFLATE compression algorithm returns what we refer to as "raw DEFLATE format". This raw DEFLATE format is commonly wrapped in container formats instead of being used directly:

  • PHP: ZLIB_ENCODING_RAW (PHP 5.4+ only)
  • PHP: gzdeflate() and gzinflate()
  • Wrapped in GZIP format
  • Wrapped in ZLIB format

Note: This format is not to be confused with what some people call "deflate format" or "deflate encoding". These names are commonly used to refer to what we call ZLIB format.

ZLIB format

This library supports the ZLIB compression format as defined in RFC 1950. This format is commonly used in a streaming context:

  • PHP: ZLIB_ENCODING_DEFLATE (PHP 5.4+ only)
  • PHP: gzcompress() and gzuncompress()
  • HTTP compression with Content-Encoding: deflate header
  • Java: DeflaterOutputStream
  • Qt's qCompress() and qUncompress() uses the ZLIB format prefixed with the uncompressed length (as UINT32BE).

Technically, this format uses raw DEFLATE compression wrapped in a ZLIB header and footer:

2 bytes header (+ optional headers) + raw DEFLATE body + 4 bytes footer

Note: This format is often referred to as the "deflate format" or "deflate encoding". This documentation avoids this name in order to avoid confusion with the raw DEFLATE format.

Usage

All classes use the Clue\React\Zlib namespace.

Compressor

The Compressor class can be used to compress a stream of data.

It implements the DuplexStreamInterface and accepts uncompressed data on its writable side and emits compressed data on its readable side.

$encoding = ZLIB_ENCODING_GZIP; // or ZLIB_ENCODING_RAW or ZLIB_ENCODING_DEFLATE
$compressor = new Clue\React\Zlib\Compressor($encoding);

$compressor->on('data', function ($data) {
    echo $data; // compressed binary data chunk
});

$compressor->write($uncompressed); // write uncompressed data chunk

This is particularly useful in a piping context:

$input->pipe($filterBadWords)->pipe($compressor)->pipe($output);

For more details, see ReactPHP's DuplexStreamInterface.

Decompressor

The Decompressor class can be used to decompress a stream of data.

It implements the DuplexStreamInterface and accepts compressed data on its writable side and emits decompressed data on its readable side.

$encoding = ZLIB_ENCODING_GZIP; // or ZLIB_ENCODING_RAW or ZLIB_ENCODING_DEFLATE
$decompressor = new Clue\React\Zlib\Decompressor($encoding);

$decompressor->on('data', function ($data) {
    echo $data; // decompressed data chunk
});

$decompressor->write($compressed); // write compressed binary data chunk

This is particularly useful in a piping context:

$input->pipe($decompressor)->pipe($filterBadWords)->pipe($output);

For more details, see ReactPHP's DuplexStreamInterface.

Install

The recommended way to install this library is through Composer. New to Composer?

This project follows SemVer. This will install the latest supported version:

$ composer require clue/zlib-react:^1.1

See also the CHANGELOG for details about version upgrades.

This project aims to run on any platform and thus does not require any PHP extensions besides ext-zlib and supports running on PHP 7 through current PHP 8+.

The ext-zlib extension is required for handling the underlying data compression and decompression. This extension is already installed as part of many PHP distributions out-of-the-box, e.g. it ships with Debian/Ubuntu-based PHP installations and Windows-based builds by default. If you're building PHP from source, you may have to manually enable it.

We're committed to providing a smooth upgrade path for legacy setups. If you need to support legacy PHP versions and legacy HHVM, you may want to check out the legacy v0.2.x release branch. This legacy release branch also provides an installation candidate that does not require ext-zlib during installation but uses runtime checks instead. In this case, you can install this project like this:

$ composer require "clue/zlib-react:^1.0||^0.2.2"

Tests

To run the test suite, you first need to clone this repo and then install all dependencies through Composer:

$ composer install

To run the test suite, go to the project root and run:

$ php vendor/bin/phpunit

License

This project is released under the permissive MIT license.

Did you know that I offer custom development services and issuing invoices for sponsorships of releases and for contributions? Contact me (@clue) for details.

More

  • If you want to learn more about processing streams of data, refer to the documentation of the underlying react/stream component.

  • If you want to process compressed tarballs (.tar.gz and .tgz file extension), you may want to use clue/reactphp-tar on the decompressed stream.

reactphp-zlib's People

Contributors

clue avatar simonfrings avatar paulrotmann avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.