All formats

Archive7z

LanguagePHP 8.2+
LicenseLGPL-3.0-or-later
Repositorygithub.com/Gemorroj/Archive7z
DocumentationREADME
Depends onthe 7-Zip program (7z, 7zz or 7za; p7zip 9.38+ also works in version 5), symfony/process, proc_open enabled
Read / writeyes / yes, everything the installed 7-Zip supports, including AES-256 and encrypted file names

Archive7z runs the 7-Zip command-line program and parses its output. It lists entries with their size, CRC, method and encryption flag, returns file contents as a string, extracts single entries or whole archives, and adds, renames and deletes entries. Because 7-Zip does the work, every 7z feature is available: LZMA2, PPMd, BCJ2, solid blocks, AES-256, header encryption. The same class works for ZIP, TAR, XZ and the formats 7-Zip can only extract (RAR, ISO, CAB…).

The trade-offs of a command-line wrapper: 7-Zip must be installed on the server, each call starts a process, and shared hosting often disables proc_open. The examples use the API of version 5.x, the current stable release (the repository’s main branch is moving to an Options-based API).

Installation#

Shell
composer require gemorroj/archive7z
# and 7-Zip itself, for example:
sudo apt install 7zip        # Debian 12+ / Ubuntu 24.04+ (command 7zz); older: p7zip-full

On Windows install 7-Zip from 7-zip.org; the library looks for it in the default location, or pass the path to the constructor.

Basic usage: list and extract a 7z archive#

PHP
<?php
require 'vendor/autoload.php';

use Archive7z\Archive7z;

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

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

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

$archive->setOutputDirectory('output/')->extract();  // the folder must exist

How to read a file from a 7z archive into a string#

PHP
$archive = new Archive7z('archive.7z');
$text = $archive->getContent('docs/readme.txt');

How to extract a single file#

PHP
$archive = new Archive7z('archive.7z');
$archive->setOutputDirectory('output/')->extractEntry('docs/report.pdf');

// or from an entry object
$entry = $archive->getEntry('docs/report.pdf');
$entry?->extractTo('output/');

How to create a 7z archive#

PHP
<?php
use Archive7z\Archive7z;

// the format comes from the extension; the file is created by the first addEntry
$archive = new Archive7z('backup.7z');
$archive->addEntry('photos');       // a directory, recursively
$archive->addEntry('report.pdf');   // a single file, stored by its base name

The default compression level is 9 (maximum). To change it, extend the class and override the protected property, as the README recommends:

PHP
class FastArchive7z extends Archive7z
{
    protected int $compressionLevel = 1;   // 0 (store) .. 9
}

How to use a password#

PHP
$archive = new Archive7z('secret.7z');
$archive->setPassword('secret');
$archive->setEncryptFilenames(true);   // encrypt the header too (7z only)
$archive->addEntry('private');

$read = new Archive7z('secret.7z');
$read->setPassword('secret');
echo $read->getContent('notes.txt');

Tips#