`PhpZip`
========
`PhpZip` is a php-library for extended work with ZIP-archives.
[![Build Status](https://travis-ci.org/Ne-Lexa/php-zip.svg?branch=master)](https://travis-ci.org/Ne-Lexa/php-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)
[![Latest Stable Version](https://poser.pugx.org/nelexa/zip/v/stable)](https://packagist.org/packages/nelexa/zip)
[![Total Downloads](https://poser.pugx.org/nelexa/zip/downloads)](https://packagist.org/packages/nelexa/zip)
[![Minimum PHP Version](http://img.shields.io/badge/php-%3E%3D%205.5-8892BF.svg)](https://php.net/)
[![License](https://poser.pugx.org/nelexa/zip/license)](https://packagist.org/packages/nelexa/zip)
[Russian docu
mentation](README.RU.md)
Table of co
ntents
-----------------
- [Features](#Features)
- [Requirements](#Requirements)
- [Installation](#Installation)
- [Examples](#Examples)
- [Glossary](#Glossary)
- [docu
mentation](#docu
mentation)
+ [Overview of methods of the class `PhpZipZipFile`](#docu
mentation-Overview)
+ [Creation/Opening of ZIP-archive](#docu
mentation-Open-Zip-Archive)
+ [Reading entries from the archive](#docu
mentation-Open-Zip-Entries)
+ [Iterating entries](#docu
mentation-Zip-Iterate)
+ [Getting information a
bout entries](#docu
mentation-Zip-Info)
+ [Adding entries to the archive](#docu
mentation-Add-Zip-Entries)
+ [Deleting entries from the archive](#docu
mentation-Remove-Zip-Entries)
+ [Working with entries and archive](#docu
mentation-Entries)
+ [Working with passwords](#docu
mentation-Password)
+ [zipalign - alignment tool for Android (APK) files](#docu
mentation-ZipAlign-Usage)
+ [Undo changes](#docu
mentation-Unchanged)
+ [Saving a file or output to a browser](#docu
mentation-Save-Or-Output-Entries)
+ [Closing the archive](#docu
mentation-Close-Zip-Archive)
- [Running the tests](#Running-Tests)
- [Changelog](#Changelog)
- [Upgrade](#Upgrade)
+ [Upgrade version 2 to version 3.0](#Upgrade-v2-to-v3)
### <a name="Features"></a> 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 a
bout each entry in the archive.
- o
nly 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).
- Built-in support for aligning the archive to optimize Android packages (APK) [`zipalign`](https://developer.android.com/studio/command-line/zipalign.html).
- Working with passwords for PHP 5.5
> **Attention!**
>
> For 32-bit systems, the `Traditio
nal 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 o
nly for some.
+ Change the password for the archive, including for individual entries.
+ Delet
e 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 `Traditio
nal PKWARE Encryption (ZipCrypto)` and `WinZIP AES Encryption` encryption methods.
+ Set the encryption method for all or individual entries in the archive.
### <a name="Requirements"></a> Requirements
- `PHP` >= 5.5 (preferably 64-bit).
- Optio
nal php-extension `bzip2` for BZIP2 compression.
- Optio
nal php-extension `openssl` or `mcrypt` for `WinZip Aes Encryption` support.
### <a name="Installation"></a> 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)
### <a name="Examples"></a> Examples
```php
// create new archive
$zipFile = new PhpZipZipFile();
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
->delet
eFromRegex('~^.~') // delet
e all hidden (Unix) files
->addFromString('dir/file.txt', 'Test file') // add a new entry from the string
->setPasswor
d('password') // set password for all entries
->outputAsAttachment('library.jar'); // output to the browser without saving to a file
}
catch(PhpZipExceptio
nZipException $e){
// handle exception
}
finally{
$zipFile->close();
}
```
Other examples can be found in the `tests/` folder
### <a name="Glossary"></a> 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.
### <a name="docu
mentation"></a> docu
mentation:
#### <a name="docu
mentation-Overview"></a> Overview of methods of the class `PhpZipZipFile`
- [ZipFile::__construct](#docu
mentation-ZipFile-__construct) - initializes the ZIP archive.
- [ZipFile::addAll](#docu
mentation-ZipFile-addAll) - adds all entries from an array.
- [ZipFile::addDir](#docu
mentation-ZipFile-addDir) - adds files to the archive from the directory on the specified path without subdirectories.
- [ZipFile::addDirRecursive](#docu
mentation-ZipFile-addDirRecursive) - adds files to the archive from the directory on the specified path with subdirectories.
- [ZipFile::addEmptyDir](#docu
mentation-ZipFile-addEmptyDir) - add a new directory.
- [ZipFile::addFile](#docu
mentation-ZipFile-addFile) - adds a file to a ZIP archive from the given path.
- [ZipFile::addSplFile](#docu
mentation-ZipFile-addSplFile) - adds a `SplFileInfo` to a ZIP archive.
- [ZipFile::addFromFinder](#docu
mentation-ZipFile-addFromFinder) - adds files from the `SymfonyComponentFinderFinder` to a ZIP archive.
- [ZipFile::addFilesFromIterator](#docu
mentation-ZipFile-addFilesFromIterator) - adds files from the iterator of directories.
- [ZipFile::addFilesFromGlob](#docu
mentation-ZipFile-addFilesFromGlob) - adds files from a directory by glob pattern without subdirectories.
- [ZipFile::addFilesFromGlobRecursive](#docu
mentation-ZipFile-addFilesFromGlobRecursive) - adds files from a directory by glob pattern with subdirectories.
- [ZipFile::addFilesFromRegex](#docu
mentation-ZipFile-addFilesFromRegex) - adds files from a directory by PCRE pattern without subdirectories.
- [ZipFile::addFilesFromRegexRecursive](#docu
mentation-ZipFile-addFilesFromRegexRecursive) - adds files from a directory by PCRE pattern with subdirectories.
- [ZipFile::addFromStream](#docu
mentation-ZipFile-addFromStream) - adds a entry from the stream to the ZIP archive.
- [ZipFile::addFromString](#docu
mentation-ZipFile-addFromString) - adds a file to a ZIP archive using its contents.
- [ZipFile::close](#docu
mentation-ZipFile-close) - close the archive.
- [ZipFile::count](#docu
mentation-ZipFile-count) - returns the number of entries in the archive.