2017-03-13 08:33:45 +03:00
|
|
|
`PhpZip`
|
2017-03-10 08:23:57 +03:00
|
|
|
====================
|
|
|
|
`PhpZip` - php library for manipulating zip archives.
|
2016-09-08 19:18:53 +03:00
|
|
|
|
2017-03-15 10:42:46 +03:00
|
|
|
[](https://travis-ci.org/Ne-Lexa/php-zip)
|
2017-03-13 08:33:45 +03:00
|
|
|
[](https://packagist.org/packages/nelexa/zip)
|
|
|
|
[](https://packagist.org/packages/nelexa/zip)
|
2017-03-15 10:42:46 +03:00
|
|
|
[](https://php.net/)
|
2017-03-13 19:59:30 +03:00
|
|
|
[](https://codeclimate.com/github/Ne-Lexa/php-zip/coverage)
|
2017-03-13 08:33:45 +03:00
|
|
|
[](https://packagist.org/packages/nelexa/zip)
|
|
|
|
|
2017-03-13 19:59:30 +03:00
|
|
|
Table of contents
|
|
|
|
-----------------
|
|
|
|
- [Features](#Features)
|
|
|
|
- [Requirements](#Requirements)
|
|
|
|
- [Installation](#Installation)
|
|
|
|
- [Examples](#Examples)
|
|
|
|
- [Documentation](#Documentation)
|
|
|
|
+ [Open Zip Archive](#Documentation-Open-Zip-Archive)
|
|
|
|
+ [Get Zip Entries](#Documentation-Open-Zip-Entries)
|
|
|
|
+ [Add Zip Entries](#Documentation-Add-Zip-Entries)
|
2017-03-15 10:42:46 +03:00
|
|
|
+ [ZipAlign Usage](#Documentation-ZipAlign-Usage)
|
|
|
|
+ [Save Zip File or Output](#Documentation-Save-Or-Output-Entries)
|
|
|
|
+ [Close Zip Archive](#Documentation-Close-Zip-Archive)
|
|
|
|
- [Running Tests]("#Running-Tests")
|
|
|
|
- [Upgrade version 2 to version 3]("#Upgrade")
|
2017-03-13 19:59:30 +03:00
|
|
|
|
|
|
|
### <a name="Features"></a> Features
|
2017-03-10 08:23:57 +03:00
|
|
|
- Opening and unzipping zip files.
|
|
|
|
- Create zip files.
|
|
|
|
- Update zip files.
|
|
|
|
- Pure php (not require extension `php-zip` and class `\ZipArchive`).
|
|
|
|
- Output the modified archive as a string or output to the browser without saving the result to disk.
|
|
|
|
- Support archive comment and entries comments.
|
|
|
|
- Get info of zip entries.
|
2017-03-15 10:42:46 +03:00
|
|
|
- Support zip password for PHP 5.5, include update and remove password.
|
2017-03-10 08:23:57 +03:00
|
|
|
- Support encryption method `Traditional PKWARE Encryption (ZipCrypto)` and `WinZIP AES Encryption`.
|
|
|
|
- Support `ZIP64` (size > 4 GiB or files > 65535 in a .ZIP archive).
|
|
|
|
- Support archive alignment functional [`zipalign`](https://developer.android.com/studio/command-line/zipalign.html).
|
2016-09-26 12:04:38 +03:00
|
|
|
|
2017-03-13 19:59:30 +03:00
|
|
|
### <a name="Requirements"></a> Requirements
|
2017-03-13 09:13:19 +03:00
|
|
|
- `PHP` >= 5.5 (64 bit)
|
2016-09-26 12:04:38 +03:00
|
|
|
- Optional php-extension `bzip2` for BZIP2 compression.
|
|
|
|
- Optional php-extension `openssl` or `mcrypt` for `WinZip Aes Encryption` support.
|
|
|
|
|
2017-03-13 19:59:30 +03:00
|
|
|
### <a name="Installation"></a> Installation
|
2017-03-10 08:23:57 +03:00
|
|
|
`composer require nelexa/zip:^3.0`
|
|
|
|
|
2017-03-13 19:59:30 +03:00
|
|
|
### <a name="Examples"></a> Examples
|
2017-03-10 08:23:57 +03:00
|
|
|
```php
|
2017-03-13 19:59:30 +03:00
|
|
|
// create new archive
|
2017-03-10 08:23:57 +03:00
|
|
|
$zipFile = new \PhpZip\ZipFile();
|
2017-03-13 19:59:30 +03:00
|
|
|
$zipFile
|
|
|
|
->addFromString("zip/entry/filename", "Is file content")
|
|
|
|
->addFile("/path/to/file", "data/tofile")
|
|
|
|
->addDir(__DIR__, "to/path/")
|
|
|
|
->saveAsFile($outputFilename)
|
|
|
|
->close();
|
2017-03-10 08:23:57 +03:00
|
|
|
|
|
|
|
// open archive, extract, add files, set password and output to browser.
|
2017-03-13 19:59:30 +03:00
|
|
|
$zipFile
|
|
|
|
->openFile($outputFilename)
|
|
|
|
->extractTo($outputDirExtract)
|
|
|
|
->deleteFromRegex('~^\.~') // delete all hidden (Unix) files
|
|
|
|
->addFromString('dir/file.txt', 'Test file')
|
|
|
|
->withNewPassword('password')
|
|
|
|
->outputAsAttachment('library.jar');
|
2017-03-10 08:23:57 +03:00
|
|
|
```
|
|
|
|
Other examples can be found in the `tests/` folder
|
2016-09-26 12:04:38 +03:00
|
|
|
|
2017-03-15 10:42:46 +03:00
|
|
|
### <a name="Documentation"></a> Documentation:
|
|
|
|
#### <a name="Documentation-Open-Zip-Archive"></a> Open Zip Archive
|
2016-09-26 12:04:38 +03:00
|
|
|
Open zip archive from file.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2017-03-10 08:23:57 +03:00
|
|
|
$zipFile = new \PhpZip\ZipFile();
|
|
|
|
$zipFile->openFile($filename);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Open zip archive from data string.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2017-03-10 08:23:57 +03:00
|
|
|
$zipFile = new \PhpZip\ZipFile();
|
2017-03-13 19:59:30 +03:00
|
|
|
$zipFile->openFromString($stringContents);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Open zip archive from stream resource.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$stream = fopen($filename, 'rb');
|
2017-03-13 19:59:30 +03:00
|
|
|
|
|
|
|
$zipFile = new \PhpZip\ZipFile();
|
|
|
|
$zipFile->openFromStream($stream);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2017-03-15 10:42:46 +03:00
|
|
|
#### <a name="Documentation-Open-Zip-Entries"></a> Get Zip Entries
|
2016-09-26 12:04:38 +03:00
|
|
|
Get num entries.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$count = count($zipFile);
|
2017-03-13 19:59:30 +03:00
|
|
|
// or
|
|
|
|
$count = $zipFile->count();
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Get list files.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$listFiles = $zipFile->getListFiles();
|
2017-03-13 19:59:30 +03:00
|
|
|
|
|
|
|
// Example result:
|
|
|
|
//
|
|
|
|
// $listFiles = [
|
|
|
|
// 'info.txt',
|
|
|
|
// 'path/to/file.jpg',
|
|
|
|
// 'another path/'
|
|
|
|
// ];
|
|
|
|
```
|
|
|
|
Get entry contents.
|
|
|
|
```php
|
|
|
|
// $entryName = 'path/to/example-entry-name.txt';
|
|
|
|
|
|
|
|
$contents = $zipFile[$entryName];
|
|
|
|
```
|
|
|
|
Checks whether a entry exists.
|
|
|
|
```php
|
|
|
|
// $entryName = 'path/to/example-entry-name.txt';
|
|
|
|
|
|
|
|
$hasEntry = isset($zipFile[$entryName]);
|
|
|
|
```
|
|
|
|
Check whether the directory entry.
|
|
|
|
```php
|
|
|
|
// $entryName = 'path/to/';
|
|
|
|
|
|
|
|
$isDirectory = $zipFile->isDirectory($entryName);
|
|
|
|
```
|
|
|
|
Extract all files to directory.
|
|
|
|
```php
|
|
|
|
$zipFile->extractTo($directory);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2017-03-13 19:59:30 +03:00
|
|
|
Extract some files to directory.
|
|
|
|
```php
|
|
|
|
$extractOnlyFiles = [
|
|
|
|
"filename1",
|
|
|
|
"filename2",
|
|
|
|
"dir/dir/dir/"
|
|
|
|
];
|
|
|
|
$zipFile->extractTo($directory, $extractOnlyFiles);
|
|
|
|
```
|
|
|
|
Iterate zip entries.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
foreach($zipFile as $entryName => $dataContent){
|
|
|
|
echo "Entry: $entryName" . PHP_EOL;
|
|
|
|
echo "Data: $dataContent" . PHP_EOL;
|
|
|
|
echo "-----------------------------" . PHP_EOL;
|
|
|
|
}
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2017-03-13 19:59:30 +03:00
|
|
|
or
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$iterator = new \ArrayIterator($zipFile);
|
|
|
|
while ($iterator->valid())
|
|
|
|
{
|
|
|
|
$entryName = $iterator->key();
|
|
|
|
$dataContent = $iterator->current();
|
|
|
|
|
|
|
|
echo "Entry: $entryName" . PHP_EOL;
|
|
|
|
echo "Data: $dataContent" . PHP_EOL;
|
|
|
|
echo "-----------------------------" . PHP_EOL;
|
|
|
|
|
|
|
|
$iterator->next();
|
|
|
|
}
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Get comment archive.
|
|
|
|
```php
|
2017-03-13 19:59:30 +03:00
|
|
|
$commentArchive = $zipFile->getArchiveComment();
|
2016-09-26 12:04:38 +03:00
|
|
|
```
|
|
|
|
Get comment zip entry.
|
|
|
|
```php
|
|
|
|
$commentEntry = $zipFile->getEntryComment($entryName);
|
|
|
|
```
|
2017-03-13 19:59:30 +03:00
|
|
|
Set password for read encrypted entries.
|
|
|
|
```php
|
|
|
|
$zipFile->withReadPassword($password);
|
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Get entry info.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$zipInfo = $zipFile->getEntryInfo('file.txt');
|
2017-03-13 19:59:30 +03:00
|
|
|
|
2016-09-26 12:04:38 +03:00
|
|
|
echo $zipInfo . PHP_EOL;
|
2017-03-13 19:59:30 +03:00
|
|
|
|
|
|
|
// Output:
|
2016-10-14 18:08:00 +03:00
|
|
|
// ZipInfo {Path="file.txt", Size=9.77KB, Compressed size=2.04KB, Modified time=2016-09-24T19:25:10+03:00, Crc=0x4b5ab5c7, Method="Deflate", Attributes="-rw-r--r--", Platform="UNIX", Version=20}
|
2017-03-13 19:59:30 +03:00
|
|
|
|
2016-09-26 12:04:38 +03:00
|
|
|
print_r($zipInfo);
|
2017-03-13 19:59:30 +03:00
|
|
|
|
|
|
|
// Output:
|
|
|
|
// PhpZip\Model\ZipInfo Object
|
|
|
|
// (
|
|
|
|
// [path:PhpZip\Model\ZipInfo:private] => file.txt
|
|
|
|
// [folder:PhpZip\Model\ZipInfo:private] =>
|
|
|
|
// [size:PhpZip\Model\ZipInfo:private] => 10000
|
|
|
|
// [compressedSize:PhpZip\Model\ZipInfo:private] => 2086
|
|
|
|
// [mtime:PhpZip\Model\ZipInfo:private] => 1474734310
|
|
|
|
// [ctime:PhpZip\Model\ZipInfo:private] =>
|
|
|
|
// [atime:PhpZip\Model\ZipInfo:private] =>
|
|
|
|
// [encrypted:PhpZip\Model\ZipInfo:private] =>
|
|
|
|
// [comment:PhpZip\Model\ZipInfo:private] =>
|
|
|
|
// [crc:PhpZip\Model\ZipInfo:private] => 1264235975
|
|
|
|
// [method:PhpZip\Model\ZipInfo:private] => Deflate
|
|
|
|
// [platform:PhpZip\Model\ZipInfo:private] => UNIX
|
|
|
|
// [version:PhpZip\Model\ZipInfo:private] => 20
|
|
|
|
// [attributes:PhpZip\Model\ZipInfo:private] => -rw-r--r--
|
|
|
|
// )
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Get info for all entries.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$zipAllInfo = $zipFile->getAllInfo();
|
2017-03-13 19:59:30 +03:00
|
|
|
|
2016-09-26 12:04:38 +03:00
|
|
|
print_r($zipAllInfo);
|
2017-03-13 19:59:30 +03:00
|
|
|
|
2016-09-26 12:04:38 +03:00
|
|
|
//Array
|
|
|
|
//(
|
|
|
|
// [file.txt] => PhpZip\Model\ZipInfo Object
|
|
|
|
// (
|
|
|
|
// ...
|
|
|
|
// )
|
|
|
|
//
|
|
|
|
// [file2.txt] => PhpZip\Model\ZipInfo Object
|
|
|
|
// (
|
|
|
|
// ...
|
|
|
|
// )
|
|
|
|
//
|
|
|
|
// ...
|
|
|
|
//)
|
2017-03-13 19:59:30 +03:00
|
|
|
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2017-03-15 10:42:46 +03:00
|
|
|
#### <a name="Documentation-Add-Zip-Entries"></a> Add Zip Entries
|
2017-03-13 19:59:30 +03:00
|
|
|
Adding a file to the zip-archive.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2017-03-13 19:59:30 +03:00
|
|
|
// entry name is file basename.
|
|
|
|
$zipFile->addFile($filename);
|
2016-09-26 12:04:38 +03:00
|
|
|
// or
|
2017-03-13 19:59:30 +03:00
|
|
|
$zipFile->addFile($filename, null);
|
2016-09-26 12:04:38 +03:00
|
|
|
|
2017-03-13 19:59:30 +03:00
|
|
|
// with entry name
|
|
|
|
$zipFile->addFile($filename, $entryName);
|
2016-09-26 12:04:38 +03:00
|
|
|
// or
|
2017-03-13 19:59:30 +03:00
|
|
|
$zipFile[$entryName] = new \SplFileInfo($filename);
|
|
|
|
|
|
|
|
// with compression method
|
2017-03-15 10:42:46 +03:00
|
|
|
$zipFile->addFile($filename, $entryName, ZipFile::METHOD_DEFLATED); // Deflate compression
|
2017-03-13 19:59:30 +03:00
|
|
|
$zipFile->addFile($filename, $entryName, ZipFile::METHOD_STORED); // No compression
|
|
|
|
$zipFile->addFile($filename, null, ZipFile::METHOD_BZIP2); // BZIP2 compression
|
2016-09-26 12:04:38 +03:00
|
|
|
```
|
|
|
|
Add entry from string data.
|
|
|
|
```php
|
2017-03-13 19:59:30 +03:00
|
|
|
$zipFile[$entryName] = $data;
|
|
|
|
// or
|
|
|
|
$zipFile->addFromString($entryName, $data);
|
|
|
|
|
|
|
|
// with compression method
|
|
|
|
$zipFile->addFromString($entryName, $data, ZipFile::METHOD_DEFLATED); // Deflate compression
|
|
|
|
$zipFile->addFromString($entryName, $data, ZipFile::METHOD_STORED); // No compression
|
|
|
|
$zipFile->addFromString($entryName, $data, ZipFile::METHOD_BZIP2); // BZIP2 compression
|
2016-09-26 12:04:38 +03:00
|
|
|
```
|
|
|
|
Add entry from stream.
|
|
|
|
```php
|
2017-03-13 19:59:30 +03:00
|
|
|
// $stream = fopen(...);
|
|
|
|
|
|
|
|
$zipFile->addFromStream($stream, $entryName);
|
|
|
|
|
|
|
|
// with compression method
|
2017-03-15 10:42:46 +03:00
|
|
|
$zipFile->addFromStream($stream, $entryName, ZipFile::METHOD_DEFLATED); // Deflate compression
|
2017-03-13 19:59:30 +03:00
|
|
|
$zipFile->addFromStream($stream, $entryName, ZipFile::METHOD_STORED); // No compression
|
|
|
|
$zipFile->addFromStream($stream, $entryName, ZipFile::METHOD_BZIP2); // BZIP2 compression
|
2016-09-26 12:04:38 +03:00
|
|
|
```
|
|
|
|
Add empty dir
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2017-03-15 10:42:46 +03:00
|
|
|
// $dirName = "path/to/";
|
|
|
|
|
2017-03-13 19:59:30 +03:00
|
|
|
$zipFile->addEmptyDir($dirName);
|
2016-09-26 12:04:38 +03:00
|
|
|
// or
|
2017-03-13 19:59:30 +03:00
|
|
|
$zipFile[$dirName] = null;
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2017-03-13 19:59:30 +03:00
|
|
|
Add all entries form string contents.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2017-03-13 19:59:30 +03:00
|
|
|
$mapData = [
|
|
|
|
'file.txt' => 'file contents',
|
|
|
|
'path/to/file.txt' => 'another file contents',
|
|
|
|
'empty dir/' => null,
|
|
|
|
];
|
|
|
|
|
|
|
|
$zipFile->addAll($mapData);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2017-03-13 19:59:30 +03:00
|
|
|
Add a directory **not recursively** to the archive.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2017-03-13 19:59:30 +03:00
|
|
|
$zipFile->addDir($dirName);
|
|
|
|
|
|
|
|
// with entry path
|
|
|
|
$localPath = "to/path/";
|
|
|
|
$zipFile->addDir($dirName, $localPath);
|
|
|
|
|
|
|
|
// with compression method for all files
|
2017-03-15 10:42:46 +03:00
|
|
|
$zipFile->addDir($dirName, $localPath, ZipFile::METHOD_DEFLATED); // Deflate compression
|
2017-03-13 19:59:30 +03:00
|
|
|
$zipFile->addDir($dirName, $localPath, ZipFile::METHOD_STORED); // No compression
|
|
|
|
$zipFile->addDir($dirName, $localPath, ZipFile::METHOD_BZIP2); // BZIP2 compression
|
2017-03-15 10:42:46 +03:00
|
|
|
```
|
|
|
|
Add a directory **recursively** to the archive.
|
|
|
|
```php
|
|
|
|
$zipFile->addDirRecursive($dirName);
|
|
|
|
|
|
|
|
// with entry path
|
|
|
|
$localPath = "to/path/";
|
|
|
|
$zipFile->addDirRecursive($dirName, $localPath);
|
2017-03-13 19:59:30 +03:00
|
|
|
|
2017-03-15 10:42:46 +03:00
|
|
|
// with compression method for all files
|
|
|
|
$zipFile->addDirRecursive($dirName, $localPath, ZipFile::METHOD_DEFLATED); // Deflate compression
|
|
|
|
$zipFile->addDirRecursive($dirName, $localPath, ZipFile::METHOD_STORED); // No compression
|
|
|
|
$zipFile->addDirRecursive($dirName, $localPath, ZipFile::METHOD_BZIP2); // BZIP2 compression
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2017-03-15 10:42:46 +03:00
|
|
|
Add a files from directory iterator.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2017-03-15 10:42:46 +03:00
|
|
|
// $directoryIterator = new \DirectoryIterator($dir); // not recursive
|
|
|
|
// $directoryIterator = new \RecursiveDirectoryIterator($dir); // recursive
|
|
|
|
|
|
|
|
$zipFile->addFilesFromIterator($directoryIterator);
|
|
|
|
|
|
|
|
// with entry path
|
|
|
|
$localPath = "to/path/";
|
|
|
|
$zipFile->addFilesFromIterator($directoryIterator, $localPath);
|
|
|
|
// or
|
|
|
|
$zipFile[$localPath] = $directoryIterator;
|
|
|
|
|
|
|
|
// with compression method for all files
|
|
|
|
$zipFile->addFilesFromIterator($directoryIterator, $localPath, ZipFile::METHOD_DEFLATED); // Deflate compression
|
|
|
|
$zipFile->addFilesFromIterator($directoryIterator, $localPath, ZipFile::METHOD_STORED); // No compression
|
|
|
|
$zipFile->addFilesFromIterator($directoryIterator, $localPath, ZipFile::METHOD_BZIP2); // BZIP2 compression
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2017-03-15 10:42:46 +03:00
|
|
|
Example add a directory to the archive with ignoring files from directory iterator.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2017-03-15 10:42:46 +03:00
|
|
|
$ignoreFiles = [
|
|
|
|
"file_ignore.txt",
|
|
|
|
"dir_ignore/sub dir ignore/"
|
|
|
|
];
|
|
|
|
|
|
|
|
// use \DirectoryIterator for not recursive
|
|
|
|
$directoryIterator = new \RecursiveDirectoryIterator($dir);
|
|
|
|
|
|
|
|
// use IgnoreFilesFilterIterator for not recursive
|
|
|
|
$ignoreIterator = new IgnoreFilesRecursiveFilterIterator(
|
|
|
|
$directoryIterator,
|
|
|
|
$ignoreFiles
|
|
|
|
);
|
|
|
|
|
|
|
|
$zipFile->addFilesFromIterator($ignoreIterator);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Add a files **recursively** from [glob pattern](https://en.wikipedia.org/wiki/Glob_(programming)) to the archive.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$globPattern = '**.{jpg,jpeg,png,gif}'; // example glob pattern -> add all .jpg, .jpeg, .png and .gif files
|
2017-03-15 10:42:46 +03:00
|
|
|
|
|
|
|
$zipFile->addFilesFromGlobRecursive($dir, $globPattern);
|
|
|
|
|
|
|
|
// with entry path
|
|
|
|
$localPath = "to/path/";
|
|
|
|
$zipFile->addFilesFromGlobRecursive($dir, $globPattern, $localPath);
|
|
|
|
|
|
|
|
// with compression method for all files
|
|
|
|
$zipFile->addFilesFromGlobRecursive($dir, $globPattern, $localPath), ZipFile::METHOD_DEFLATED); // Deflate compression
|
|
|
|
$zipFile->addFilesFromGlobRecursive($dir, $globPattern, $localPath), ZipFile::METHOD_STORED); // No compression
|
|
|
|
$zipFile->addFilesFromGlobRecursive($dir, $globPattern, $localPath), ZipFile::METHOD_BZIP2); // BZIP2 compression
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Add a files **not recursively** from [glob pattern](https://en.wikipedia.org/wiki/Glob_(programming)) to the archive.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2017-03-15 10:42:46 +03:00
|
|
|
$globPattern = '**.{jpg,jpeg,png,gif}'; // example glob pattern -> add all .jpg, .jpeg, .png and .gif files
|
|
|
|
|
|
|
|
$zipFile->addFilesFromGlob($dir, $globPattern);
|
|
|
|
|
|
|
|
// with entry path
|
|
|
|
$localPath = "to/path/";
|
|
|
|
$zipFile->addFilesFromGlob($dir, $globPattern, $localPath);
|
|
|
|
|
|
|
|
// with compression method for all files
|
|
|
|
$zipFile->addFilesFromGlob($dir, $globPattern, $localPath), ZipFile::METHOD_DEFLATED); // Deflate compression
|
|
|
|
$zipFile->addFilesFromGlob($dir, $globPattern, $localPath), ZipFile::METHOD_STORED); // No compression
|
|
|
|
$zipFile->addFilesFromGlob($dir, $globPattern, $localPath), ZipFile::METHOD_BZIP2); // BZIP2 compression
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Add a files **recursively** from [RegEx (Regular Expression) pattern](https://en.wikipedia.org/wiki/Regular_expression) to the archive.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$regexPattern = '/\.(jpe?g|png|gif)$/si'; // example regex pattern -> add all .jpg, .jpeg, .png and .gif files
|
2017-03-15 10:42:46 +03:00
|
|
|
|
|
|
|
$zipFile->addFilesFromRegexRecursive($dir, $regexPattern);
|
|
|
|
|
|
|
|
// with entry path
|
|
|
|
$localPath = "to/path/";
|
|
|
|
$zipFile->addFilesFromRegexRecursive($dir, $regexPattern, $localPath);
|
|
|
|
|
|
|
|
// with compression method for all files
|
|
|
|
$zipFile->addFilesFromRegexRecursive($dir, $regexPattern, $localPath, ZipFile::METHOD_DEFLATED); // Deflate compression
|
|
|
|
$zipFile->addFilesFromRegexRecursive($dir, $regexPattern, $localPath, ZipFile::METHOD_STORED); // No compression
|
|
|
|
$zipFile->addFilesFromRegexRecursive($dir, $regexPattern, $localPath, ZipFile::METHOD_BZIP2); // BZIP2 compression
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Add a files **not recursively** from [RegEx (Regular Expression) pattern](https://en.wikipedia.org/wiki/Regular_expression) to the archive.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2017-03-15 10:42:46 +03:00
|
|
|
$regexPattern = '/\.(jpe?g|png|gif)$/si'; // example regex pattern -> add all .jpg, .jpeg, .png and .gif files
|
|
|
|
|
|
|
|
$zipFile->addFilesFromRegex($dir, $regexPattern);
|
|
|
|
|
|
|
|
// with entry path
|
|
|
|
$localPath = "to/path/";
|
|
|
|
$zipFile->addFilesFromRegex($dir, $regexPattern, $localPath);
|
|
|
|
|
|
|
|
// with compression method for all files
|
|
|
|
$zipFile->addFilesFromRegex($dir, $regexPattern, $localPath, ZipFile::METHOD_DEFLATED); // Deflate compression
|
|
|
|
$zipFile->addFilesFromRegex($dir, $regexPattern, $localPath, ZipFile::METHOD_STORED); // No compression
|
|
|
|
$zipFile->addFilesFromRegex($dir, $regexPattern, $localPath, ZipFile::METHOD_BZIP2); // BZIP2 compression
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Rename entry name.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2017-03-15 10:42:46 +03:00
|
|
|
$zipFile->rename($oldName, $newName);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Delete entry by name.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2017-03-15 10:42:46 +03:00
|
|
|
$zipFile->deleteFromName($entryName);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Delete entries from [glob pattern](https://en.wikipedia.org/wiki/Glob_(programming)).
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$globPattern = '**.{jpg,jpeg,png,gif}'; // example glob pattern -> delete all .jpg, .jpeg, .png and .gif files
|
2017-03-15 10:42:46 +03:00
|
|
|
|
|
|
|
$zipFile->deleteFromGlob($globPattern);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Delete entries from [RegEx (Regular Expression) pattern](https://en.wikipedia.org/wiki/Regular_expression).
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$regexPattern = '/\.(jpe?g|png|gif)$/si'; // example regex pattern -> delete all .jpg, .jpeg, .png and .gif files
|
2017-03-15 10:42:46 +03:00
|
|
|
|
|
|
|
$zipFile->deleteFromRegex($regexPattern);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Delete all entries.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2017-03-15 10:42:46 +03:00
|
|
|
$zipFile->deleteAll();
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Sets the compression level for entries.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
// This property is only used if the effective compression method is DEFLATED or BZIP2.
|
2017-03-15 10:42:46 +03:00
|
|
|
// Legal values are ZipFile::LEVEL_DEFAULT_COMPRESSION or range from
|
|
|
|
// ZipFile::LEVEL_BEST_SPEED to ZipFile::LEVEL_BEST_COMPRESSION.
|
|
|
|
|
|
|
|
$compressionMethod = ZipFile::LEVEL_BEST_COMPRESSION;
|
|
|
|
|
|
|
|
$zipFile->setCompressionLevel($compressionLevel);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Set comment archive.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2017-03-15 10:42:46 +03:00
|
|
|
$zipFile->setArchiveComment($commentArchive);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Set comment zip entry.
|
|
|
|
```php
|
2017-03-15 10:42:46 +03:00
|
|
|
$zipFile->setEntryComment($entryName, $entryComment);
|
2016-09-26 12:04:38 +03:00
|
|
|
```
|
2017-03-15 10:42:46 +03:00
|
|
|
Set a new password.
|
2016-09-26 12:04:38 +03:00
|
|
|
```php
|
2017-03-15 10:42:46 +03:00
|
|
|
$zipFile->withNewPassword($password);
|
2016-09-26 12:04:38 +03:00
|
|
|
```
|
2017-03-15 10:42:46 +03:00
|
|
|
Set a new password and encryption method.
|
2016-09-26 12:04:38 +03:00
|
|
|
```php
|
2017-03-13 19:59:30 +03:00
|
|
|
$encryptionMethod = ZipFile::ENCRYPTION_METHOD_WINZIP_AES; // default value
|
2017-03-15 10:42:46 +03:00
|
|
|
$zipFile->withNewPassword($password, $encryptionMethod);
|
2016-09-08 19:18:53 +03:00
|
|
|
|
2016-09-26 12:04:38 +03:00
|
|
|
// Support encryption methods:
|
2017-03-13 19:59:30 +03:00
|
|
|
// ZipFile::ENCRYPTION_METHOD_TRADITIONAL - Traditional PKWARE Encryption
|
|
|
|
// ZipFile::ENCRYPTION_METHOD_WINZIP_AES - WinZip AES Encryption
|
2016-09-26 12:04:38 +03:00
|
|
|
```
|
|
|
|
Remove password from all entries.
|
|
|
|
```php
|
2017-03-15 10:42:46 +03:00
|
|
|
$zipFile->withoutPassword();
|
2016-09-26 12:04:38 +03:00
|
|
|
```
|
2017-03-15 10:42:46 +03:00
|
|
|
#### <a name="Documentation-ZipAlign-Usage"></a> ZipAlign Usage
|
|
|
|
Set archive alignment ([`zipalign`](https://developer.android.com/studio/command-line/zipalign.html)).
|
2016-09-26 12:04:38 +03:00
|
|
|
```php
|
2017-03-15 10:42:46 +03:00
|
|
|
// before save or output
|
|
|
|
$zipFile->setAlign(4); // alternative command: zipalign -f -v 4 filename.zip
|
2016-09-26 12:04:38 +03:00
|
|
|
```
|
2017-03-15 10:42:46 +03:00
|
|
|
#### <a name="Documentation-Save-Or-Output-Entries"></a> Save Zip File or Output
|
2016-09-26 12:04:38 +03:00
|
|
|
Save archive to a file.
|
|
|
|
```php
|
2017-03-15 10:42:46 +03:00
|
|
|
$zipFile->saveAsFile($filename);
|
2016-09-26 12:04:38 +03:00
|
|
|
```
|
|
|
|
Save archive to a stream.
|
|
|
|
```php
|
2017-03-15 10:42:46 +03:00
|
|
|
// $fp = fopen($filename, 'w+b');
|
|
|
|
|
|
|
|
$zipFile->saveAsStream($fp);
|
2016-09-26 12:04:38 +03:00
|
|
|
```
|
|
|
|
Returns the zip archive as a string.
|
|
|
|
```php
|
2017-03-15 10:42:46 +03:00
|
|
|
$rawZipArchiveBytes = $zipFile->outputAsString();
|
2016-09-26 12:04:38 +03:00
|
|
|
```
|
|
|
|
Output .ZIP archive as attachment and terminate.
|
|
|
|
```php
|
2017-03-15 10:42:46 +03:00
|
|
|
$zipFile->outputAsAttachment($outputFilename);
|
2016-09-26 12:04:38 +03:00
|
|
|
// or set mime type
|
2017-03-15 10:42:46 +03:00
|
|
|
$mimeType = 'application/zip'
|
|
|
|
$zipFile->outputAsAttachment($outputFilename, $mimeType);
|
2016-09-26 12:04:38 +03:00
|
|
|
```
|
2017-03-15 10:42:46 +03:00
|
|
|
Rewrite and reopen zip archive.
|
2016-09-26 12:04:38 +03:00
|
|
|
```php
|
2017-03-15 10:42:46 +03:00
|
|
|
$zipFile->rewrite();
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2017-03-15 10:42:46 +03:00
|
|
|
#### <a name="Documentation-Close-Zip-Archive"></a> Close Zip Archive
|
|
|
|
Close zip archive.
|
2016-09-26 12:04:38 +03:00
|
|
|
```php
|
2017-03-15 10:42:46 +03:00
|
|
|
$zipFile->close();
|
2016-09-26 12:04:38 +03:00
|
|
|
```
|
2017-03-15 10:42:46 +03:00
|
|
|
### <a name="Running-Tests"></a> Running Tests
|
|
|
|
Installing development dependencies.
|
|
|
|
```bash
|
|
|
|
composer install --dev
|
2016-09-26 12:04:38 +03:00
|
|
|
```
|
2017-03-15 10:42:46 +03:00
|
|
|
Run tests
|
|
|
|
```bash
|
|
|
|
vendor/bin/phpunit -v -c bootstrap.xml
|
2016-09-26 12:04:38 +03:00
|
|
|
```
|
2017-03-15 10:42:46 +03:00
|
|
|
### <a name="Upgrade"></a> Upgrade version 2 to version 3
|
|
|
|
Update to the New Major Version via Composer
|
|
|
|
```json
|
2016-09-26 12:04:38 +03:00
|
|
|
{
|
2017-03-15 10:42:46 +03:00
|
|
|
"require": {
|
|
|
|
"nelexa/zip": "^3.0"
|
|
|
|
}
|
2016-09-26 12:04:38 +03:00
|
|
|
}
|
|
|
|
```
|
2017-03-15 10:42:46 +03:00
|
|
|
Next, use Composer to download new versions of the libraries:
|
|
|
|
```bash
|
|
|
|
composer update nelexa/zip
|
|
|
|
```
|
|
|
|
Update your Code to Work with the New Version:
|
|
|
|
- Class `ZipOutputFile` merged to `ZipFile` and removed.
|
|
|
|
+ `new \PhpZip\ZipOutputFile()` to `new \PhpZip\ZipFile()`
|
|
|
|
- Static initialization methods are now not static.
|
|
|
|
+ `\PhpZip\ZipFile::openFromFile($filename);` to `(new \PhpZip\ZipFile())->openFile($filename);`
|
|
|
|
+ `\PhpZip\ZipOutputFile::openFromFile($filename);` to `(new \PhpZip\ZipFile())->openFile($filename);`
|
|
|
|
+ `\PhpZip\ZipFile::openFromString($contents);` to `(new \PhpZip\ZipFile())->openFromString($contents);`
|
|
|
|
+ `\PhpZip\ZipFile::openFromStream($stream);` to `(new \PhpZip\ZipFile())->openFromStream($stream);`
|
|
|
|
+ `\PhpZip\ZipOutputFile::create()` to `new \PhpZip\ZipFile()`
|
|
|
|
+ `\PhpZip\ZipOutputFile::openFromZipFile(\PhpZip\ZipFile $zipFile)` > `(new \PhpZip\ZipFile())->openFile($filename);`
|
|
|
|
- Rename methods:
|
|
|
|
+ `addFromFile` to `addFile`
|
|
|
|
+ `setLevel` to `setCompressionLevel`
|
|
|
|
+ `ZipFile::setPassword` to `ZipFile::withReadPassword`
|
|
|
|
+ `ZipOutputFile::setPassword` to `ZipFile::withNewPassword`
|
|
|
|
+ `ZipOutputFile::removePasswordAllEntries` to `ZipFile::withoutPassword`
|
|
|
|
+ `ZipOutputFile::setComment` to `ZipFile::setArchiveComment`
|
|
|
|
+ `ZipFile::getComment` to `ZipFile::getArchiveComment`
|
|
|
|
- Changed signature for methods `addDir`, `addFilesFromGlob`, `addFilesFromRegex`.
|
|
|
|
- Remove methods
|
|
|
|
+ `getLevel`
|
|
|
|
+ `setCompressionMethod`
|
|
|
|
+ `setEntryPassword`
|
2016-09-26 12:04:38 +03:00
|
|
|
|
|
|
|
|