PHP package to handle archives (.zip, .rar, .tar, .7z) or .pdf with hybrid solution (native/p7zip), designed to works with eBooks (.epub, .cbz, .cbr, .cb7, .cbt).
Fund package maintenance!
Open Issues: 0
- php: ^8.1
- smalot/pdfparser: ^2.4
- symfony/process: ^6.2
- laravel/pint: ^1.6
- pestphp/pest: ^2.0
- phpstan/phpstan: ^1.10
- spatie/ray: ^1.28
- ext-imagick: *
- ext-rar: *
This package is auto-updated.
Last update: 2023-05-29 18:03:14 UTC
PHP package to handle archives (
p7zip), designed to works with eBooks (
Supports Linux, macOS and Windows.
For some formats (
rarPHP extension or p7zip binary could be necessary, see Requirements.
- PHP >= 8.1
- Depends of archive type and features you want to use
|✅||Optional (for extraction)
Here you can read some installation guides for dependencies
- On macOS, for
.rarextract, you have to install
rarbinary to extract files,
- On Windows, for
imagickPHP extension have to work but my tests failed on this feature. So to extract PDF pages I advice to use WSL.
If you want more informations, you can read section About.
- List files as
filesmethod: list of files
firstmethod: first file
lastmethod: last file
findmethod: find first file that match with
filtermethod: find all files that match with
- Content of file
contentmethod: content of file as string (useful for images)
textmethod: content of text file (binaries files return
- Extract files
extractmethod: extract files to directory
extractAllmethod: extract all files to directory
- Metadata of archive with
- Useful for PDF files
- Count files
- Create or edit archives, only with
makemethod: create or edit archive
addFilesmethod: add files to archive
addFromStringmethod: add string to archive
addDirectoriesmethods: add directories to archive
savemethod: save archive
You can install the package via composer:
composer require kiwilan/php-archive
Read and extract
With archive file (
$archive = Archive::test('path/to/archive.zip'); $files = $archive->files(); // ArchiveItem $count = $archive->count(); // int of files count $images = $archive->filter('jpeg'); // ArchiveItem with `jpeg` in their path $metadataXml = $archive->find('metadata.xml'); // First ArchiveItem with `metadata.xml` in their path $content = $archive->content($metadataXml); // `metadata.xml` file content $paths = $archive->extract('/path/to/directory', [$metadataXml]); // string of extracted files paths $paths = $archive->extractAll('/path/to/directory'); // string of extracted files paths
PDF files works with same API than archives but with some differences.
$archive = Archive::test('path/to/file.pdf'); $metadata = $archive->metadata(); // Metadata of PDF $content = $archive->content($archive->first()); // PDF page as image $text = $archive->text($archive->first()); // PDF page as text
You can create archive with method
Works only with
$archive = Archive::make('path/to/archive.zip'); $archive->addFiles([ 'path/to/file1.txt', 'path/to/file2.txt', 'path/to/file3.txt', ]); $archive->addFromString('test.txt', 'Hello World!'); $archive->addDirectory('path/to/directory'); $archive->save();
You can edit archive with same method
$archive = Archive::make('path/to/archive.zip'); $archive->addFromString('test.txt', 'Hello World!'); $archive->save();
This package was inspired by this excellent post on StackOverflow which make state of the art of PHP archive handling. The package Gemorroj/Archive7z was also a good source of inspiration cause it's the only package that handle
.7z archives with wrapper of
p7zip fork binary. But I would to handle all main archives formats with native PHP solution it possible, and use
p7zip binary only if native solution is not available.
State of the art of PHP archive handling:
.rarwith RarArchive if
rarextension is installed
.7zcan't be handled with native PHP solution
Why not full wrapper of
This solution is used by Gemorroj/Archive7z, and it works well. But another problem is the usage of the
p7zip fork which is not the official
p7zip binary and can be difficult to install on some systems.
PHP can handle natively some archive formats, but not all. So I choose to use native PHP solution when it's possible, and use
p7zip binary with official version when it's not possible.
rar PHP extension is not installed by default on PHP, developers have to install it manually. This extension is not actively maintained and users could have some compilation problems. To install it with PHP 8.1 or 8.2, it's necessary to compile manually the extension, you could read this guide if you want to install it (for PHP 8.2, you will have a warning message but it's not a problem, the extension will work).
rar PHP extension is a problem because it's not sure to have a compatibility with future PHP versions. So I choose to handle
rar archives with
p7zip binary if
rar PHP extension is not installed.
PHP can't handle
.7z archives natively, so I choose to use
p7zip binary. You will have to install it on your system to use this package. You could read this guide if you want to install it.
PHP can't handle
smalot/pdfparser package, embedded in this package. To extract pages as images, you have to install
imagick extension you could read this guide if you want to install it.
eBooks and comics
This package can handle
.cbt archives, it's depends of the extension, check requirements section.
- Gemorroj/Archive7z: handle many archives with p7zip-project/p7zip binary
- splitbrain/php-archive: native PHP solution to handle
- List files in
.tararchives using PHP: https://stackoverflow.com/a/39163620/11008206
- Compression and Archive Extensions: https://www.php.net/manual/en/refs.compression.php
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
- All Contributors
- spatie for
smalot/pdfparserfor PDF parser
The MIT License (MIT). Please see License File for more information.