mirror of
https://github.com/Ne-Lexa/php-zip.git
synced 2025-08-01 21:20:09 +02:00
Added an event that runs before the archive is saved or output.
This commit is contained in:
@@ -610,7 +610,8 @@ class ZipFile implements ZipFileInterface
|
|||||||
\Iterator $iterator,
|
\Iterator $iterator,
|
||||||
$localPath = '/',
|
$localPath = '/',
|
||||||
$compressionMethod = null
|
$compressionMethod = null
|
||||||
) {
|
)
|
||||||
|
{
|
||||||
$localPath = (string)$localPath;
|
$localPath = (string)$localPath;
|
||||||
if (null !== $localPath && 0 !== strlen($localPath)) {
|
if (null !== $localPath && 0 !== strlen($localPath)) {
|
||||||
$localPath = rtrim($localPath, '/');
|
$localPath = rtrim($localPath, '/');
|
||||||
@@ -694,7 +695,8 @@ class ZipFile implements ZipFileInterface
|
|||||||
$localPath = '/',
|
$localPath = '/',
|
||||||
$recursive = true,
|
$recursive = true,
|
||||||
$compressionMethod = null
|
$compressionMethod = null
|
||||||
) {
|
)
|
||||||
|
{
|
||||||
$inputDir = (string)$inputDir;
|
$inputDir = (string)$inputDir;
|
||||||
if (null === $inputDir || 0 === strlen($inputDir)) {
|
if (null === $inputDir || 0 === strlen($inputDir)) {
|
||||||
throw new InvalidArgumentException('Input dir empty');
|
throw new InvalidArgumentException('Input dir empty');
|
||||||
@@ -789,7 +791,8 @@ class ZipFile implements ZipFileInterface
|
|||||||
$localPath = "/",
|
$localPath = "/",
|
||||||
$recursive = true,
|
$recursive = true,
|
||||||
$compressionMethod = null
|
$compressionMethod = null
|
||||||
) {
|
)
|
||||||
|
{
|
||||||
$regexPattern = (string)$regexPattern;
|
$regexPattern = (string)$regexPattern;
|
||||||
if (empty($regexPattern)) {
|
if (empty($regexPattern)) {
|
||||||
throw new InvalidArgumentException("regex pattern empty");
|
throw new InvalidArgumentException("regex pattern empty");
|
||||||
@@ -1311,6 +1314,8 @@ class ZipFile implements ZipFileInterface
|
|||||||
*/
|
*/
|
||||||
protected function writeZipToStream($handle)
|
protected function writeZipToStream($handle)
|
||||||
{
|
{
|
||||||
|
$this->onBeforeSave();
|
||||||
|
|
||||||
$output = new ZipOutputStream($handle, $this->zipModel);
|
$output = new ZipOutputStream($handle, $this->zipModel);
|
||||||
$output->writeZip();
|
$output->writeZip();
|
||||||
}
|
}
|
||||||
@@ -1332,6 +1337,13 @@ class ZipFile implements ZipFileInterface
|
|||||||
return $content;
|
return $content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event before save or output.
|
||||||
|
*/
|
||||||
|
protected function onBeforeSave()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Close zip archive and release input stream.
|
* Close zip archive and release input stream.
|
||||||
*/
|
*/
|
||||||
|
42
tests/PhpZip/ZipEventTest.php
Normal file
42
tests/PhpZip/ZipEventTest.php
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PhpZip;
|
||||||
|
|
||||||
|
class ZipFileExtended extends ZipFile
|
||||||
|
{
|
||||||
|
protected function onBeforeSave()
|
||||||
|
{
|
||||||
|
parent::onBeforeSave();
|
||||||
|
$this->setZipAlign(4);
|
||||||
|
$this->deleteFromRegex('~^META\-INF/~i');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ZipEventTest extends ZipTestCase
|
||||||
|
{
|
||||||
|
public function testBeforeSave()
|
||||||
|
{
|
||||||
|
$zipFile = new ZipFileExtended();
|
||||||
|
$zipFile->openFile(__DIR__ . '/resources/test.apk');
|
||||||
|
self::assertTrue(isset($zipFile['META-INF/MANIFEST.MF']));
|
||||||
|
self::assertTrue(isset($zipFile['META-INF/CERT.SF']));
|
||||||
|
self::assertTrue(isset($zipFile['META-INF/CERT.RSA']));
|
||||||
|
$zipFile->saveAsFile($this->outputFilename);
|
||||||
|
self::assertFalse(isset($zipFile['META-INF/MANIFEST.MF']));
|
||||||
|
self::assertFalse(isset($zipFile['META-INF/CERT.SF']));
|
||||||
|
self::assertFalse(isset($zipFile['META-INF/CERT.RSA']));
|
||||||
|
$zipFile->close();
|
||||||
|
|
||||||
|
self::assertCorrectZipArchive($this->outputFilename);
|
||||||
|
$result = self::doZipAlignVerify($this->outputFilename);
|
||||||
|
if (null !== $result) {
|
||||||
|
self::assertTrue($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
$zipFile->openFile($this->outputFilename);
|
||||||
|
self::assertFalse(isset($zipFile['META-INF/MANIFEST.MF']));
|
||||||
|
self::assertFalse(isset($zipFile['META-INF/CERT.SF']));
|
||||||
|
self::assertFalse(isset($zipFile['META-INF/CERT.RSA']));
|
||||||
|
$zipFile->close();
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user