`PhpZip` is a php-library for extended work with ZIP-archives.
[![Packagist Version](https://img.shields.io/packagist/v/nelexa/zip.svg)](https://packagist.org/packages/nelexa/zip)
[![Packagist Downloads](https://img.shields.io/packagist/dt/nelexa/zip.svg?color=%23ff007f)](https://packagist.org/packages/nelexa/zip)
[![Code Coverage](https://scrutinizer-ci.com/g/Ne-Lexa/php-zip/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/Ne-Lexa/php-zip/?branch=master)
[![Build Status](https://github.com/Ne-Lexa/php-zip/workflows/build/badge.svg)](https://github.com/Ne-Lexa/php-zip/actions)
[![License](https://img.shields.io/packagist/l/nelexa/zip.svg)](https://github.com/Ne-Lexa/php-zip/blob/master/LICENSE)
[Russian Documentation](README.RU.md)
### Versions & Dependencies
| Version | PHP | Documentation |
| ------------------- | ---------- | -------------------------------------------------------------------- |
| ^4.0 (master) | ^7.4\|^8.0 | current |
| ^3.0 | ^5.5\|^7.0 | [Docs v3.3](https://github.com/Ne-Lexa/php-zip/blob/3.3.3/README.md) |
Table of contents
-----------------
- [Features](#features)
- [Requirements](#requirements)
- [Installation](#installation)
- [Examples](#examples)
- [Glossary](#glossary)
- [Documentation](#documentation)
+ [Overview of methods of the class `\PhpZip\ZipFile`](#overview-of-methods-of-the-class-phpzipzipfile)
+ [Creation/Opening of ZIP-archive](#creationopening-of-zip-archive)
+ [Reading entries from the archive](#reading-entries-from-the-archive)
+ [Iterating entries](#iterating-entries)
+ [Getting information about entries](#getting-information-about-entries)
+ [Adding entries to the archive](#adding-entries-to-the-archive)
+ [Deleting entries from the archive](#deleting-entries-from-the-archive)
+ [Working with entries and archive](#working-with-entries-and-archive)
+ [Working with passwords](#working-with-passwords)
+ [Undo changes](#undo-changes)
+ [Saving a file or output to a browser](#saving-a-file-or-output-to-a-browser)
+ [Closing the archive](#closing-the-archive)
- [Running the tests](#running-the-tests)
- [Changelog](#changelog)
- [Upgrade](#upgrade)
+ [Upgrade version 3 to version 4](#upgrade-version-3-to-version-4)
+ [Upgrade version 2 to version 3](#upgrade-version-2-to-version-3)
### Features
- Opening and unzipping zip files.
- Creating ZIP-archives.
- Modifying ZIP archives.
- Pure php (not require extension `php-zip` and class `\ZipArchive`).
- It supports saving the archive to a file, outputting the archive to the browser, or outputting it as a string without saving it to a file.
- Archival comments and comments of individual entry are supported.
- Get information about each entry in the archive.
- Only the following compression methods are supported:
+ No compressed (Stored).
+ Deflate compression.
+ BZIP2 compression with the extension `php-bz2`.
- Support for `ZIP64` (file size is more than 4 GB or the number of entries in the archive is more than 65535).
- Working with passwords
> **Attention!**
>
> For 32-bit systems, the `Traditional PKWARE Encryption (ZipCrypto)` encryption method is not currently supported.
> Use the encryption method `WinZIP AES Encryption`, whenever possible.
+ Set the password to read the archive for all entries or only for some.
+ Change the password for the archive, including for individual entries.
+ Delete the archive password for all or individual entries.
+ Set the password and/or the encryption method, both for all, and for individual entries in the archive.
+ Set different passwords and encryption methods for different entries.
+ Delete the password for all or some entries.
+ Support `Traditional PKWARE Encryption (ZipCrypto)` and `WinZIP AES Encryption` encryption methods.
+ Set the encryption method for all or individual entries in the archive.
### Requirements
- `PHP` >= 7.4 or `PHP` >= 8.0 (preferably 64-bit).
- Optional php-extension `bzip2` for BZIP2 compression.
- Optional php-extension `openssl` for `WinZip Aes Encryption` support.
### Installation
`composer require nelexa/zip`
Latest stable version: [![Latest Stable Version](https://poser.pugx.org/nelexa/zip/v/stable)](https://packagist.org/packages/nelexa/zip)
### Examples
```php
// create new archive
$zipFile = new \PhpZip\ZipFile();
try{
$zipFile
->addFromString('zip/entry/filename', 'Is file content') // add an entry from the string
->addFile('/path/to/file', 'data/tofile') // add an entry from the file
->addDir(__DIR__, 'to/path/') // add files from the directory
->saveAsFile($outputFilename) // save the archive to a file
->close(); // close archive
// open archive, extract, add files, set password and output to browser.
$zipFile
->openFile($outputFilename) // open archive from file
->extractTo($outputDirExtract) // extract files to the specified directory
->deleteFromRegex('~^\.~') // delete all hidden (Unix) files
->addFromString('dir/file.txt', 'Test file') // add a new entry from the string
->setPassword('password') // set password for all entries
->outputAsAttachment('library.jar'); // output to the browser without saving to a file
}
catch(\PhpZip\Exception\ZipException $e){
// handle exception
}
finally{
$zipFile->close();
}
```
Other examples can be found in the `tests/` folder
### Glossary
**Zip Entry** - file or folder in a ZIP-archive. Each entry in the archive has certain properties, for example: file name, compression method, encryption method, file size before compression, file size after compression, CRC32 and others.
### Documentation:
#### Overview of methods of the class `\PhpZip\ZipFile`
- [ZipFile::__construct](#zipfile__construct) - initializes the ZIP archive.
- [ZipFile::addAll](#zipfileaddall) - adds all entries from an array.
- [ZipFile::addDir](#zipfileadddir) - adds files to the archive from the directory on the specified path without subdirectories.
- [ZipFile::addDirRecursive](#zipfileadddirrecursive) - adds files to the archive from the directory on the specified path with subdirectories.
- [ZipFile::addEmptyDir](#zipfileaddemptydir) - add a new directory.
- [ZipFile::addFile](#zipfileaddfile) - adds a file to a ZIP archive from the given path.
- [ZipFile::addSplFile](#zipfileaddsplfile) - adds a `\SplFileInfo` to a ZIP archive.
- [ZipFile::addFromFinder](#zipfileaddfromfinder) - adds files from the `Symfony\Component\Finder\Finder` to a ZIP archive.
- [ZipFile::addFilesFromIterator](#zipfileaddfilesfromiterator) - adds files from the iterator of directories.
- [ZipFile::addFilesFromGlob](#zipfileaddfilesfromglob) - adds files from a directory by glob pattern without subdirectories.
- [ZipFile::addFilesFromGlobRecursive](#zipfileaddfilesfromglobrecursive) - adds files from a directory by glob pattern with subdirectories.
- [ZipFile::addFilesFromRegex](#zipfileaddfilesfromregex) - adds files from a directory by PCRE pattern without subdirectories.
- [ZipFile::addFilesFromRegexRecursive](#zipfileaddfilesfromregexrecursive) - adds files from a directory by PCRE pattern with subdirectories.
- [ZipFile::addFromStream](#zipfileaddfromstream) - adds an entry from the stream to the ZIP archive.
- [ZipFile::addFromString](#zipfileaddfromstring) - adds a file to a ZIP archive using its contents.
- [ZipFile::close](#zipfileclose) - close the archive.
- [ZipFile::count](#zipfilecount) - returns the number of entries in the archive.
- [ZipFile::deleteFromName](#zipfiledeletefromname) - deletes an entry in the archive using its name.
- [ZipFile::deleteFromGlob](#zipfiledeletefromglob) - deletes an entries in the archive using glob pattern.
- [ZipFile::delete