PHP code example of petrknap / xz-utils

1. Go to this page and download the library: Download petrknap/xz-utils library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

petrknap / xz-utils example snippets


$encoded = xzencode(b'data') or throw new Exception();
$decoded = xzdecode($encoded) or throw new Exception();

printf('`%s` was decoded from encoded base64(`%s`)', $decoded, base64_encode($encoded));

$xz = new PetrKnap\XzUtils\Xz();
$compressed = $xz->compress(b'data');
$decompressed = $xz->decompress($compressed);

printf('`%s` was decompressed from compressed base64(`%s`)', $decompressed, base64_encode($compressed));

use PetrKnap\ExternalFilter\Filter;
use PetrKnap\XzUtils\FilterFactory;

$file = fopen('README.md', 'r');
$xzFile = tmpfile();

# compress file to file
FilterFactory::xz()->compress()->filter(input: $file, output: $xzFile);

fclose($file);
rewind($xzFile);

# decompress file, filter headline and print it
print FilterFactory::xz()->decompress()->pipe(new Filter('head', ['-n', '1']))->filter($xzFile);

fclose($xzFile);