gemorroj / archive7z
7z cli wrapper
5.9.0
2026-01-25 11:31 UTC
Requires
- php: >=8.2
- symfony/process: ^7.4||^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.93
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.5||^12.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Features:
- Supported all 7-zip formats:
- Packing / unpacking:
7z,XZ,BZIP2,GZIP,TAR,ZIPandWIM. - Unpacking only:
APFS,AR,ARJ,Base64,CAB,CHM,CPIO,CramFS,DMG,EXT,FAT,GPT,HFS,IHEX,ISO,LZH,LZMA,MBR,MSI,NSIS,NTFS,QCOW2,RAR,RPM,SquashFS,UDF,UEFI,VDI,VHD,VHDX,VMDK,XARandZ.
- Packing / unpacking:
- List files and directories
- Get contents of any file from archive
- Delete files or directories
- Add files or directories
Requirements:
- PHP >= 8.2
- allow proc_open function
- for Windows: 7-zip >= 7.30 (https://www.7-zip.org/)
- for Linux/MacOs: 7-zip >= 21.01 or p7zip >= 9.38 (https://github.com/p7zip-project/p7zip)
Installation:
composer require gemorroj/archive7z
Notes:
- https://sourceforge.net/p/p7zip/discussion/383043/thread/fa143cf2/
- Filenames are expected to be UTF-8; for non-UTF-8 names pass
fallbackEncodingtoArchiveOptions, see #15. - Pass
envtoArchiveOptionsto set environment variables for the 7-Zip process (for exampleenv: ['LC_ALL' => 'ru_RU.UTF-8']).
Limitations:
- Filenames are given as UTF-8. Archives whose names use another encoding (CP866, CP1251, ...) need an explicit
ArchiveOptions::fallbackEncoding—FallbackEncoding::codepage(...)(exact) ordetect()(heuristic); otherwise they are unreadable on Linux and may be mangled on Windows. - For
zip/tar/gzipthe names are read from the container, so listing, selection and extraction behave the same on every platform; other formats rely on 7-Zip's own decoding.
Recommendations:
Archive7z is final and wraps the 7-Zip feature set.
Reuse it by composition — a higher-level class that has an Archive7z instance.
Example:
<?php use Archive7z\Archive7z; use Archive7z\Config\ArchiveOptions; use Archive7z\Config\SolidMode; $obj = new Archive7z( 'path_to_7z_file.7z', new ArchiveOptions( compressionLevel: 6, overwriteMode: ArchiveOptions::OVERWRITE_MODE_S, ), ); if (!$obj->isValid()) { throw new \RuntimeException('Incorrect archive'); } print_r($obj->getInfo()); /* Archive7z\Model\Info Object ( [data:Archive7z\Model\Info:private] => Array ( ... ) [path:Archive7z\Model\Info:private] => /full_path_to/test.7z [type:Archive7z\Model\Info:private] => 7z [physicalSize:Archive7z\Model\Info:private] => 165343 [headersSize:Archive7z\Model\Info:private] => 241 [method:Archive7z\Model\Info:private] => LZMA2:192k [solid:Archive7z\Model\Info:private] => + [blocks:Archive7z\Model\Info:private] => 1 [codePage:Archive7z\Model\Info:private] => ) */ // $obj->getEntries('test', 100) foreach ($obj->getEntries() as $entry) { print_r($entry); /* Archive7z\Model\Entry Object ( [data:Archive7z\Model\Entry:private] => Array ( ... ) [path:Archive7z\Model\Entry:private] => 1.jpg [size:Archive7z\Model\Entry:private] => 91216 [packedSize:Archive7z\Model\Entry:private] => 165102 [modified:Archive7z\Model\Entry:private] => 2013-06-10 09:56:07.0000000 [created:Archive7z\Model\Entry:private] => [attributes:Archive7z\Model\Entry:private] => A [crc:Archive7z\Model\Entry:private] => 871345C2 [encrypted:Archive7z\Model\Entry:private] => - [method:Archive7z\Model\Entry:private] => LZMA2:192k [block:Archive7z\Model\Entry:private] => 0 [comment:Archive7z\Model\Entry:private] => [hostOs:Archive7z\Model\Entry:private] => [characteristics:Archive7z\Model\Entry:private] => [folder:Archive7z\Model\Entry:private] => [archive:Archive7z\Model\Entry:private] => Archive7z\Archive7z Object ( ... ) ) */ if ($entry->getPath() === 'test/test.txt') { $entry->extract('path_to_extract_folder/'); // extract the file } } echo $obj->getContent('test/test.txt'); // show content of the file $obj->extract('path_to_extract_folder/'); // extract the archive // password-protected archive: pass the password to ArchiveOptions $obj = new Archive7z('path_to_protected.7z', new ArchiveOptions(password: 'pass')); $obj->extractEntry('test/test.txt', 'path_to_extract_pass_folder/'); // extract the password-protected entry $archive = new Archive7z('path_to_archive.7z', new ArchiveOptions(solidMode: SolidMode::off())); $archive->addEntry(__DIR__); // add directory to the archive (include subfolders) $archive->addEntry(__FILE__); // add file to the archive $archive->renameEntry(__FILE__, __FILE__.'new'); // rename the file in the archive $archive->delEntry(__FILE__.'new'); // remove the file from the archive