PHP code example of horde / compress

1. Go to this page and download the library: Download horde/compress 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/ */

    

horde / compress example snippets


use Horde\Compress\CompressFactory;

$factory = new CompressFactory();
$zip = $factory->create('zip');
$tar = $factory->create('tar');
$gzip = $factory->create('gzip');

$factory = new CompressFactory($logger);
$zip = $factory->create('zip');

use Horde\Compress\CompressFactory;
use Horde\Compress\Driver\Zip;

$zip = (new CompressFactory())->create('zip');

$files = [
    ['data' => 'Hello World', 'name' => 'hello.txt', 'time' => time()],
    ['data' => file_get_contents('image.png'), 'name' => 'image.png', 'time' => filemtime('image.png')],
];

$archive = $zip->compress($files);
file_put_contents('archive.zip', $archive);

use Horde\Compress\Driver\Zip;

$zip = (new CompressFactory())->create('zip');
$data = file_get_contents('archive.zip');

// List contents
$listing = $zip->decompress($data, ['action' => Zip::ZIP_LIST]);
foreach ($listing as $entry) {
    echo $entry['name'] . ' (' . $entry['size'] . " bytes)\n";
}

// Extract a specific file by index
$content = $zip->decompress($data, ['action' => Zip::ZIP_DATA, 'info' => $listing, 'key' => 0]);

use Horde\Compress\CompressFactory;

$tar = (new CompressFactory())->create('tar');

$files = [
    ['data' => 'content', 'name' => 'file.txt', 'time' => time()],
];

$archive = $tar->compress($files);
file_put_contents('archive.tar', $archive);

$tar = (new CompressFactory())->create('tar');
$archive = $tar->compressDirectory('/path/to/directory');

$gzip = (new CompressFactory())->create('gzip');
$raw = $gzip->decompress(file_get_contents('file.gz'));

use Horde\Compress\Tnef\TnefDecoder;

$decoder = new TnefDecoder();
$parts = $decoder->decompress(file_get_contents('winmail.dat'));

foreach ($parts as $part) {
    echo $part['name'] . ' (' . $part['type'] . '/' . $part['subtype'] . ")\n";
}

// Access message metadata
$msgInfo = $decoder->getMsgInfo();
echo 'Subject: ' . $msgInfo->subject . "\n";

$rar = (new CompressFactory())->create('rar');
$listing = $rar->decompress(file_get_contents('archive.rar'));

foreach ($listing as $entry) {
    echo $entry['name'] . ' (' . $entry['size'] . " bytes)\n";
}