From 58e9f4bf7364b880a6d6ca0caceb6add5981300a Mon Sep 17 00:00:00 2001 From: wapplay-home-linux Date: Thu, 2 Mar 2017 00:16:09 +0300 Subject: [PATCH] Easy to initialize the output zip file. --- CHANGELOG.md | 5 +++++ README.md | 24 +++++++++++++++++++----- src/PhpZip/ZipFile.php | 7 +++++++ src/PhpZip/ZipOutputFile.php | 16 ++++++++++++++++ tests/PhpZip/ZipTest.php | 7 +++---- 5 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..7636218 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +## 2.2.0 (2017-03-02) + +Features: + - create output object `ZipOutputFile` from `ZipFile` in method `ZipFile::edit()`. + - create output object `ZipOutputFile` from filename in static method `ZipOutputFile::openFromFile(string $filename)`. \ No newline at end of file diff --git a/README.md b/README.md index 0e35c60..6037fa4 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,10 @@ Get entry content. ```php $data = $zipFile->getEntryContent($entryName); ``` +Edit zip archive +```php +$zipOutputFile = $zipFile->edit(); +``` Close zip archive. ```php $zipFile->close(); @@ -162,6 +166,11 @@ $zipOutputFile = \PhpZip\ZipOutputFile::create(); ``` Open zip file from update. ```php +$filename = "file.zip"; +$zipOutputFile = \PhpZip\ZipOutputFile::openFromFile($filename); +``` +or +```php // initial ZipFile $zipFile = \PhpZip\ZipFile::openFromFile($filename); @@ -169,23 +178,28 @@ $zipFile = \PhpZip\ZipFile::openFromFile($filename); $zipOutputFile = new \PhpZip\ZipOutputFile($zipFile); // or $zipOutputFile = \PhpZip\ZipOutputFile::openFromZipFile($zipFile); +// or +$zipOutputFile = $zipFile->edit(); ``` Add entry from file. ```php $zipOutputFile->addFromFile($filename); // $entryName == basename($filename); $zipOutputFile->addFromFile($filename, $entryName); $zipOutputFile->addFromFile($filename, $entryName, ZipEntry::METHOD_DEFLATED); +$zipOutputFile->addFromFile($filename, $entryName, ZipEntry::METHOD_STORED); // no compress $zipOutputFile->addFromFile($filename, null, ZipEntry::METHOD_BZIP2); // $entryName == basename($filename); ``` Add entry from string data. ```php -$zipOutputFile->addFromString($entryName, $data) -$zipOutputFile->addFromString($entryName, $data, ZipEntry::METHOD_DEFLATED) +$zipOutputFile->addFromString($entryName, $data); +$zipOutputFile->addFromString($entryName, $data, ZipEntry::METHOD_DEFLATED); +$zipOutputFile->addFromString($entryName, $data, ZipEntry::METHOD_STORED); // no compress ``` Add entry from stream. ```php -$zipOutputFile->addFromStream($stream, $entryName) -$zipOutputFile->addFromStream($stream, $entryName, ZipEntry::METHOD_DEFLATED) +$zipOutputFile->addFromStream($stream, $entryName); +$zipOutputFile->addFromStream($stream, $entryName, ZipEntry::METHOD_DEFLATED); +$zipOutputFile->addFromStream($stream, $entryName, ZipEntry::METHOD_STORED); // no compress ``` Add empty dir ```php @@ -446,7 +460,7 @@ $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 = \PhpZip\ZipOutputFile::openFromZipFile($zipFile); // create zip output archive for update +$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 diff --git a/src/PhpZip/ZipFile.php b/src/PhpZip/ZipFile.php index dd870d4..61fad42 100644 --- a/src/PhpZip/ZipFile.php +++ b/src/PhpZip/ZipFile.php @@ -157,6 +157,13 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants return $zipFile; } + /** + * @return ZipOutputFile + */ + public function edit(){ + return ZipOutputFile::openFromZipFile($this); + } + /** * Check zip file signature * diff --git a/src/PhpZip/ZipOutputFile.php b/src/PhpZip/ZipOutputFile.php index 0f2fe5a..8ebc132 100644 --- a/src/PhpZip/ZipOutputFile.php +++ b/src/PhpZip/ZipOutputFile.php @@ -152,6 +152,22 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants return new self($zipFile); } + /** + * Open zip file from update. + * + * @param string $filename + * @return ZipOutputFile + * @throws IllegalArgumentException + * @see ZipOutputFile::__construct() + */ + public static function openFromFile($filename) + { + if (empty($filename)) { + throw new IllegalArgumentException("Zip file is null"); + } + return new self(ZipFile::openFromFile($filename)); + } + /** * Count zip entries. * diff --git a/tests/PhpZip/ZipTest.php b/tests/PhpZip/ZipTest.php index eb2603b..8d9b766 100644 --- a/tests/PhpZip/ZipTest.php +++ b/tests/PhpZip/ZipTest.php @@ -279,11 +279,9 @@ class ZipTest extends ZipTestCase self::assertCorrectZipArchive($this->outputFilename); - $zipFile = ZipFile::openFromFile($this->outputFilename); - $outputZipFile = new ZipOutputFile($zipFile); + $outputZipFile = ZipOutputFile::openFromFile($this->outputFilename); $outputZipFile->rename($oldName, $newName); $outputZipFile->saveAsFile($this->outputFilename); - $zipFile->close(); self::assertCorrectZipArchive($this->outputFilename); @@ -309,7 +307,7 @@ class ZipTest extends ZipTestCase self::assertCorrectZipArchive($this->outputFilename); $zipFile = ZipFile::openFromFile($this->outputFilename); - $outputZipFile = new ZipOutputFile($zipFile); + $outputZipFile = $zipFile->edit(); $outputZipFile->deleteFromName($deleteEntryName); $outputZipFile->saveAsFile($this->outputFilename); $zipFile->close(); @@ -1087,6 +1085,7 @@ class ZipTest extends ZipTestCase $zipFile = ZipFile::openFromFile($this->outputFilename); self::assertEquals($zipFile->count(), $countFiles); foreach ($zipFile as $entry => $content) { + strlen($content); } $zipFile->close(); }