PHP code example of gemorroj / archive7z

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

    

gemorroj / archive7z example snippets



use Archive7z\Archive7z;

$archive = new Archive7z('path_to_archive.7z');

if (!$archive->isValid()) {
    throw new \RuntimeException('Incorrect archive');
}

// archive type, sizes, method, solid flag, ... (Model\Info)
print_r($archive->getInfo());

foreach ($archive->getEntries() as $entry) {
    printf("%-30s %10s\n", $entry->getPath(), $entry->getSize());
}

// filter by a wildcard mask and/or limit the count
$images = $archive->getEntries('*.jpg', 100);


use Archive7z\Archive7z;

$archive = new Archive7z('path_to_archive.7z');

// bytes of a single file
$content = $archive->getContent('test/test.txt');

// one entry by its exact name (no wildcard expansion)
$entry = $archive->getEntry('test/test.txt');
echo $entry->getSize(), ' ', $entry->getCrc(), "\n";
$entry->extract('path_to_folder/'); // the same as $archive->extractEntry('test/test.txt', 'path_to_folder/')

// the whole archive
$archive->extract('path_to_folder/');


use Archive7z\Archive7z;
use Archive7z\Config\CompressOptions;
use Archive7z\Config\Options;
use Archive7z\Config\SolidMode;

// the format comes from the extension; the file is created if it does not exist yet
$archive = new Archive7z('new.7z', new Options(
    compress: new CompressOptions(
        compressionLevel: 9,
        solidMode: SolidMode::on(),
    ),
));

$archive->addEntry(__FILE__); // a single file (stored by basename)
$archive->addEntry(__DIR__);  // a directory, recursively

// zip / tar / tar.gz / xz / bz2 / wim are created the same way
$zip = new Archive7z('new.zip', new Options(compress: new CompressOptions(compressionLevel: 6)));
$zip->addEntry('path_to_file.txt');


use Archive7z\Archive7z;

$archive = new Archive7z('path_to_archive.7z');

$archive->addEntry('another.txt'); // append to an existing archive
$archive->renameEntry('another.txt', 'renamed.txt');
$archive->delEntry('renamed.txt');


use Archive7z\Archive7z;
use Archive7z\Config\CompressOptions;
use Archive7z\Config\Options;

// create an encrypted archive, encrypting the file names too (7z format only)
$archive = new Archive7z('secret.7z', new Options(
    password: 'pass',
    compress: new CompressOptions(encryptFilenames: true),
));
$archive->addEntry(__FILE__);

// read it back
$read = new Archive7z('secret.7z', new Options(password: 'pass'));
echo $read->getContent(basename(__FILE__));


use Archive7z\Archive7z;
use Archive7z\Config\CompressOptions;
use Archive7z\Config\ExtractOptions;
use Archive7z\Config\Options;
use Archive7z\Config\SolidMode;

$archive = new Archive7z('backup.7z', new Options(
    extract: new ExtractOptions(
        overwriteMode: ExtractOptions::OVERWRITE_MODE_S, // skip existing files when extracting
    ),
    compress: new CompressOptions(
        compressionLevel: 9,
        solidMode: SolidMode::on(),
    ),
));
$archive->addEntry(__DIR__);
$archive->extract('path_to_folder/');


use Archive7z\Archive7z;
use Archive7z\Config\Options;
use Archive7z\Config\FallbackEncoding;

// detected automatically (default)
$archive = new Archive7z('old.zip');

// pin the exact code page when the guess must be deterministic
$archive = new Archive7z('old.zip', new Options(
    fallbackEncoding: FallbackEncoding::codepage('CP866'),
));

foreach ($archive->getEntries() as $entry) {
    echo $entry->getPath(), "\n"; // e.g. чавес.jpg
}


use Archive7z\Config\Options;
use Archive7z\Config\FallbackEncoding;

// detection is the default; pass candidates to restrict the guess (best effort)
new Options(fallbackEncoding: FallbackEncoding::detect());

// or decode the names yourself: $decode maps raw bytes -> UTF-8,
// the optional $encode maps a UTF-8 mask/path back to raw bytes
new Options(fallbackEncoding: FallbackEncoding::viaCallback(
    static function (string $raw): string {
        return \mb_convert_encoding($raw, 'UTF-8', 'CP866');
    },
    static function (string $name): string {
        return \mb_convert_encoding($name, 'CP866', 'UTF-8');
    },
));


use Archive7z\Archive7z;
use Archive7z\Config\Options;

// a specific 7-Zip binary and a longer timeout (seconds)
$archive = new Archive7z('archive.7z', null, '/usr/bin/7zz', 120.0);

// environment variables for the 7-Zip process
new Options(env: ['LC_ALL' => 'ru_RU.UTF-8']);

// 7-Zip build info and archive warnings
echo Archive7z::get7zipInformation();
var_dump($archive->getWarnings());