1
0
mirror of https://github.com/Ne-Lexa/php-zip.git synced 2025-01-17 20:58:22 +01:00

Easy to initialize the output zip file.

This commit is contained in:
wapplay-home-linux 2017-03-02 00:16:09 +03:00
parent 8d140cc1a1
commit 58e9f4bf73
5 changed files with 50 additions and 9 deletions

5
CHANGELOG.md Normal file
View File

@ -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)`.

View File

@ -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

View File

@ -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
*

View File

@ -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.
*

View File

@ -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();
}