1
0
mirror of https://github.com/Ne-Lexa/php-zip.git synced 2025-07-18 22:51:11 +02:00

Update README

This commit is contained in:
wapplay-home-linux
2017-03-15 10:42:46 +03:00
parent 0dbdc0faeb
commit 3ab98532a0
12 changed files with 228 additions and 228 deletions

374
README.md
View File

@@ -2,9 +2,10 @@
==================== ====================
`PhpZip` - php library for manipulating zip archives. `PhpZip` - php library for manipulating zip archives.
[![Build Status](https://travis-ci.org/Ne-Lexa/php-zip.svg?branch=feature/3.0.0-dev)](https://travis-ci.org/Ne-Lexa/php-zip)
[![Latest Stable Version](https://poser.pugx.org/nelexa/zip/v/stable)](https://packagist.org/packages/nelexa/zip) [![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) [![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/) [![Minimum PHP Version](http://img.shields.io/badge/php%2064bit-%3E%3D%205.5-8892BF.svg)](https://php.net/)
[![Test Coverage](https://codeclimate.com/github/Ne-Lexa/php-zip/badges/coverage.svg)](https://codeclimate.com/github/Ne-Lexa/php-zip/coverage) [![Test Coverage](https://codeclimate.com/github/Ne-Lexa/php-zip/badges/coverage.svg)](https://codeclimate.com/github/Ne-Lexa/php-zip/coverage)
[![License](https://poser.pugx.org/nelexa/zip/license)](https://packagist.org/packages/nelexa/zip) [![License](https://poser.pugx.org/nelexa/zip/license)](https://packagist.org/packages/nelexa/zip)
@@ -18,6 +19,11 @@ Table of contents
+ [Open Zip Archive](#Documentation-Open-Zip-Archive) + [Open Zip Archive](#Documentation-Open-Zip-Archive)
+ [Get Zip Entries](#Documentation-Open-Zip-Entries) + [Get Zip Entries](#Documentation-Open-Zip-Entries)
+ [Add Zip Entries](#Documentation-Add-Zip-Entries) + [Add Zip Entries](#Documentation-Add-Zip-Entries)
+ [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")
### <a name="Features"></a> Features ### <a name="Features"></a> Features
- Opening and unzipping zip files. - Opening and unzipping zip files.
@@ -27,7 +33,7 @@ Table of contents
- Output the modified archive as a string or output to the browser without saving the result to disk. - Output the modified archive as a string or output to the browser without saving the result to disk.
- Support archive comment and entries comments. - Support archive comment and entries comments.
- Get info of zip entries. - Get info of zip entries.
- Support zip password for PHP < 5.6.0 (`\ZipArchive` required this version), include update and remove password. - Support zip password for PHP 5.5, include update and remove password.
- Support encryption method `Traditional PKWARE Encryption (ZipCrypto)` and `WinZIP AES Encryption`. - Support encryption method `Traditional PKWARE Encryption (ZipCrypto)` and `WinZIP AES Encryption`.
- Support `ZIP64` (size > 4 GiB or files > 65535 in a .ZIP archive). - 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). - Support archive alignment functional [`zipalign`](https://developer.android.com/studio/command-line/zipalign.html).
@@ -62,8 +68,8 @@ $zipFile
``` ```
Other examples can be found in the `tests/` folder Other examples can be found in the `tests/` folder
### <a name="Documentation"></a> Documentation ### <a name="Documentation"></a> Documentation:
###### <a name="Documentation-Open-Zip-Archive"></a> Open Zip Archive #### <a name="Documentation-Open-Zip-Archive"></a> Open Zip Archive
Open zip archive from file. Open zip archive from file.
```php ```php
$zipFile = new \PhpZip\ZipFile(); $zipFile = new \PhpZip\ZipFile();
@@ -80,8 +86,8 @@ $stream = fopen($filename, 'rb');
$zipFile = new \PhpZip\ZipFile(); $zipFile = new \PhpZip\ZipFile();
$zipFile->openFromStream($stream); $zipFile->openFromStream($stream);
###### <a name="Documentation-Open-Zip-Entries"></a> Get Zip Entries
``` ```
#### <a name="Documentation-Open-Zip-Entries"></a> Get Zip Entries
Get num entries. Get num entries.
```php ```php
$count = count($zipFile); $count = count($zipFile);
@@ -217,8 +223,8 @@ print_r($zipAllInfo);
// ... // ...
//) //)
###### <a name="Documentation-Add-Zip-Entries"></a> Add Zip Entries
``` ```
#### <a name="Documentation-Add-Zip-Entries"></a> Add Zip Entries
Adding a file to the zip-archive. Adding a file to the zip-archive.
```php ```php
// entry name is file basename. // entry name is file basename.
@@ -232,7 +238,7 @@ $zipFile->addFile($filename, $entryName);
$zipFile[$entryName] = new \SplFileInfo($filename); $zipFile[$entryName] = new \SplFileInfo($filename);
// with compression method // with compression method
$zipOFile->addFile($filename, $entryName, ZipFile::METHOD_DEFLATED); // Deflate compression $zipFile->addFile($filename, $entryName, ZipFile::METHOD_DEFLATED); // Deflate compression
$zipFile->addFile($filename, $entryName, ZipFile::METHOD_STORED); // No compression $zipFile->addFile($filename, $entryName, ZipFile::METHOD_STORED); // No compression
$zipFile->addFile($filename, null, ZipFile::METHOD_BZIP2); // BZIP2 compression $zipFile->addFile($filename, null, ZipFile::METHOD_BZIP2); // BZIP2 compression
``` ```
@@ -254,12 +260,14 @@ Add entry from stream.
$zipFile->addFromStream($stream, $entryName); $zipFile->addFromStream($stream, $entryName);
// with compression method // with compression method
$zipFile->addFromStream($stream, $entryName, ZipFile::METHOD_DEFLATED); Deflate compression $zipFile->addFromStream($stream, $entryName, ZipFile::METHOD_DEFLATED); // Deflate compression
$zipFile->addFromStream($stream, $entryName, ZipFile::METHOD_STORED); // No compression $zipFile->addFromStream($stream, $entryName, ZipFile::METHOD_STORED); // No compression
$zipFile->addFromStream($stream, $entryName, ZipFile::METHOD_BZIP2); // BZIP2 compression $zipFile->addFromStream($stream, $entryName, ZipFile::METHOD_BZIP2); // BZIP2 compression
``` ```
Add empty dir Add empty dir
```php ```php
// $dirName = "path/to/";
$zipFile->addEmptyDir($dirName); $zipFile->addEmptyDir($dirName);
// or // or
$zipFile[$dirName] = null; $zipFile[$dirName] = null;
@@ -283,263 +291,259 @@ $localPath = "to/path/";
$zipFile->addDir($dirName, $localPath); $zipFile->addDir($dirName, $localPath);
// with compression method for all files // with compression method for all files
$zipFile->addDir($dirName, $localPath, ZipFile::METHOD_DEFLATED); Deflate compression $zipFile->addDir($dirName, $localPath, ZipFile::METHOD_DEFLATED); // Deflate compression
$zipFile->addDir($dirName, $localPath, ZipFile::METHOD_STORED); // No compression $zipFile->addDir($dirName, $localPath, ZipFile::METHOD_STORED); // No compression
$zipFile->addDir($dirName, $localPath, ZipFile::METHOD_BZIP2); // BZIP2 compression $zipFile->addDir($dirName, $localPath, ZipFile::METHOD_BZIP2); // BZIP2 compression
```
Add a directory **recursively** to the archive.
```php
$zipFile->addDirRecursive($dirName);
// with entry path
$localPath = "to/path/";
$zipFile->addDirRecursive($dirName, $localPath);
// 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
``` ```
Add a directory to the archive with ignoring files. Add a files from directory iterator.
```php ```php
$ignoreFiles = ["file_ignore.txt", "dir_ignore/sub dir ignore/"]; // $directoryIterator = new \DirectoryIterator($dir); // not recursive
$zipOutputFile->addDir($dirName, $boolResursive, $moveToPath, $ignoreFiles); // $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
``` ```
Add a directory and set compression method. Example add a directory to the archive with ignoring files from directory iterator.
```php ```php
$compressionMethod = ZipFile::METHOD_DEFLATED; $ignoreFiles = [
$zipOutputFile->addDir($dirName, $boolRecursive, $moveToPath, $ignoreFiles, $compressionMethod); "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);
``` ```
Add a files **recursively** from [glob pattern](https://en.wikipedia.org/wiki/Glob_(programming)) to the archive. Add a files **recursively** from [glob pattern](https://en.wikipedia.org/wiki/Glob_(programming)) to the archive.
```php ```php
$globPattern = '**.{jpg,jpeg,png,gif}'; // example glob pattern -> add all .jpg, .jpeg, .png and .gif files $globPattern = '**.{jpg,jpeg,png,gif}'; // example glob pattern -> add all .jpg, .jpeg, .png and .gif files
$zipOutputFile->addFilesFromGlob($inputDir, $globPattern);
$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
``` ```
Add a files **not recursively** from [glob pattern](https://en.wikipedia.org/wiki/Glob_(programming)) to the archive. Add a files **not recursively** from [glob pattern](https://en.wikipedia.org/wiki/Glob_(programming)) to the archive.
```php ```php
$recursive = false; $globPattern = '**.{jpg,jpeg,png,gif}'; // example glob pattern -> add all .jpg, .jpeg, .png and .gif files
$zipOutputFile->addFilesFromGlob($inputDir, $globPattern, $recursive);
``` $zipFile->addFilesFromGlob($dir, $globPattern);
Add a files from [glob pattern](https://en.wikipedia.org/wiki/Glob_(programming)) to the archive by path `$moveToPath`.
```php // with entry path
$moveToPath = 'dir/dir2/dir3'; $localPath = "to/path/";
$zipOutputFile->addFilesFromGlob($inputDir, $globPattern, $recursive = true, $moveToPath); $zipFile->addFilesFromGlob($dir, $globPattern, $localPath);
```
Add a files from [glob pattern](https://en.wikipedia.org/wiki/Glob_(programming)) to the archive and set compression method. // with compression method for all files
```php $zipFile->addFilesFromGlob($dir, $globPattern, $localPath), ZipFile::METHOD_DEFLATED); // Deflate compression
$compressionMethod = ZipFile::METHOD_DEFLATED; $zipFile->addFilesFromGlob($dir, $globPattern, $localPath), ZipFile::METHOD_STORED); // No compression
$zipOutputFile->addFilesFromGlob($inputDir, $globPattern, $recursive, $moveToPath, $compressionMethod); $zipFile->addFilesFromGlob($dir, $globPattern, $localPath), ZipFile::METHOD_BZIP2); // BZIP2 compression
``` ```
Add a files **recursively** from [RegEx (Regular Expression) pattern](https://en.wikipedia.org/wiki/Regular_expression) to the archive. Add a files **recursively** from [RegEx (Regular Expression) pattern](https://en.wikipedia.org/wiki/Regular_expression) to the archive.
```php ```php
$regexPattern = '/\.(jpe?g|png|gif)$/si'; // example regex pattern -> add all .jpg, .jpeg, .png and .gif files $regexPattern = '/\.(jpe?g|png|gif)$/si'; // example regex pattern -> add all .jpg, .jpeg, .png and .gif files
$zipOutputFile->addFilesFromRegex($inputDir, $regexPattern);
$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
``` ```
Add a files **not recursively** from [RegEx (Regular Expression) pattern](https://en.wikipedia.org/wiki/Regular_expression) to the archive. Add a files **not recursively** from [RegEx (Regular Expression) pattern](https://en.wikipedia.org/wiki/Regular_expression) to the archive.
```php ```php
$recursive = false; $regexPattern = '/\.(jpe?g|png|gif)$/si'; // example regex pattern -> add all .jpg, .jpeg, .png and .gif files
$zipOutputFile->addFilesFromRegex($inputDir, $regexPattern, $recursive);
``` $zipFile->addFilesFromRegex($dir, $regexPattern);
Add a files from [RegEx (Regular Expression) pattern](https://en.wikipedia.org/wiki/Regular_expression) to the archive by path `$moveToPath`.
```php // with entry path
$moveToPath = 'dir/dir2/dir3'; $localPath = "to/path/";
$zipOutputFile->addFilesFromRegex($inputDir, $regexPattern, $recursive = true, $moveToPath); $zipFile->addFilesFromRegex($dir, $regexPattern, $localPath);
```
Add a files from [RegEx (Regular Expression) pattern](https://en.wikipedia.org/wiki/Regular_expression) to the archive and set compression method. // with compression method for all files
```php $zipFile->addFilesFromRegex($dir, $regexPattern, $localPath, ZipFile::METHOD_DEFLATED); // Deflate compression
$compressionMethod = ZipFile::METHOD_DEFLATED; $zipFile->addFilesFromRegex($dir, $regexPattern, $localPath, ZipFile::METHOD_STORED); // No compression
$zipOutputFile->addFilesFromRegex($inputDir, $regexPattern, $recursive, $moveToPath, $compressionMethod); $zipFile->addFilesFromRegex($dir, $regexPattern, $localPath, ZipFile::METHOD_BZIP2); // BZIP2 compression
``` ```
Rename entry name. Rename entry name.
```php ```php
$zipOutputFile->rename($oldName, $newName); $zipFile->rename($oldName, $newName);
``` ```
Delete entry by name. Delete entry by name.
```php ```php
$zipOutputFile->deleteFromName($entryName); $zipFile->deleteFromName($entryName);
``` ```
Delete entries from [glob pattern](https://en.wikipedia.org/wiki/Glob_(programming)). Delete entries from [glob pattern](https://en.wikipedia.org/wiki/Glob_(programming)).
```php ```php
$globPattern = '**.{jpg,jpeg,png,gif}'; // example glob pattern -> delete all .jpg, .jpeg, .png and .gif files $globPattern = '**.{jpg,jpeg,png,gif}'; // example glob pattern -> delete all .jpg, .jpeg, .png and .gif files
$zipOutputFile->deleteFromGlob($globPattern);
$zipFile->deleteFromGlob($globPattern);
``` ```
Delete entries from [RegEx (Regular Expression) pattern](https://en.wikipedia.org/wiki/Regular_expression). Delete entries from [RegEx (Regular Expression) pattern](https://en.wikipedia.org/wiki/Regular_expression).
```php ```php
$regexPattern = '/\.(jpe?g|png|gif)$/si'; // example regex pattern -> delete all .jpg, .jpeg, .png and .gif files $regexPattern = '/\.(jpe?g|png|gif)$/si'; // example regex pattern -> delete all .jpg, .jpeg, .png and .gif files
$zipOutputFile->deleteFromRegex($regexPattern);
$zipFile->deleteFromRegex($regexPattern);
``` ```
Delete all entries. Delete all entries.
```php ```php
$zipOutputFile->deleteAll(); $zipFile->deleteAll();
```
Get num entries.
```php
$count = $zipOutputFile->count();
// or
$count = count($zipOutputFile);
```
Get list files.
```php
$listFiles = $zipOutputFile->getListFiles();
```
Get the compression level for entries.
```php
$compressionLevel = $zipOutputFile->getLevel();
``` ```
Sets the compression level for entries. Sets the compression level for entries.
```php ```php
// This property is only used if the effective compression method is DEFLATED or BZIP2. // This property is only used if the effective compression method is DEFLATED or BZIP2.
// Legal values are ZipOutputFile::LEVEL_DEFAULT_COMPRESSION or range from // Legal values are ZipFile::LEVEL_DEFAULT_COMPRESSION or range from
// ZipOutputFile::LEVEL_BEST_SPEED to ZipOutputFile::LEVEL_BEST_COMPRESSION. // ZipFile::LEVEL_BEST_SPEED to ZipFile::LEVEL_BEST_COMPRESSION.
$compressionMethod = ZipOutputFile::LEVEL_BEST_COMPRESSION;
$zipOutputFile->setLevel($compressionLevel); $compressionMethod = ZipFile::LEVEL_BEST_COMPRESSION;
```
Get comment archive. $zipFile->setCompressionLevel($compressionLevel);
```php
$commentArchive = $zipOutputFile->getComment();
``` ```
Set comment archive. Set comment archive.
```php ```php
$zipOutputFile->setComment($commentArchive); $zipFile->setArchiveComment($commentArchive);
```
Get comment zip entry.
```php
$commentEntry = $zipOutputFile->getEntryComment($entryName);
``` ```
Set comment zip entry. Set comment zip entry.
```php ```php
$zipOutputFile->setEntryComment($entryName, $entryComment); $zipFile->setEntryComment($entryName, $entryComment);
``` ```
Set compression method for zip entry. Set a new password.
```php ```php
$compressionMethod = ZipFile::METHOD_DEFLATED; $zipFile->withNewPassword($password);
$zipOutputMethod->setCompressionMethod($entryName, $compressionMethod);
// Support compression methods:
// ZipFile::METHOD_STORED - no compression
// ZipFile::METHOD_DEFLATED - deflate compression
// ZipFile::METHOD_BZIP2 - bzip2 compression (need bz2 extension)
``` ```
Set a password for all previously added entries. Set a new password and encryption method.
```php
$zipOutputFile->setPassword($password);
```
Set a password and encryption method for all previously added entries.
```php ```php
$encryptionMethod = ZipFile::ENCRYPTION_METHOD_WINZIP_AES; // default value $encryptionMethod = ZipFile::ENCRYPTION_METHOD_WINZIP_AES; // default value
$zipOutputFile->setPassword($password, $encryptionMethod); $zipFile->withNewPassword($password, $encryptionMethod);
// Support encryption methods: // Support encryption methods:
// ZipFile::ENCRYPTION_METHOD_TRADITIONAL - Traditional PKWARE Encryption // ZipFile::ENCRYPTION_METHOD_TRADITIONAL - Traditional PKWARE Encryption
// ZipFile::ENCRYPTION_METHOD_WINZIP_AES - WinZip AES Encryption // ZipFile::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.
```php
$zipOutputFile->setEntryPassword($entryName, $password, $encryptionMethod);
// Support encryption methods:
// ZipFile::ENCRYPTION_METHOD_TRADITIONAL - Traditional PKWARE Encryption
// ZipFile::ENCRYPTION_METHOD_WINZIP_AES - WinZip AES Encryption (default value)
```
Remove password from all entries. Remove password from all entries.
```php ```php
$zipOutputFile->removePasswordAllEntries(); $zipFile->withoutPassword();
``` ```
Remove password for concrete zip entry. #### <a name="Documentation-ZipAlign-Usage"></a> ZipAlign Usage
Set archive alignment ([`zipalign`](https://developer.android.com/studio/command-line/zipalign.html)).
```php ```php
$zipOutputFile->removePasswordFromEntry($entryName); // before save or output
$zipFile->setAlign(4); // alternative command: zipalign -f -v 4 filename.zip
``` ```
#### <a name="Documentation-Save-Or-Output-Entries"></a> Save Zip File or Output
Save archive to a file. Save archive to a file.
```php ```php
$zipOutputFile->saveAsFile($filename); $zipFile->saveAsFile($filename);
``` ```
Save archive to a stream. Save archive to a stream.
```php ```php
$handle = fopen($filename, 'w+b'); // $fp = fopen($filename, 'w+b');
$autoCloseResource = true;
$zipOutputFile->saveAsStream($handle, $autoCloseResource); $zipFile->saveAsStream($fp);
if(!$autoCloseResource){
fclose($handle);
}
``` ```
Returns the zip archive as a string. Returns the zip archive as a string.
```php ```php
$rawZipArchiveBytes = $zipOutputFile->outputAsString(); $rawZipArchiveBytes = $zipFile->outputAsString();
``` ```
Output .ZIP archive as attachment and terminate. Output .ZIP archive as attachment and terminate.
```php ```php
$zipOutputFile->outputAsAttachment($outputFilename); $zipFile->outputAsAttachment($outputFilename);
// or set mime type // or set mime type
$zipOutputFile->outputAsAttachment($outputFilename = 'output.zip', $mimeType = 'application/zip'); $mimeType = 'application/zip'
$zipFile->outputAsAttachment($outputFilename, $mimeType);
``` ```
Extract all files to directory. Rewrite and reopen zip archive.
```php ```php
$zipOutputFile->extractTo($directory); $zipFile->rewrite();
```
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();
echo "Entry: $entryName" . PHP_EOL;
echo "Data: $dataContent" . PHP_EOL;
echo "-----------------------------" . PHP_EOL;
$iterator->next();
}
```
Set zip alignment (alternate program `zipalign`).
```php
// before save or output
$zipOutputFile->setAlign(4); // alternative cmd: zipalign -f -v 4 filename.zip
``` ```
#### <a name="Documentation-Close-Zip-Archive"></a> Close Zip Archive
Close zip archive. Close zip archive.
```php ```php
$zipOutputFile->close(); $zipFile->close();
``` ```
Examples ### <a name="Running-Tests"></a> Running Tests
-------- Installing development dependencies.
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
$zipOutputFile = $zipFile->edit(); // create zip output archive for update
$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
Running Tests
-------------
```bash ```bash
vendor/bin/phpunit -v --tap -c bootstrap.xml composer install --dev
``` ```
Run tests
```bash
vendor/bin/phpunit -v -c bootstrap.xml
```
### <a name="Upgrade"></a> Upgrade version 2 to version 3
Update to the New Major Version via Composer
```json
{
"require": {
"nelexa/zip": "^3.0"
}
}
```
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)` &gt; `(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`

View File

@@ -213,7 +213,7 @@ class TraditionalPkwareEncryptionEngine implements CryptoEngine
*/ */
private function encryptData($content) private function encryptData($content)
{ {
if ($content === null) { if (null === $content) {
throw new ZipCryptoException('content is null'); throw new ZipCryptoException('content is null');
} }
$buff = ''; $buff = '';

View File

@@ -119,7 +119,7 @@ class WinZipAesEngine implements CryptoEngine
$content = substr($content, $start, $size); $content = substr($content, $start, $size);
$mac = hash_hmac('sha1', $content, $sha1MacParam, true); $mac = hash_hmac('sha1', $content, $sha1MacParam, true);
if ($authenticationCode !== substr($mac, 0, 10)) { if (substr($mac, 0, 10) !== $authenticationCode) {
throw new ZipAuthenticationException($this->entry->getName() . throw new ZipAuthenticationException($this->entry->getName() .
" (authenticated WinZip AES entry content has been tampered with)"); " (authenticated WinZip AES entry content has been tampered with)");
} }
@@ -143,7 +143,7 @@ class WinZipAesEngine implements CryptoEngine
for ($i = 0; $i < $numOfBlocks; ++$i) { for ($i = 0; $i < $numOfBlocks; ++$i) {
for ($j = 0; $j < 16; ++$j) { for ($j = 0; $j < 16; ++$j) {
$n = ord($iv[$j]); $n = ord($iv[$j]);
if (++$n === 0x100) { if (0x100 === ++$n) {
// overflow, set this one to 0, increment next // overflow, set this one to 0, increment next
$iv[$j] = chr(0); $iv[$j] = chr(0);
} else { } else {

View File

@@ -45,7 +45,7 @@ abstract class ExtraField implements ExtraFieldHeader
if (isset(self::getRegistry()[$headerId])) { if (isset(self::getRegistry()[$headerId])) {
$extraClassName = self::getRegistry()[$headerId]; $extraClassName = self::getRegistry()[$headerId];
$extraField = new $extraClassName; $extraField = new $extraClassName;
if ($headerId !== $extraField::getHeaderId()) { if ($extraField::getHeaderId() !== $headerId) {
throw new ZipException('Runtime error support headerId ' . $headerId); throw new ZipException('Runtime error support headerId ' . $headerId);
} }
} else { } else {
@@ -61,7 +61,7 @@ abstract class ExtraField implements ExtraFieldHeader
*/ */
private static function getRegistry() private static function getRegistry()
{ {
if (self::$registry === null) { if (null === self::$registry) {
self::$registry[WinZipAesEntryExtraField::getHeaderId()] = WinZipAesEntryExtraField::class; self::$registry[WinZipAesEntryExtraField::getHeaderId()] = WinZipAesEntryExtraField::class;
self::$registry[NtfsExtraField::getHeaderId()] = NtfsExtraField::class; self::$registry[NtfsExtraField::getHeaderId()] = NtfsExtraField::class;
} }

View File

@@ -86,7 +86,7 @@ class NtfsExtraField extends ExtraField
fseek($handle, $off, SEEK_SET); fseek($handle, $off, SEEK_SET);
$unpack = unpack('vtag/vsizeAttr', fread($handle, 4)); $unpack = unpack('vtag/vsizeAttr', fread($handle, 4));
if ($unpack['sizeAttr'] === 24) { if (24 === $unpack['sizeAttr']) {
$tagData = fread($handle, $unpack['sizeAttr']); $tagData = fread($handle, $unpack['sizeAttr']);
$this->mtime = PackUtil::unpackLongLE(substr($tagData, 0, 8)) / 10000000 - 11644473600; $this->mtime = PackUtil::unpackLongLE(substr($tagData, 0, 8)) / 10000000 - 11644473600;
@@ -110,7 +110,7 @@ class NtfsExtraField extends ExtraField
*/ */
public function writeTo($handle, $off) public function writeTo($handle, $off)
{ {
if ($this->mtime !== null && $this->atime !== null && $this->ctime !== null) { if (null !== $this->mtime && null !== $this->atime && null !== $this->ctime) {
fseek($handle, $off, SEEK_SET); fseek($handle, $off, SEEK_SET);
fwrite($handle, pack('Vvv', 0, 1, 8 * 3 + strlen($this->rawData))); fwrite($handle, pack('Vvv', 0, 1, 8 * 3 + strlen($this->rawData)));
$mtimeLong = ($this->mtime + 11644473600) * 10000000; $mtimeLong = ($this->mtime + 11644473600) * 10000000;

View File

@@ -224,7 +224,7 @@ class CentralDirectory
*/ */
public function setZipAlign($zipAlign = null) public function setZipAlign($zipAlign = null)
{ {
if ($zipAlign === null) { if (null === $zipAlign) {
$this->zipAlign = null; $this->zipAlign = null;
return; return;
} }
@@ -331,7 +331,7 @@ class CentralDirectory
if (isset($this->modifiedEntries[$entryName])) continue; if (isset($this->modifiedEntries[$entryName])) continue;
if ( if (
($this->password !== null || $this->clearPassword) && (null !== $this->password || $this->clearPassword) &&
$entry->isEncrypted() && $entry->isEncrypted() &&
$entry->getPassword() !== null && $entry->getPassword() !== null &&
( (
@@ -359,7 +359,7 @@ class CentralDirectory
if (null === $outputEntry) { // remove marked entry if (null === $outputEntry) { // remove marked entry
unset($memoryEntriesResult[$entryName]); unset($memoryEntriesResult[$entryName]);
} else { } else {
if ($this->password !== null) { if (null !== $this->password) {
$outputEntry->setPassword($this->password, $this->encryptionMethod); $outputEntry->setPassword($this->password, $this->encryptionMethod);
} }
$memoryEntriesResult[$entryName] = $outputEntry; $memoryEntriesResult[$entryName] = $outputEntry;

View File

@@ -168,8 +168,7 @@ abstract class ZipAbstractEntry implements ZipEntry
if (0x0000 > $length || $length > 0xffff) { if (0x0000 > $length || $length > 0xffff) {
throw new ZipException('Illegal zip entry name parameter'); throw new ZipException('Illegal zip entry name parameter');
} }
$encoding = mb_detect_encoding($this->name, "ASCII, UTF-8", true); $this->setGeneralPurposeBitFlag(self::GPBF_UTF8, true);
$this->setGeneralPurposeBitFlag(self::GPBF_UTF8, $encoding === 'UTF-8');
$this->name = $name; $this->name = $name;
return $this; return $this;
} }
@@ -792,10 +791,7 @@ abstract class ZipAbstractEntry implements ZipEntry
throw new ZipException("Comment too long"); throw new ZipException("Comment too long");
} }
} }
$encoding = mb_detect_encoding($this->name, "ASCII, UTF-8", true);
if ($encoding === 'UTF-8') {
$this->setGeneralPurposeBitFlag(self::GPBF_UTF8, true); $this->setGeneralPurposeBitFlag(self::GPBF_UTF8, true);
}
$this->comment = $comment; $this->comment = $comment;
return $this; return $this;
} }
@@ -853,7 +849,7 @@ abstract class ZipAbstractEntry implements ZipEntry
public function setPassword($password, $encryptionMethod = null) public function setPassword($password, $encryptionMethod = null)
{ {
$this->password = $password; $this->password = $password;
if ($encryptionMethod !== null) { if (null !== $encryptionMethod) {
$this->setEncryptionMethod($encryptionMethod); $this->setEncryptionMethod($encryptionMethod);
} }
$this->setEncrypted(!empty($this->password)); $this->setEncrypted(!empty($this->password));

View File

@@ -240,7 +240,7 @@ abstract class ZipNewEntry extends ZipAbstractEntry
fwrite($outputStream, str_repeat(chr(0), $padding)); fwrite($outputStream, str_repeat(chr(0), $padding));
} }
if ($entryContent !== null) { if (null !== $entryContent) {
fwrite($outputStream, $entryContent); fwrite($outputStream, $entryContent);
} }

View File

@@ -47,7 +47,7 @@ class ZipNewStreamEntry extends ZipNewEntry
*/ */
function __destruct() function __destruct()
{ {
if ($this->stream !== null) { if (null !== $this->stream) {
fclose($this->stream); fclose($this->stream);
$this->stream = null; $this->stream = null;
} }

View File

@@ -118,7 +118,7 @@ class ZipReadEntry extends ZipAbstractEntry
*/ */
public function getEntryContent() public function getEntryContent()
{ {
if ($this->entryContent === null) { if (null === $this->entryContent) {
if ($this->isDirectory()) { if ($this->isDirectory()) {
$this->entryContent = null; $this->entryContent = null;
return $this->entryContent; return $this->entryContent;
@@ -319,7 +319,7 @@ class ZipReadEntry extends ZipAbstractEntry
function __destruct() function __destruct()
{ {
if ($this->entryContent !== null && is_resource($this->entryContent)) { if (null !== $this->entryContent && is_resource($this->entryContent)) {
fclose($this->entryContent); fclose($this->entryContent);
} }
} }

View File

@@ -193,7 +193,7 @@ class ZipInfo
$ctime = null; $ctime = null;
$field = $entry->getExtraField(NtfsExtraField::getHeaderId()); $field = $entry->getExtraField(NtfsExtraField::getHeaderId());
if ($field !== null && $field instanceof NtfsExtraField) { if (null !== $field && $field instanceof NtfsExtraField) {
/** /**
* @var NtfsExtraField $field * @var NtfsExtraField $field
*/ */
@@ -311,7 +311,7 @@ class ZipInfo
if ($entry->getMethod() === ZipEntry::METHOD_WINZIP_AES) { if ($entry->getMethod() === ZipEntry::METHOD_WINZIP_AES) {
$field = $entry->getExtraField(WinZipAesEntryExtraField::getHeaderId()); $field = $entry->getExtraField(WinZipAesEntryExtraField::getHeaderId());
$return = ucfirst(self::$valuesCompressionMethod[$entry->getMethod()]); $return = ucfirst(self::$valuesCompressionMethod[$entry->getMethod()]);
if ($field !== null) { if (null !== $field) {
/** /**
* @var WinZipAesEntryExtraField $field * @var WinZipAesEntryExtraField $field
*/ */

View File

@@ -356,7 +356,7 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator
touch($dir, $entry->getTime()); touch($dir, $entry->getTime());
} }
if (file_put_contents($file, $entry->getEntryContent()) === false) { if (file_put_contents($file, $entry->getEntryContent()) === false) {
throw new ZipException('Can not extract file '.$entry->getName()); throw new ZipException('Can not extract file ' . $entry->getName());
} }
touch($file, $entry->getTime()); touch($file, $entry->getTime());
} }
@@ -427,7 +427,7 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator
*/ */
public function addFile($filename, $localName = null, $compressionMethod = null) public function addFile($filename, $localName = null, $compressionMethod = null)
{ {
if ($filename === null) { if (null === $filename) {
throw new InvalidArgumentException("Filename is null"); throw new InvalidArgumentException("Filename is null");
} }
if (!is_file($filename)) { if (!is_file($filename)) {
@@ -438,9 +438,9 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator
if (function_exists('mime_content_type')) { if (function_exists('mime_content_type')) {
$mimeType = @mime_content_type($filename); $mimeType = @mime_content_type($filename);
$type = strtok($mimeType, '/'); $type = strtok($mimeType, '/');
if ($type === 'image') { if ('image' === $type) {
$compressionMethod = self::METHOD_STORED; $compressionMethod = self::METHOD_STORED;
} elseif ($type === 'text' && filesize($filename) < 150) { } elseif ('text' === $type && filesize($filename) < 150) {
$compressionMethod = self::METHOD_STORED; $compressionMethod = self::METHOD_STORED;
} else { } else {
$compressionMethod = self::METHOD_DEFLATED; $compressionMethod = self::METHOD_DEFLATED;
@@ -457,7 +457,7 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator
if (!($handle = @fopen($filename, 'rb'))) { if (!($handle = @fopen($filename, 'rb'))) {
throw new InvalidArgumentException('File ' . $filename . ' can not open.'); throw new InvalidArgumentException('File ' . $filename . ' can not open.');
} }
if ($localName === null) { if (null === $localName) {
$localName = basename($filename); $localName = basename($filename);
} }
$this->addFromStream($handle, $localName, $compressionMethod); $this->addFromStream($handle, $localName, $compressionMethod);
@@ -570,7 +570,7 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator
public function addDir($inputDir, $localPath = "/", $compressionMethod = null) public function addDir($inputDir, $localPath = "/", $compressionMethod = null)
{ {
$inputDir = (string)$inputDir; $inputDir = (string)$inputDir;
if ($inputDir === null || strlen($inputDir) === 0) { if (null === $inputDir || strlen($inputDir) === 0) {
throw new InvalidArgumentException('Input dir empty'); throw new InvalidArgumentException('Input dir empty');
} }
if (!is_dir($inputDir)) { if (!is_dir($inputDir)) {
@@ -600,7 +600,7 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator
public function addDirRecursive($inputDir, $localPath = "/", $compressionMethod = null) public function addDirRecursive($inputDir, $localPath = "/", $compressionMethod = null)
{ {
$inputDir = (string)$inputDir; $inputDir = (string)$inputDir;
if ($inputDir === null || strlen($inputDir) === 0) { if (null === $inputDir || strlen($inputDir) === 0) {
throw new InvalidArgumentException('Input dir empty'); throw new InvalidArgumentException('Input dir empty');
} }
if (!is_dir($inputDir)) { if (!is_dir($inputDir)) {
@@ -660,7 +660,7 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator
} }
} }
} }
if(empty($files)){ if (empty($files)) {
return $this; return $this;
} }
@@ -752,7 +752,7 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator
$globPattern = $inputDir . $globPattern; $globPattern = $inputDir . $globPattern;
$filesFound = FilesUtil::globFileSearch($globPattern, GLOB_BRACE, $recursive); $filesFound = FilesUtil::globFileSearch($globPattern, GLOB_BRACE, $recursive);
if ($filesFound === false || empty($filesFound)) { if (false === $filesFound || empty($filesFound)) {
return $this; return $this;
} }
if (!empty($localPath) && is_string($localPath)) { if (!empty($localPath) && is_string($localPath)) {
@@ -846,7 +846,7 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator
$inputDir = rtrim($inputDir, '/\\') . DIRECTORY_SEPARATOR; $inputDir = rtrim($inputDir, '/\\') . DIRECTORY_SEPARATOR;
$files = FilesUtil::regexFileSearch($inputDir, $regexPattern, $recursive); $files = FilesUtil::regexFileSearch($inputDir, $regexPattern, $recursive);
if ($files === false || empty($files)) { if (false === $files || empty($files)) {
return $this; return $this;
} }
if (!empty($localPath) && is_string($localPath)) { if (!empty($localPath) && is_string($localPath)) {
@@ -882,7 +882,7 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator
*/ */
public function rename($oldName, $newName) public function rename($oldName, $newName)
{ {
if ($oldName === null || $newName === null) { if (null === $oldName || null === $newName) {
throw new InvalidArgumentException("name is null"); throw new InvalidArgumentException("name is null");
} }
$this->centralDirectory->rename($oldName, $newName); $this->centralDirectory->rename($oldName, $newName);
@@ -913,7 +913,7 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator
*/ */
public function deleteFromGlob($globPattern) public function deleteFromGlob($globPattern)
{ {
if ($globPattern === null || !is_string($globPattern) || empty($globPattern)) { if (null === $globPattern || !is_string($globPattern) || empty($globPattern)) {
throw new InvalidArgumentException("Glob pattern is empty"); throw new InvalidArgumentException("Glob pattern is empty");
} }
$globPattern = '~' . FilesUtil::convertGlobToRegEx($globPattern) . '~si'; $globPattern = '~' . FilesUtil::convertGlobToRegEx($globPattern) . '~si';
@@ -930,7 +930,7 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator
*/ */
public function deleteFromRegex($regexPattern) public function deleteFromRegex($regexPattern)
{ {
if ($regexPattern === null || !is_string($regexPattern) || empty($regexPattern)) { if (null === $regexPattern || !is_string($regexPattern) || empty($regexPattern)) {
throw new InvalidArgumentException("Regex pattern is empty."); throw new InvalidArgumentException("Regex pattern is empty.");
} }
$this->centralDirectory->deleteEntriesFromRegex($regexPattern); $this->centralDirectory->deleteEntriesFromRegex($regexPattern);
@@ -1087,13 +1087,13 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator
*/ */
public function rewrite() public function rewrite()
{ {
if($this->inputStream === null){ if (null === $this->inputStream) {
throw new ZipException('input stream is null'); throw new ZipException('input stream is null');
} }
$meta = stream_get_meta_data($this->inputStream); $meta = stream_get_meta_data($this->inputStream);
$content = $this->outputAsString(); $content = $this->outputAsString();
$this->close(); $this->close();
if ($meta['wrapper_type'] === 'plainfile') { if ('plainfile' === $meta['wrapper_type']) {
if (file_put_contents($meta['uri'], $content) === false) { if (file_put_contents($meta['uri'], $content) === false) {
throw new ZipException("Can not overwrite the zip file in the {$meta['uri']} file."); throw new ZipException("Can not overwrite the zip file in the {$meta['uri']} file.");
} }
@@ -1110,11 +1110,11 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator
*/ */
public function close() public function close()
{ {
if ($this->inputStream !== null) { if (null !== $this->inputStream) {
fclose($this->inputStream); fclose($this->inputStream);
$this->inputStream = null; $this->inputStream = null;
} }
if ($this->centralDirectory !== null) { if (null !== $this->centralDirectory) {
$this->centralDirectory->release(); $this->centralDirectory->release();
$this->centralDirectory = null; $this->centralDirectory = null;
} }
@@ -1165,7 +1165,7 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator
*/ */
public function offsetSet($entryName, $contents) public function offsetSet($entryName, $contents)
{ {
if ($entryName === null) { if (null === $entryName) {
throw new InvalidArgumentException('entryName is null'); throw new InvalidArgumentException('entryName is null');
} }
$entryName = (string)$entryName; $entryName = (string)$entryName;
@@ -1181,7 +1181,7 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator
return; return;
} }
$contents = (string)$contents; $contents = (string)$contents;
if ($entryName[strlen($entryName) - 1] === '/') { if ('/' === $entryName[strlen($entryName) - 1]) {
$this->addEmptyDir($entryName); $this->addEmptyDir($entryName);
} else { } else {
$this->addFromString($entryName, $contents); $this->addFromString($entryName, $contents);