PHPackages                             glucnac/ziparchivemanager - 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. [File &amp; Storage](/categories/file-storage)
4. /
5. glucnac/ziparchivemanager

ActiveLibrary[File &amp; Storage](/categories/file-storage)

glucnac/ziparchivemanager
=========================

A simple wrapper around PHP's ZipArchive class that provides a more object-oriented interface to make it easier to create, extract, and modify zip archives.

v2.0.0(1y ago)07MITPHPPHP &gt;=8.1

Since May 17Pushed 1y ago1 watchersCompare

[ Source](https://github.com/GlucNAc/ZipArchiveManager)[ Packagist](https://packagist.org/packages/glucnac/ziparchivemanager)[ Docs](https://github.com/GlucNAc/ZipArchiveManager)[ RSS](/packages/glucnac-ziparchivemanager/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (10)Versions (4)Used By (0)

GlucNAc/ZipArchiveManager
=========================

[](#glucnacziparchivemanager)

 **A simple wrapper around PHP's native [ZipArchive](https://www.php.net/manual/en/class.ziparchive.php), to make it easier to work with.**

 [![Source Code](https://camo.githubusercontent.com/c5ba489dd858d26110921c45eb9a2b1b440e01598d36027f4a299263d1911edc/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f736f757263652d476c75634e41632f5a6970417263686976654d616e616765722d626c75652e7376673f7374796c653d666c61742d737175617265)](https://github.com/GlucNAc/ZipArchiveManager) [![Download Package](https://camo.githubusercontent.com/5226b5fa220333a67fab360f58d2fef7cc911506fcbebb4af5ae016510c46136/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f476c75634e41632f5a6970417263686976654d616e616765722e7376673f7374796c653d666c61742d737175617265266c6162656c3d72656c65617365)](https://packagist.org/packages/GlucNAc/ZipArchiveManager) [![PHP Programming Language](https://camo.githubusercontent.com/23691852bbbff32f0439b47bd4afff4b901b776c52c6aefd84919c74a06b2654/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f476c75634e41632f5a6970417263686976654d616e616765722e7376673f7374796c653d666c61742d73717561726526636f6c6f72423d253233383839324246)](https://php.net) [![Read License](https://camo.githubusercontent.com/1f3812057024b7511a6299d96486cf720d0946176e951537f7849892be1f740e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f476c75634e41632f5a6970417263686976654d616e616765722e7376673f7374796c653d666c61742d737175617265)](https://github.com/GlucNAc/ZipArchiveManager/blob/master/LICENSE) [![Build Status](https://camo.githubusercontent.com/0ac00b07eac13d648825a2061e2adb38a7f406f7ec5952e8285b809baab8f6ea/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f476c75634e41632f5a6970417263686976654d616e616765722f636f6e74696e756f75732d696e746567726174696f6e2e796d6c3f6272616e63683d6d6173746572267374796c653d666c61742d737175617265266c6f676f3d676974687562)](https://github.com/GlucNAc/ZipArchiveManager/actions/workflows/continuous-integration.yml) [![Code coverage badge](https://camo.githubusercontent.com/bbfafd2b27a0b3b45aca5cf003342a50a24aa1669832448509acecf65738a201/68747470733a2f2f636f6465636f762e696f2f67682f476c75634e41632f5a6970417263686976654d616e616765722f67726170682f62616467652e7376673f746f6b656e3d53334130584a45564e4d)](https://codecov.io/gh/GlucNAc/ZipArchiveManager)

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

[](#installation)

Install this package as a dependency using [Composer](https://getcomposer.org).

```
composer require glucnac/ziparchivemanager
```

Usage
-----

[](#usage)

The GlucNAc/ZipArchiveManager provides a more object-oriented interface to work with PHP's native [ZipArchive](https://www.php.net/manual/en/class.ziparchive.php), making it easier to create, extract, and modify zip archives.

### Creating a new ZipArchive

[](#creating-a-new-ziparchive)

To create a new zip archive, you can use the `ZipArchiveBuilder` class. The `ZipArchiveBuilder` requires a `ZipArchiveManager` object to manage the storage of the archive. The `ZipArchiveBuilder` class provides a fluent interface to add files to the archive.

```
use GlucNAc\ZipArchiveManager\ZipArchiveBuilder;
use GlucNAc\ZipArchiveManager\ZipArchiveManager;

$zipArchiveManager = new ZipArchiveManager('/path/to/storage/archive');
$zipArchiveBuilder = new ZipArchiveBuilder($zipArchiveManager);

$zipArchive = $zipArchiveBuilder
    ->new('test.zip')
    ->addFiles([
        '/path/to/file1.txt',
        '/path/to/dir/file2.txt',
    ])
    ->addFile('/path/to/file3.txt')
    ->build();
```

Now the archive exists at `/path/to/storage/archive/test.zip` and will have this structure:

```
test.zip
├── /path/to/file1.txt
├── /path/to/file3.txt
└── /path/to/dir
             └── file2.txt

```

This can also be done quickly with the `ZipArchiveBuilder::buildWithFiles` method:

```
use GlucNAc\ZipArchiveManager\ZipArchiveManager;
use GlucNAc\ZipArchiveManager\ZipArchiveBuilder;

$zipArchiveManager = new ZipArchiveManager('/path/to/storage/archive');
$zipArchiveBuilder = new ZipArchiveBuilder($zipArchiveManager);

$zipArchive = $zipArchiveBuilder->buildWithFiles('test.zip', [
    '/path/to/file1.txt',
    '/path/to/dir/file2.txt',
    '/path/to/file3.txt',
]);
```

#### Customizing the file structure in the archive

[](#customizing-the-file-structure-in-the-archive)

By default, the archive structure will mirror the structure of the files. If you want to change the structure of the files in the archive, you can use an associative array where the keys are the paths to the files and the values are the paths to the files in the archive:

```
use GlucNAc\ZipArchiveManager\ZipArchiveManager;
use GlucNAc\ZipArchiveManager\ZipArchiveBuilder;

$zipArchiveManager = new ZipArchiveManager('/path/to/storage/archive');
$zipArchiveBuilder = new ZipArchiveBuilder($zipArchiveManager);

$zipArchive = $zipArchiveBuilder->buildWithFiles('test.zip', [
    '/path/to/file1.txt' => 'file1.txt',
    '/path/to/dir/file2.txt' => 'dir/file2.txt',
    '/path/to/file3.txt' => 'file3.txt',
]);
```

Now the archive exists at `/path/to/storage/archive/test.zip`and will have this structure:

```
test.zip
├── file1.txt
├── file3.txt
└── dir
    └── file2.txt

```

#### Adding files from a directory

[](#adding-files-from-a-directory)

Given the following directory structure:

```
/path/to
├── file1.txt
├── file3.txt
└── dir
    └── file2.txt

```

If you want to create an archive with all the files in the `/path/to` directory, while keeping the structure of the files in the archive, you can use the `ZipArchiveBuilder::addFilesFromPath` method:

```
use GlucNAc\ZipArchiveManager\ZipArchiveBuilder;
use GlucNAc\ZipArchiveManager\ZipArchiveManager;

$zipArchiveManager = new ZipArchiveManager('/path/to/storage/archive');
$zipArchiveBuilder = new ZipArchiveBuilder($zipArchiveManager);

$zipArchive = $zipArchiveBuilder->buildWithFiles(
    'test.zip',
    ArchivableFileManager::getArchivableFilesFromPath('/path/to'),
);
```

The `ArchivableFileManager::getArchivableFilesFromPath` method returns an array of `ArchivableFile` objects, which implement the `ArchivableFileInterface` interface, and where the path of the files in the archive will be relative to the path passed to the method (`/path/to` in this case).

This works because `ZipArchiveBuilder::buildWithFiles`, `ZipArchiveBuilder::addFiles` and `ZipArchiveBuilder::addFile` methods also accept an array of `ArchivableFileInterface` objects (in addition to file paths).

See the [ArchivableFile](#ArchivableFile) section for more information about `ArchivableFileInterface` and `ArchivableFile` objects.

#### Keeping the archive open

[](#keeping-the-archive-open)

By default, `build` methods will close the archive after building it. If you want to keep the archive open, you can pass `false` as the first argument to the `build` method:

```
use GlucNAc\ZipArchiveManager\ZipArchiveManager;
use GlucNAc\ZipArchiveManager\ZipArchiveBuilder;

$zipArchiveManager = new ZipArchiveManager('/path/to/storage/archive');
$zipArchiveBuilder = new ZipArchiveBuilder($zipArchiveManager);

$zipArchive = $zipArchiveBuilder
    ->new('test.zip')
    ->addFiles([
        '/path/to/file1.txt',
        '/path/to/dir/file2.txt',
    ])
    ->addFile('/path/to/file3.txt')
    ->build(false);

// Do something with the archive

// Close the archive: this will save the archive to the storage
$zipArchiveManager->close($zipArchive);
```

### Adding files to an existing zip archive

[](#adding-files-to-an-existing-zip-archive)

To add files to an existing zip archive, you just have to open the archive with the `ZipArchiveManager::open` method and use methods described in the [Creating a new ZipArchive](#Creating-a-new-ZipArchive) section:

```
use GlucNAc\ZipArchiveManager\ZipArchiveManager;

$zipArchiveManager = new ZipArchiveManager('/path/to/storage/archive');

// Assume the archive exists at /path/to/storage/archive/test.zip
$zipArchive = $zipArchiveManager->open('test.zip');
```

### Extracting files from a ZipArchive

[](#extracting-files-from-a-ziparchive)

To extract files from a zip archive, simply use the `ZipArchiveManager::extractFiles` method:

```
use GlucNAc\ZipArchiveManager\ZipArchiveManager;

$zipArchiveManager = new ZipArchiveManager('/path/to/storage/archive');

// Assume the archive exists at /path/to/storage/archive/test.zip
$zipArchive = $zipArchiveManager->open('test.zip');

$zipArchiveManager->extractFiles($zipArchive, '/path/to/extracted/files/dir');
```

### ArchivableFile

[](#archivablefile)

For this section, let's consider the following directory structure:

```
/path/to
├── file1.txt
├── file3.txt
└── dir
    └── file2.txt

```

You may have noticed that `ZipArchiveManager` uses the `ArchivableFile` class to represent files that can be added to a zip archive. More precisely, `ZipArchiveManager` expects an object that implements the [ArchivableFileInterface](src/File/ArchivableFileInterface.php). This interface defines the methods that an object must implement to be considered an archivable file. The `ArchivableFile` class provided by this package implements this interface and provides a simple way to work with files that can be added to a zip archive.

```
interface ArchivableFileInterface
{
    public function getFullPath(): string;
    public function getFileName(): string;
    public function getExtension(): string;

    /**
     * This method is used to get the name of the file inside the archive. It can be useful to rename
     * the file on the fly, or to put it in a subdirectory by returning a relative path.
     */
    public function getEntryName(): string;
    public function setEntryName(string|null $entryName): static;
}

$archivableFile = new ArchivableFile('/path/to/file1.txt');

$archivableFile->getFullPath();  // /path/to/file1.txt
$archivableFile->getFileName();  // file1.txt
$archivableFile->getExtension(); // txt
$archivableFile->getEntryName(); // /path/to/file1.txt (by default, the entry name equals the full path)
```

You can create your own class that implements this interface, or you can use the `ArchivableFile` class provided by this package. To do so, you can use the `ArchivableFileManager::getArchivableFileFromPath` method to get an `ArchivableFile` object from a file path:

```
use GlucNAc\ZipArchiveManager\ArchivableFileManager;

$archivableFile = ArchivableFileManager::getArchivableFileFromPath('/path/to/file1.txt');
$archivableFile->setEntryName('file1.txt'); // This will be the path of the file in the archive
```

You can also use the `ArchivableFileManager::getArchivableFilesFromPath` method to get an array of `ArchivableFile` objects from a directory path:

```
use GlucNAc\ZipArchiveManager\ArchivableFileManager;

[$file1, $file2, $file3] = ArchivableFileManager::getArchivableFilesFromPath('/path/to');

// By default, when using the ArchivableFileManager::getArchivableFilesFromPath method,
// the entry name of the files will be relative to the path passed to the method.
$file1->getEntryName(); // file1.txt
$file2->getEntryName(); // dir/file2.txt
$file3->getEntryName(); // file3.txt
```

Internally, the `ArchivableFile` class uses the `SplFileInfo` class to represent files. You can also use the `SplFileInfoToArchivableFileTransformer` class to transform an `SplFileInfo` object into an `ArchivableFile` object:

```
use GlucNAc\ZipArchiveManager\ArchivableFile;

$splFileInfo = new SplFileInfo('/path/to/file.txt');

$archivableFile = SplFileInfoToArchivableFileTransformer::getArchivableFile($splFileInfo);
```

Contributing
------------

[](#contributing)

Contributions are welcome! To contribute, please familiarize yourself with [CONTRIBUTING.md](CONTRIBUTING.md).

Coordinated Disclosure
----------------------

[](#coordinated-disclosure)

Keeping user information safe and secure is a top priority, and we welcome the contribution of external security researchers. If you believe you've found a security issue in software that is maintained in this repository, please read [SECURITY.md](SECURITY.md) for instructions on submitting a vulnerability report.

Copyright and License
---------------------

[](#copyright-and-license)

GlucNAc/ZipArchiveManager is copyright © [GlucNAc](https://github.com/GlucNAc)and licensed for use under the terms of the MIT License (MIT). Please see [LICENSE](LICENSE) for more information.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance34

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~2 days

Total

2

Last Release

719d ago

Major Versions

v1.0.0 → v2.0.02024-05-20

### Community

Maintainers

![](https://www.gravatar.com/avatar/dd3b97d61b85c80668786bd955e5eb38f972f6f3a371e9a87fccce3f3244d394?d=identicon)[GlucNAc](/maintainers/GlucNAc)

---

Top Contributors

[![GlucNAc](https://avatars.githubusercontent.com/u/51360240?v=4)](https://github.com/GlucNAc "GlucNAc (1 commits)")

---

Tags

archivezipwrapper

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleECS

Type Coverage Yes

### Embed Badge

![Health badge](/badges/glucnac-ziparchivemanager/health.svg)

```
[![Health](https://phpackages.com/badges/glucnac-ziparchivemanager/health.svg)](https://phpackages.com/packages/glucnac-ziparchivemanager)
```

###  Alternatives

[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.4k5.6M648](/packages/sylius-sylius)[maennchen/zipstream-php

ZipStream is a library for dynamically streaming dynamic zip files from PHP without writing to the disk at all on the server.

1.9k286.3M147](/packages/maennchen-zipstream-php)[nelexa/zip

PhpZip is a php-library for extended work with ZIP-archives. Open, create, update, delete, extract and get info tool. Supports appending to existing ZIP files, WinZip AES encryption, Traditional PKWARE Encryption, BZIP2 compression, external file attributes and ZIP64 extensions. Alternative ZipArchive. It does not require php-zip extension.

4967.4M112](/packages/nelexa-zip)[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)[splitbrain/php-archive

Pure-PHP implementation to read and write TAR and ZIP archives

1061.4M20](/packages/splitbrain-php-archive)[phpzip/phpzip

Package to create and stream archives of compressed files in ZIP format with PHP 5.3+

124840.7k9](/packages/phpzip-phpzip)

PHPackages © 2026

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