PHPackages                             kiwilan/php-archive - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [PDF &amp; Document Generation](/categories/documents)
4. /
5. kiwilan/php-archive

ActiveLibrary[PDF &amp; Document Generation](/categories/documents)

kiwilan/php-archive
===================

PHP package to handle archives (.zip, .rar, .tar, .7z, .pdf) with unified API and hybrid solution (native/p7zip), designed to works with EPUB and CBA (.cbz, .cbr, .cb7, .cbt).

2.3.02(8mo ago)1027.6k↓23.1%5[6 issues](https://github.com/kiwilan/php-archive/issues)[4 PRs](https://github.com/kiwilan/php-archive/pulls)1MITPHPPHP ^8.1CI passing

Since Mar 24Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/kiwilan/php-archive)[ Packagist](https://packagist.org/packages/kiwilan/php-archive)[ Docs](https://github.com/kiwilan/php-archive)[ GitHub Sponsors](https://github.com/kiwilan)[ RSS](/packages/kiwilan-php-archive/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (32)Used By (1)

PHP Archive
===========

[](#php-archive)

[![Banner with archives picture in background and PHP Archive title](https://raw.githubusercontent.com/kiwilan/php-archive/main/docs/banner.jpg)](https://raw.githubusercontent.com/kiwilan/php-archive/main/docs/banner.jpg)

[![php](https://camo.githubusercontent.com/2d44ab343cf5a2aab849b6954ee31dd09ba1f1f11bf0f168ccd08e186ec060b1/68747470733a2f2f696d672e736869656c64732e696f2f7374617469632f76313f7374796c653d666c6174266c6162656c3d504850266d6573736167653d76382e3126636f6c6f723d373737424234266c6f676f3d706870266c6f676f436f6c6f723d666666666666266c6162656c436f6c6f723d313831383162)](https://www.php.net/)[![version](https://camo.githubusercontent.com/dedf0f42606a637fbb451fdb36dfbc6844d5bb50303ccb38c6dac13b9a399d80/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6b6977696c616e2f7068702d617263686976652e7376673f7374796c653d666c617426636f6c6f72413d31383138314226636f6c6f72423d373737424234)](https://packagist.org/packages/kiwilan/php-archive)[![downloads](https://camo.githubusercontent.com/aaa629901d52748f1324303c07555fa715ce191a690d93f835be29d5ae88574d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6b6977696c616e2f7068702d617263686976652e7376673f7374796c653d666c617426636f6c6f72413d31383138314226636f6c6f72423d373737424234)](https://packagist.org/packages/kiwilan/php-archive)[![license](https://camo.githubusercontent.com/62173fc4d03cc7dd51f3997dce0fc86ce69e3b7230fd05640c560e1b1297fa58/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6b6977696c616e2f7068702d617263686976652e7376673f7374796c653d666c617426636f6c6f72413d31383138314226636f6c6f72423d373737424234)](https://github.com/kiwilan/php-archive/blob/main/README.md)[![tests](https://camo.githubusercontent.com/f6468f98d06078486c9c1bffd47b75add1e2b08f1a90322466fc7aaba006aca0/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6b6977696c616e2f7068702d617263686976652f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c617426636f6c6f72413d313831383142)](https://github.com/kiwilan/php-archive/actions/workflows/run-tests.yml)[![codecov](https://camo.githubusercontent.com/d76f75c41c664cd357b2da7e24d59c11f5828d9727f103a2c53f51f27ad2a366/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f67682f6b6977696c616e2f7068702d617263686976652f6d61696e3f7374796c653d666c617426636f6c6f72413d31383138314226636f6c6f72423d373737424234)](https://codecov.io/gh/kiwilan/php-archive)

PHP package to handle archives (`.zip`, `.rar`, `.tar`, `.7z`, `.pdf`) with unified API and hybrid solution (native/`p7zip`), designed to works with EPUB and CBA (`.cbz`, `.cbr`, `.cb7`, `.cbt`).

Supports Linux, macOS and Windows.

Warning

For some formats (`.rar` and `.7z`) [`rar` PHP extension](https://github.com/cataphract/php-rar) or [p7zip](https://www.7-zip.org/) binary could be necessary, see [Requirements](#requirements).

Requirements
------------

[](#requirements)

- **PHP version** &gt;= *8.1*
- **PHP extensions**:
    - [`zip`](https://www.php.net/manual/en/book.zip.php) (native, optional) for `.ZIP`, `.EPUB`, `.CBZ` archives
    - [`fileinfo`](https://www.php.net/manual/en/book.fileinfo.php) (native, optional) for better file detection
    - [`rar`](https://www.php.net/manual/en/book.rar.php) (optional) for `.RAR`, `.CBR` archives
    - [`imagick`](https://www.php.net/manual/en/book.imagick.php) (optional) for `.PDF`
    - [`bz2`](https://www.php.net/manual/en/book.bzip2.php) (optional) for `.BZ2` archives

TypeSupportedRequirementUses`.zip`, `.epub`, `.cbz`✅N/AUses [`zip` extension](https://www.php.net/manual/en/book.zip.php)`.tar`, `.tar.gz`, `.cbt`✅N/AUses [`phar` extension](https://www.php.net/manual/en/book.phar.php)\*`.rar`, `.cbr`✅[`rar` PHP extension](https://github.com/cataphract/php-rar) or [`p7zip`](https://www.7-zip.org/) binaryPHP `rar` or `p7zip``.7z`, `.cb7`✅[`p7zip`](https://www.7-zip.org/) binary`p7zip` binary`.pdf`✅Optional (for extraction) [`imagick` PHP extension](https://github.com/Imagick/imagick)[`smalot/pdfparser`](https://github.com/smalot/pdfparser)\*: for `.tar` archives with password, `.7z` will be used because extension don't support password.

Note

Here you can read some installation guides for dependencies

- [`p7zip` guide](https://gist.github.com/ewilan-riviere/85d657f9283fa6af255531d97da5d71d)
- [`rar` PHP extension guide](https://gist.github.com/ewilan-riviere/3f4efd752905abe24fd1cd44412d9db9#winrar)
- [`imagick` PHP extension guide](https://gist.github.com/ewilan-riviere/3f4efd752905abe24fd1cd44412d9db9#imagemagick)

Warning

- **On macOS**, for `.rar` extract, you have to [install `rar` binary](https://gist.github.com/ewilan-riviere/85d657f9283fa6af255531d97da5d71d#macos) to extract files, `p7zip` not support `.rar` extraction.
- **On Windows**, for `.pdf` extract, [`imagick` PHP extension](https://github.com/Imagick/imagick) have to work but **my tests failed on this feature**. So to extract PDF pages I advice to use [WSL](https://learn.microsoft.com/en-us/windows/wsl/install).

If you want more information, you can read section [**About**](#about).

Features
--------

[](#features)

- List files as `ArchiveItem` array
    - With `getFileItems()` method: list of files
    - With `getFileItem(string $path)` method: file corresponding to `path` property
    - With `getFirst()` method: first file
    - With `getLast()` method: last file
    - With `find()` method: find first file that match with `path` property
    - With `filter()` method: find all files that match with `path` property
- Content of file
    - With `getContents()` method: content of file as string (useful for images)
    - With `getText()` method: content of text file (binaries files return `null`)
- Extract files
    - With `extract()` method: extract files to directory
    - With `extractAll()` method: extract all files to directory
- Stat of archive corresponding to [`stat`](https://www.php.net/manual/en/function.stat.php)
- PDF metadata: `getTitle()`, `getAuthor()`, `getSubject()`, `getCreator()`, `getCreationDate()`, `getModDate()`, `getPages()`,
- Count files
- Create or edit archives, only with `.zip` format
    - With `make()` method: create or edit archive
    - With `addFiles()` method: add files to archive
    - With `addFromString()` method: add string to archive
    - With `addDirectory()` and `addDirectories()` methods: add directories to archive
    - With `save()` method: save archive

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require kiwilan/php-archive
```

Usage
-----

[](#usage)

### Read and extract

[](#read-and-extract)

With archive file (`.zip`, `.rar`, `.tar`, `.7z`, `epub`, `cbz`, `cbr`, `cb7`, `cbt`, `tar.gz`, `.pdf`).

```
$archive = Archive::read('path/to/archive.zip');

$files = $archive->getFileItems(); // ArchiveItem[]
$count = $archive->getCount(); // 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->getContents($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::read('path/to/file.pdf');

$pdf = $archive->getPdf(); // Metadata of PDF

$content = $archive->getContents($archive->getFirst()); // PDF page as image
$text = $archive->getText($archive->getFirst()); // PDF page as text
```

### Read from string

[](#read-from-string)

You can read archive from string with `readFromString` method.

```
$archive = Archive::readFromString($string);
```

This method will try to detect the format of the archive from the string. If you have an error, you can use `readFromString` method with third argument to specify the format of the archive.

```
$archive = Archive::readFromString($string, extension: 'zip');
```

### Password protected

[](#password-protected)

You can read password protected archives with `read` or `readFromString` method.

Warning

Works only with archives and not with PDF files.

```
$archive = Archive::read('path/to/password-protected-archive.zip', 'password');
```

### Override binary path

[](#override-binary-path)

For `p7zip` binary, you can override the path with `overrideBinaryPath` method.

```
$archive = Archive::read($path)->overrideBinaryPath('/opt/homebrew/bin/7z');
```

### Stat

[](#stat)

From `stat` PHP function:

> Gives information about a file

```
$archive = Archive::read('path/to/file.zip');
$stat = $archive->stat();

$stat->getPath(); // Path of file
$stat->getDeviceNumber(); // Device number
$stat->getInodeNumber(); // Inode number
$stat->getInodeProtectionMode(); // Inode protection mode
$stat->getNumberOfLinks(); // Number of links
$stat->getUserId(); // User ID
$stat->getGroupId(); // Group ID
$stat->getDeviceType(); // Device type
$stat->getSize(); // Size of file
$stat->getLastAccessAt(); // Last access time
$stat->getCreatedAt(); // Creation time
$stat->getModifiedAt(); // Last modification time
$stat->getBlockSize(); // Block size
$stat->getNumberOfBlocks(); // Number of blocks
$stat->getStatus(); // Status
```

### Create

[](#create)

You can create archive with method `Archive::make` method.

Works only with `.zip` archives.

```
$archive = Archive::make('path/to/archive.zip');
$files = [
    'path/to/file/in/archive-file1.txt' => 'path/to/real-file1.txt',
    'path/to/file/in/archive-file2.txt' => 'path/to/real-file2.txt',
    'path/to/file/in/archive-file3.txt' => 'path/to/real-file3.txt',
];

foreach ($files as $pathInArchive => $pathToRealFile) {
    $archive->addFile($pathInArchive, $pathToRealFile);
}
$archive->addFromString('test.txt', 'Hello World!');
$archive->addDirectory('./directory', 'path/to/directory');
$archive->save();
```

### Edit

[](#edit)

You can edit archive with same method `Archive::make` method.

```
$archive = Archive::make('path/to/archive.zip');
$archive->addFromString('test.txt', 'Hello World!');
$archive->save();
```

Testing
-------

[](#testing)

```
composer test
```

About
-----

[](#about)

This package was inspired by [this excellent post](https://stackoverflow.com/a/39163620/11008206) on StackOverflow which make state of the art of PHP archive handling. The package [Gemorroj/Archive7z](https://github.com/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:

- `.zip` with [ZipArchive](https://www.php.net/manual/en/class.ziparchive.php)
- `.tar` with [PharData](https://www.php.net/manual/en/class.phardata.php)
- `.rar` with [RarArchive](https://www.php.net/manual/en/class.rararchive.php) if `rar` extension is installed
- `.7z` can't be handled with native PHP solution

TypeIs nativeSolutionZIP✅NativeTAR✅NativeRAR❌`rar` or `p7zip` binary7ZIP❌`p7zip` binaryPDF❌`smalot/pdfparser`### Why not full wrapper of `p7zip` binary?

[](#why-not-full-wrapper-of-p7zip-binary)

This solution is used by [Gemorroj/Archive7z](https://github.com/Gemorroj/Archive7z), and it works well. But another problem is the usage of the [`p7zip` fork](https://github.com/p7zip-project/p7zip) 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.

### Case of `rar`

[](#case-of-rar)

The [`rar` PHP extension](https://github.com/cataphract/php-rar) 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](https://gist.github.com/ewilan-riviere/3f4efd752905abe24fd1cd44412d9db9#winrar) 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).

But `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.

### Case of `7zip`

[](#case-of-7zip)

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](https://gist.github.com/ewilan-riviere/85d657f9283fa6af255531d97da5d71d) if you want to install it.

### Case of `pdf`

[](#case-of-pdf)

PHP can't handle `.pdf` archives natively, so I choose to use `smalot/pdfparser` package, embedded in this package. To extract pages as images, you have to install [`imagick` extension](https://github.com/Imagick/imagick) you could read [this guide](https://gist.github.com/ewilan-riviere/3f4efd752905abe24fd1cd44412d9db9#imagemagick) if you want to install it.

### eBooks and comics

[](#ebooks-and-comics)

This package can handle `.epub`, `.cbz`, `.cbr`, `.cb7`, `.cbt` archives, it's depends on the extension, check [requirements](#requirements) section.

### More

[](#more)

Alternatives:

- [Gemorroj/Archive7z](https://github.com/Gemorroj/Archive7z): handle many archives with [p7zip-project/p7zip](https://github.com/p7zip-project/p7zip) binary
- [splitbrain/php-archive](https://github.com/splitbrain/php-archive): native PHP solution to handle `.zip` and `.tar` archives
- [maennchen/ZipStream-PHP](https://github.com/maennchen/ZipStream-PHP): A fast and simple streaming zip file downloader for PHP. Using this library will save you from having to write the Zip to disk.

Documentation:

- List files in `.7z`, `.rar` and `.tar` archives using PHP:
- Compression and Archive Extensions:

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Credits
-------

[](#credits)

- [Ewilan Riviere](https://github.com/ewilan-riviere)
- [All Contributors](../../contributors)
- [spatie](https://github.com/spatie) for `spatie/package-skeleton-php` and `spatie/temporary-directory`
- [`smalot/pdfparser`](https://github.com/smalot/pdfparser) for PDF parser
- [`7-zip`](https://www.7-zip.org/) for `p7zip` binary

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

[![](https://user-images.githubusercontent.com/48261459/201463225-0a5a084e-df15-4b11-b1d2-40fafd3555cf.svg)](https://github.com/kiwilan)

###  Health Score

49

—

FairBetter than 95% of packages

Maintenance64

Regular maintenance activity

Popularity37

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 92.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~33 days

Recently: every ~177 days

Total

28

Last Release

260d ago

Major Versions

0.1.1 → 1.0.02023-03-30

1.5.12 → 2.0.02023-08-08

### Community

Maintainers

![](https://www.gravatar.com/avatar/58eb34eac9af07c3352e83060e472e1c280838ebe0568692561c00a2cfde9e57?d=identicon)[ewilan-riviere](/maintainers/ewilan-riviere)

---

Top Contributors

[![ewilan-riviere](https://avatars.githubusercontent.com/u/48261459?v=4)](https://github.com/ewilan-riviere "ewilan-riviere (441 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (18 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (14 commits)")[![onigoetz](https://avatars.githubusercontent.com/u/309594?v=4)](https://github.com/onigoetz "onigoetz (4 commits)")

---

Tags

7z7ziparchivecb7cbacbrcbtcbzebookepubpdfphprartarzipphppdfarchivetarzipebook7zrar7zipepubcbrcbtcbzcb7

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/kiwilan-php-archive/health.svg)

```
[![Health](https://phpackages.com/badges/kiwilan-php-archive/health.svg)](https://phpackages.com/packages/kiwilan-php-archive)
```

###  Alternatives

[kiwilan/php-ebook

PHP package to read metadata and extract covers from eBooks, comics and audiobooks.

3414.6k1](/packages/kiwilan-php-ebook)[wapmorgan/unified-archive

UnifiedArchive - an archive manager with unified interface of working with all popular archive formats (zip/7z/rar/gz/bz2/xz/cab/tar/tar.gz/tar.bz2/tar.x/tar.Z/...) for PHP with ability for listing, reading, extracting and creation + built-in console archive manager.

2791.6M7](/packages/wapmorgan-unified-archive)[raulfraile/distill

Smart compressed files extractor

228190.4k9](/packages/raulfraile-distill)[easybook/easybook

Book publishing application

76011.5k](/packages/easybook-easybook)[barracudanetworks/archivestream-php

A library for dynamically streaming dynamic tar or zip files without the need to have the complete file stored on the server.

77192.2k1](/packages/barracudanetworks-archivestream-php)[pontedilana/php-weasyprint

PHP library allowing PDF generation from an url or a html page. Wrapper for Kozea/WeasyPrint.

761.1M7](/packages/pontedilana-php-weasyprint)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
