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-13 08:33:45 +03:00
|
|
|
[](https://packagist.org/packages/nelexa/zip)
|
|
|
|
[](https://packagist.org/packages/nelexa/zip)
|
|
|
|
[](https://packagist.org/packages/nelexa/zip)
|
|
|
|
|
2017-03-10 08:23:57 +03:00
|
|
|
Features:
|
|
|
|
---------
|
|
|
|
- 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.
|
|
|
|
- Support zip password for PHP < 5.6.0 (`\ZipArchive` required this version), include update and remove password.
|
|
|
|
- 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
|
|
|
|
|
|
|
Requirements
|
|
|
|
------------
|
2017-03-13 09:13:19 +03:00
|
|
|
- `PHP` >= 5.5 (64 bit)
|
2016-10-14 18:08:00 +03:00
|
|
|
- PHP-extension `mbstring`
|
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.
|
|
|
|
|
|
|
|
Installation
|
|
|
|
------------
|
2017-03-10 08:23:57 +03:00
|
|
|
`composer require nelexa/zip:^3.0`
|
|
|
|
|
|
|
|
Samples
|
|
|
|
-------
|
|
|
|
```php
|
|
|
|
// create archive
|
|
|
|
$zipFile = new \PhpZip\ZipFile();
|
|
|
|
$zipFile->addFromString("zip/entry/filename", "Is file content")
|
|
|
|
->addFile("/path/to/file", "data/tofile")
|
|
|
|
->addDir(__DIR__, "to/path/")
|
|
|
|
->saveAsFile($outputFilename)
|
|
|
|
->close();
|
|
|
|
|
|
|
|
// open archive, extract, add files, set password and output to browser.
|
|
|
|
$zipFile->openFile($outputFilename)
|
|
|
|
->extractTo($outputDirExtract)
|
|
|
|
->deleteFromRegex('~^\.~') // delete all hidden (Unix) files
|
|
|
|
->addFromString('dir/file.txt', 'Test file')
|
|
|
|
->withNewPassword('password')
|
|
|
|
->outputAsAttachment('library.jar');
|
|
|
|
```
|
|
|
|
Other examples can be found in the `tests/` folder
|
2016-09-26 12:04:38 +03:00
|
|
|
|
|
|
|
Documentation
|
|
|
|
-------------
|
2017-03-10 08:23:57 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
$data = file_get_contents($urlOrFile);
|
|
|
|
$zipFile = new \PhpZip\ZipFile();
|
|
|
|
$zipFile->openFromString($filename);
|
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');
|
|
|
|
$zipFile = \PhpZip\ZipFile::openFromStream($stream);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
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 = $zipFile->count();
|
|
|
|
// or
|
|
|
|
$count = count($zipFile);
|
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();
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Foreach 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
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Iterator zip entries.
|
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
|
|
|
Checks whether a entry exists.
|
|
|
|
```php
|
|
|
|
$boolValue = $zipFile->hasEntry($entryName);
|
|
|
|
```
|
|
|
|
Check whether the directory entry.
|
|
|
|
```php
|
|
|
|
$boolValue = $zipFile->isDirectory($entryName);
|
|
|
|
```
|
|
|
|
Set password to all encrypted entries.
|
|
|
|
```php
|
|
|
|
$zipFile->setPassword($password);
|
|
|
|
```
|
|
|
|
Set password to concrete zip entry.
|
|
|
|
```php
|
|
|
|
$zipFile->setEntryPassword($entryName, $password);
|
|
|
|
```
|
|
|
|
Get comment archive.
|
|
|
|
```php
|
|
|
|
$commentArchive = $zipFile->getComment();
|
|
|
|
```
|
|
|
|
Get comment zip entry.
|
|
|
|
```php
|
|
|
|
$commentEntry = $zipFile->getEntryComment($entryName);
|
|
|
|
```
|
|
|
|
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');
|
|
|
|
echo $zipInfo . PHP_EOL;
|
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}
|
2016-09-26 12:04:38 +03:00
|
|
|
print_r($zipInfo);
|
|
|
|
//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
|
2016-10-14 18:08:00 +03:00
|
|
|
// [attributes:PhpZip\Model\ZipInfo:private] => -rw-r--r--
|
2016-09-26 12:04:38 +03:00
|
|
|
//)
|
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();
|
|
|
|
print_r($zipAllInfo);
|
|
|
|
//Array
|
|
|
|
//(
|
|
|
|
// [file.txt] => PhpZip\Model\ZipInfo Object
|
|
|
|
// (
|
|
|
|
// ...
|
|
|
|
// )
|
|
|
|
//
|
|
|
|
// [file2.txt] => PhpZip\Model\ZipInfo Object
|
|
|
|
// (
|
|
|
|
// ...
|
|
|
|
// )
|
|
|
|
//
|
|
|
|
// ...
|
|
|
|
//)
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Extract all files to directory.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$zipFile->extractTo($directory);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Extract some files to directory.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$extractOnlyFiles = ["filename1", "filename2", "dir/dir/dir/"];
|
|
|
|
$zipFile->extractTo($directory, $extractOnlyFiles);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Get entry content.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$data = $zipFile->getEntryContent($entryName);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2017-03-02 00:16:09 +03:00
|
|
|
Edit zip archive
|
|
|
|
```php
|
|
|
|
$zipOutputFile = $zipFile->edit();
|
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Close zip archive.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$zipFile->close();
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
#### Class `\PhpZip\ZipOutputFile` (create, update, extract)
|
|
|
|
Create zip archive.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$zipOutputFile = new \PhpZip\ZipOutputFile();
|
|
|
|
// or
|
|
|
|
$zipOutputFile = \PhpZip\ZipOutputFile::create();
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Open zip file from update.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2017-03-02 00:16:09 +03:00
|
|
|
$filename = "file.zip";
|
|
|
|
$zipOutputFile = \PhpZip\ZipOutputFile::openFromFile($filename);
|
|
|
|
```
|
|
|
|
or
|
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
// initial ZipFile
|
|
|
|
$zipFile = \PhpZip\ZipFile::openFromFile($filename);
|
|
|
|
|
|
|
|
// Create output stream from update zip file
|
|
|
|
$zipOutputFile = new \PhpZip\ZipOutputFile($zipFile);
|
|
|
|
// or
|
|
|
|
$zipOutputFile = \PhpZip\ZipOutputFile::openFromZipFile($zipFile);
|
2017-03-02 00:16:09 +03:00
|
|
|
// or
|
|
|
|
$zipOutputFile = $zipFile->edit();
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Add entry from file.
|
|
|
|
```php
|
|
|
|
$zipOutputFile->addFromFile($filename); // $entryName == basename($filename);
|
|
|
|
$zipOutputFile->addFromFile($filename, $entryName);
|
|
|
|
$zipOutputFile->addFromFile($filename, $entryName, ZipEntry::METHOD_DEFLATED);
|
2017-03-02 00:16:09 +03:00
|
|
|
$zipOutputFile->addFromFile($filename, $entryName, ZipEntry::METHOD_STORED); // no compress
|
2016-09-26 12:04:38 +03:00
|
|
|
$zipOutputFile->addFromFile($filename, null, ZipEntry::METHOD_BZIP2); // $entryName == basename($filename);
|
|
|
|
```
|
|
|
|
Add entry from string data.
|
|
|
|
```php
|
2017-03-02 00:16:09 +03:00
|
|
|
$zipOutputFile->addFromString($entryName, $data);
|
|
|
|
$zipOutputFile->addFromString($entryName, $data, ZipEntry::METHOD_DEFLATED);
|
|
|
|
$zipOutputFile->addFromString($entryName, $data, ZipEntry::METHOD_STORED); // no compress
|
2016-09-26 12:04:38 +03:00
|
|
|
```
|
|
|
|
Add entry from stream.
|
|
|
|
```php
|
2017-03-02 00:16:09 +03:00
|
|
|
$zipOutputFile->addFromStream($stream, $entryName);
|
|
|
|
$zipOutputFile->addFromStream($stream, $entryName, ZipEntry::METHOD_DEFLATED);
|
|
|
|
$zipOutputFile->addFromStream($stream, $entryName, ZipEntry::METHOD_STORED); // no compress
|
2016-09-26 12:04:38 +03:00
|
|
|
```
|
|
|
|
Add empty dir
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$zipOutputFile->addEmptyDir($dirName);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Add a directory **recursively** to the archive.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$zipOutputFile->addDir($dirName);
|
|
|
|
// or
|
|
|
|
$zipOutputFile->addDir($dirName, true);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Add a directory **not recursively** to the archive.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$zipOutputFile->addDir($dirName, false);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Add a directory to the archive by path `$moveToPath`
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$moveToPath = 'dir/subdir/';
|
|
|
|
$zipOutputFile->addDir($dirName, $boolResursive, $moveToPath);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Add a directory to the archive with ignoring files.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$ignoreFiles = ["file_ignore.txt", "dir_ignore/sub dir ignore/"];
|
|
|
|
$zipOutputFile->addDir($dirName, $boolResursive, $moveToPath, $ignoreFiles);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Add a directory and set compression method.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$compressionMethod = ZipEntry::METHOD_DEFLATED;
|
|
|
|
$zipOutputFile->addDir($dirName, $boolRecursive, $moveToPath, $ignoreFiles, $compressionMethod);
|
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
|
|
|
|
$zipOutputFile->addFilesFromGlob($inputDir, $globPattern);
|
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
|
2016-09-26 12:04:38 +03:00
|
|
|
$recursive = false;
|
|
|
|
$zipOutputFile->addFilesFromGlob($inputDir, $globPattern, $recursive);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Add a files from [glob pattern](https://en.wikipedia.org/wiki/Glob_(programming)) to the archive by path `$moveToPath`.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$moveToPath = 'dir/dir2/dir3';
|
|
|
|
$zipOutputFile->addFilesFromGlob($inputDir, $globPattern, $recursive = true, $moveToPath);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Add a files from [glob pattern](https://en.wikipedia.org/wiki/Glob_(programming)) to the archive and set compression method.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$compressionMethod = ZipEntry::METHOD_DEFLATED;
|
|
|
|
$zipOutputFile->addFilesFromGlob($inputDir, $globPattern, $recursive, $moveToPath, $compressionMethod);
|
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
|
|
|
|
$zipOutputFile->addFilesFromRegex($inputDir, $regexPattern);
|
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
|
2016-09-26 12:04:38 +03:00
|
|
|
$recursive = false;
|
|
|
|
$zipOutputFile->addFilesFromRegex($inputDir, $regexPattern, $recursive);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Add a files from [RegEx (Regular Expression) pattern](https://en.wikipedia.org/wiki/Regular_expression) to the archive by path `$moveToPath`.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$moveToPath = 'dir/dir2/dir3';
|
|
|
|
$zipOutputFile->addFilesFromRegex($inputDir, $regexPattern, $recursive = true, $moveToPath);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Add a files from [RegEx (Regular Expression) pattern](https://en.wikipedia.org/wiki/Regular_expression) to the archive and set compression method.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$compressionMethod = ZipEntry::METHOD_DEFLATED;
|
|
|
|
$zipOutputFile->addFilesFromRegex($inputDir, $regexPattern, $recursive, $moveToPath, $compressionMethod);
|
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
|
2016-09-26 12:04:38 +03:00
|
|
|
$zipOutputFile->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
|
2016-09-26 12:04:38 +03:00
|
|
|
$zipOutputFile->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
|
|
|
|
$zipOutputFile->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
|
|
|
|
$zipOutputFile->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
|
2016-09-26 12:04:38 +03:00
|
|
|
$zipOutputFile->deleteAll();
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
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 = $zipOutputFile->count();
|
|
|
|
// or
|
|
|
|
$count = count($zipOutputFile);
|
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 = $zipOutputFile->getListFiles();
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Get the compression level for entries.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$compressionLevel = $zipOutputFile->getLevel();
|
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.
|
|
|
|
// Legal values are ZipOutputFile::LEVEL_DEFAULT_COMPRESSION or range from
|
|
|
|
// ZipOutputFile::LEVEL_BEST_SPEED to ZipOutputFile::LEVEL_BEST_COMPRESSION.
|
|
|
|
$compressionMethod = ZipOutputFile::LEVEL_BEST_COMPRESSION;
|
|
|
|
$zipOutputFile->setLevel($compressionLevel);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Get comment archive.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$commentArchive = $zipOutputFile->getComment();
|
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
|
2016-09-26 12:04:38 +03:00
|
|
|
$zipOutputFile->setComment($commentArchive);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Get comment zip entry.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$commentEntry = $zipOutputFile->getEntryComment($entryName);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Set comment zip entry.
|
|
|
|
```php
|
|
|
|
$zipOutputFile->setEntryComment($entryName, $entryComment);
|
|
|
|
```
|
|
|
|
Set compression method for zip entry.
|
|
|
|
```php
|
|
|
|
$compressionMethod = ZipEntry::METHOD_DEFLATED;
|
|
|
|
$zipOutputMethod->setCompressionMethod($entryName, $compressionMethod);
|
|
|
|
|
|
|
|
// Support compression methods:
|
|
|
|
// ZipEntry::METHOD_STORED - no compression
|
|
|
|
// ZipEntry::METHOD_DEFLATED - deflate compression
|
|
|
|
// ZipEntry::METHOD_BZIP2 - bzip2 compression (need bz2 extension)
|
|
|
|
```
|
|
|
|
Set a password for all previously added entries.
|
|
|
|
```php
|
|
|
|
$zipOutputFile->setPassword($password);
|
|
|
|
```
|
|
|
|
Set a password and encryption method for all previously added entries.
|
|
|
|
```php
|
|
|
|
$encryptionMethod = ZipEntry::ENCRYPTION_METHOD_WINZIP_AES; // default value
|
|
|
|
$zipOutputFile->setPassword($password, $encryptionMethod);
|
2016-09-08 19:18:53 +03:00
|
|
|
|
2016-09-26 12:04:38 +03:00
|
|
|
// Support encryption methods:
|
|
|
|
// ZipEntry::ENCRYPTION_METHOD_TRADITIONAL - Traditional PKWARE Encryption
|
|
|
|
// ZipEntry::ENCRYPTION_METHOD_WINZIP_AES - WinZip AES Encryption
|
|
|
|
```
|
|
|
|
Set a password for a concrete entry.
|
|
|
|
```php
|
|
|
|
$zipOutputFile->setEntryPassword($entryName, $password);
|
|
|
|
```
|
|
|
|
Set a password and encryption method for a concrete entry.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$zipOutputFile->setEntryPassword($entryName, $password, $encryptionMethod);
|
2016-09-08 19:18:53 +03:00
|
|
|
|
2016-09-26 12:04:38 +03:00
|
|
|
// Support encryption methods:
|
|
|
|
// ZipEntry::ENCRYPTION_METHOD_TRADITIONAL - Traditional PKWARE Encryption
|
|
|
|
// ZipEntry::ENCRYPTION_METHOD_WINZIP_AES - WinZip AES Encryption (default value)
|
|
|
|
```
|
|
|
|
Remove password from all entries.
|
|
|
|
```php
|
|
|
|
$zipOutputFile->removePasswordAllEntries();
|
|
|
|
```
|
|
|
|
Remove password for concrete zip entry.
|
|
|
|
```php
|
|
|
|
$zipOutputFile->removePasswordFromEntry($entryName);
|
|
|
|
```
|
|
|
|
Save archive to a file.
|
|
|
|
```php
|
|
|
|
$zipOutputFile->saveAsFile($filename);
|
|
|
|
```
|
|
|
|
Save archive to a stream.
|
|
|
|
```php
|
2016-09-26 18:33:05 +03:00
|
|
|
$handle = fopen($filename, 'w+b');
|
2016-09-26 12:04:38 +03:00
|
|
|
$autoCloseResource = true;
|
|
|
|
$zipOutputFile->saveAsStream($handle, $autoCloseResource);
|
|
|
|
if(!$autoCloseResource){
|
|
|
|
fclose($handle);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
Returns the zip archive as a string.
|
|
|
|
```php
|
|
|
|
$rawZipArchiveBytes = $zipOutputFile->outputAsString();
|
|
|
|
```
|
|
|
|
Output .ZIP archive as attachment and terminate.
|
|
|
|
```php
|
|
|
|
$zipOutputFile->outputAsAttachment($outputFilename);
|
|
|
|
// or set mime type
|
|
|
|
$zipOutputFile->outputAsAttachment($outputFilename = 'output.zip', $mimeType = 'application/zip');
|
|
|
|
```
|
|
|
|
Extract all files to directory.
|
|
|
|
```php
|
|
|
|
$zipOutputFile->extractTo($directory);
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Extract some files to directory.
|
|
|
|
```php
|
|
|
|
$extractOnlyFiles = ["filename1", "filename2", "dir/dir/dir/"];
|
|
|
|
$zipOutputFile->extractTo($directory, $extractOnlyFiles);
|
|
|
|
```
|
|
|
|
Get entry contents.
|
|
|
|
```php
|
|
|
|
$data = $zipOutputFile->getEntryContent($entryName);
|
|
|
|
```
|
|
|
|
Foreach zip entries.
|
|
|
|
```php
|
|
|
|
foreach($zipOutputFile as $entryName => $dataContent){
|
|
|
|
echo "Entry: $entryName" . PHP_EOL;
|
|
|
|
echo "Data: $dataContent" . PHP_EOL;
|
|
|
|
echo "-----------------------------" . PHP_EOL;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
Iterator zip entries.
|
|
|
|
```php
|
|
|
|
$iterator = new \ArrayIterator($zipOutputFile);
|
|
|
|
while ($iterator->valid())
|
|
|
|
{
|
|
|
|
$entryName = $iterator->key();
|
|
|
|
$dataContent = $iterator->current();
|
2016-09-08 19:18:53 +03:00
|
|
|
|
2016-09-26 12:04:38 +03:00
|
|
|
echo "Entry: $entryName" . PHP_EOL;
|
|
|
|
echo "Data: $dataContent" . PHP_EOL;
|
|
|
|
echo "-----------------------------" . PHP_EOL;
|
|
|
|
|
|
|
|
$iterator->next();
|
|
|
|
}
|
|
|
|
```
|
2016-09-26 16:56:06 +03:00
|
|
|
Set zip alignment (alternate program `zipalign`).
|
|
|
|
```php
|
|
|
|
// before save or output
|
|
|
|
$zipOutputFile->setAlign(4); // alternative cmd: zipalign -f -v 4 filename.zip
|
|
|
|
```
|
2016-09-26 12:04:38 +03:00
|
|
|
Close zip archive.
|
2016-09-08 19:18:53 +03:00
|
|
|
```php
|
2016-09-26 12:04:38 +03:00
|
|
|
$zipOutputFile->close();
|
|
|
|
```
|
|
|
|
Examples
|
|
|
|
--------
|
|
|
|
Create, open, extract and update archive.
|
|
|
|
```php
|
|
|
|
$outputFilename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'output.zip';
|
|
|
|
$outputDirExtract = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'extract';
|
|
|
|
|
|
|
|
if(!is_dir($outputDirExtract)){
|
|
|
|
mkdir($outputDirExtract, 0755, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$zipOutputFile = \PhpZip\ZipOutputFile::create(); // create archive
|
|
|
|
$zipOutputFile->addDir(__DIR__, true); // add this dir to archive
|
|
|
|
$zipOutputFile->saveAsFile($outputFilename); // save as file
|
|
|
|
$zipOutputFile->close(); // close output file, release all streams
|
|
|
|
|
|
|
|
$zipFile = \PhpZip\ZipFile::openFromFile($outputFilename); // open zip archive from file
|
|
|
|
$zipFile->extractTo($outputDirExtract); // extract files to dir
|
|
|
|
|
2017-03-02 00:16:09 +03:00
|
|
|
$zipOutputFile = $zipFile->edit(); // create zip output archive for update
|
2016-09-26 12:04:38 +03:00
|
|
|
$zipOutputFile->deleteFromRegex('~^\.~'); // delete all hidden (Unix) files
|
|
|
|
$zipOutputFile->addFromString('dir/file.txt', 'Test file'); // add files from string contents
|
|
|
|
$zipOutputFile->saveAsFile($outputFilename); // update zip file
|
|
|
|
$zipOutputFile->close(); // close output file, release all streams
|
|
|
|
|
|
|
|
$zipFile->close(); // close input file, release all streams
|
|
|
|
```
|
|
|
|
Other examples can be found in the `tests/` folder
|
2016-09-08 19:18:53 +03:00
|
|
|
|
2016-09-26 12:04:38 +03:00
|
|
|
Running Tests
|
|
|
|
-------------
|
|
|
|
```bash
|
|
|
|
vendor/bin/phpunit -v --tap -c bootstrap.xml
|
2016-09-08 19:18:53 +03:00
|
|
|
```
|