1
0
mirror of https://github.com/Ne-Lexa/php-zip.git synced 2025-07-31 04:30:13 +02:00

Implemented the ability to override the instance of ZipContainer.

ZipContainer will be cloned before writing the zip file.
Tested custom ZipWriter, ZipReader, ZipContainer and ZipFile.
This commit is contained in:
Ne-Lexa
2020-01-21 14:10:47 +03:00
parent e0da8c94be
commit 5ec656fde4
15 changed files with 472 additions and 7 deletions

View File

@@ -0,0 +1,66 @@
<?php
namespace PhpZip\Tests\Internal\Epub;
use PhpZip\Constants\ZipCompressionMethod;
use PhpZip\Constants\ZipPlatform;
use PhpZip\Exception\ZipUnsupportMethodException;
use PhpZip\IO\ZipWriter;
use PhpZip\Model\Data\ZipNewData;
use PhpZip\Model\ZipEntry;
/**
* Class EpubWriter.
*
* @property EpubZipContainer $zipContainer
*/
class EpubWriter extends ZipWriter
{
/**
* @throws ZipUnsupportMethodException
*/
protected function beforeWrite()
{
parent::beforeWrite();
if (!$this->zipContainer->hasEntry('mimetype')) {
$zipEntry = new ZipEntry('mimetype');
$zipEntry->setCreatedOS(ZipPlatform::OS_DOS);
$zipEntry->setExtractedOS(ZipPlatform::OS_DOS);
$zipEntry->setCompressionMethod(ZipCompressionMethod::STORED);
$zipEntry->setData(new ZipNewData($zipEntry, 'application/epub+zip'));
$this->zipContainer->addEntry($zipEntry);
}
$this->sortEntries();
}
private function sortEntries()
{
$this->zipContainer->sortByEntry(
static function (ZipEntry $a, ZipEntry $b) {
if (strcasecmp($a->getName(), 'mimetype') === 0) {
return -1;
}
if (strcasecmp($b->getName(), 'mimetype') === 0) {
return 1;
}
if ($a->isDirectory() && $b->isDirectory()) {
return strcmp($a->getName(), $b->getName());
}
if ($a->isDirectory()) {
return -1;
}
if ($b->isDirectory()) {
return 1;
}
return strcmp($a->getName(), $b->getName());
}
);
}
}