Home

Awesome

Zstd Extension for PHP

Linux Windows

This extension allows Zstandard.

Documentation for Zstandard can be found at » https://github.com/facebook/zstd.

Build from sources

% git clone --recursive --depth=1 https://github.com/kjdev/php-ext-zstd.git
% cd php-ext-zstd
% phpize
% ./configure
% make
% make install

To use the system library

% ./configure --with-libzstd

Install from pecl:

% pecl install zstd

Distribution binary packages

Fedora

Fedora users can install the » php-zstd package from official repository.

dnf install php-zstd

CentOS / RHEL

CentOS / RHEL (and other clones) users can install the » php-zstd package from » EPEL repository.

yum install php-zstd

Other RPM packages of this extension, for other PHP versions, are available in » Remi's RPM repository.

Configration

php.ini:

extension=zstd.so

Output handler option

NameDefaultChangeable
zstd.output_compression0PHP_INI_ALL
zstd.output_compression_level-1PHP_INI_ALL
zstd.output_compression_dict""PHP_INI_ALL

Constant

NameDescription
ZSTD_COMPRESS_LEVEL_MINMinimal compress level value
ZSTD_COMPRESS_LEVEL_MAXMaximal compress level value
ZSTD_COMPRESS_LEVEL_DEFAULTDefault compress level value
LIBZSTD_VERSION_NUMBERlibzstd version number
LIBZSTD_VERSION_STRINGlibzstd version string

Function

zstd_compress — Zstandard compression

Description

string zstd_compress ( string $data [, int $level = 3 ] )

Zstandard compression.

Parameters

Return Values

Returns the compressed data or FALSE if an error occurred.

zstd_uncompress — Zstandard decompression

Description

string zstd_uncompress ( string $data )

Zstandard decompression.

Alias: zstd_decompress

Parameters

Return Values

Returns the decompressed data or FALSE if an error occurred.

zstd_compress_dict — Zstandard compression using a digested dictionary

Description

string zstd_compress_dict ( string $data , string $dict [, int $level = 3 ])

Zstandard compression using a digested dictionary.

Alias: zstd_compress_usingcdict

Parameters

Return Values

Returns the compressed data or FALSE if an error occurred.

zstd_uncompress_dict — Zstandard decompression using a digested dictionary

Description

string zstd_uncompress_dict ( string $data , string $dict )

Zstandard decompression using a digested dictionary.

Alias: zstd_decompress_dict, zstd_uncompress_usingcdict, zstd_decompress_usingcdict

Parameters

Return Values

Returns the decompressed data or FALSE if an error occurred.

Namespace

Namespace Zstd;

function compress( $data [, $level = 3 ] )
function uncompress( $data )
function compress_dict ( $data, $dict )
function uncompress_dict ( $data, $dict )

zstd_compress, zstd_uncompress, zstd_compress_dict and zstd_uncompress_dict function alias.

Streams

Zstd compression and decompression are available using the compress.zstd:// stream prefix.

Output handler

ini_set('zstd.output_compression', 'On');
// OR
// ob_start('ob_zstd_handler');

echo ...;

"Accept-Encoding: zstd" must be specified.

Examples

// Using functions
$data = zstd_compress('test');
zstd_uncompress($data);

// Using namespaced functions
$data = \Zstd\compress('test');
\Zstd\uncompress($data);

// Using streams
file_put_contents("compress.zstd:///path/to/data.zstd", $data);
readfile("compress.zstd:///path/to/data.zstd");

// Providing level of compression, when using streams 
$context = stream_context_create([
    'zstd' => [
            'level' => ZSTD_COMPRESS_LEVEL_MIN,
        ],
    ],
);

file_put_contents("compress.zstd:///path/to/data.zstd", $data, context: $context);
readfile("compress.zstd:///path/to/data.zstd", context: $context);