mirror of
https://github.com/Ne-Lexa/php-zip.git
synced 2025-08-06 15:36:28 +02:00
Add ZipModel for all changes.
This commit is contained in:
143
tests/PhpZip/PhpZipExtResourceTest.php
Normal file
143
tests/PhpZip/PhpZipExtResourceTest.php
Normal file
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
|
||||
namespace PhpZip;
|
||||
|
||||
use PhpZip\Exception\ZipAuthenticationException;
|
||||
|
||||
/**
|
||||
* Some tests from the official extension of php-zip.
|
||||
*/
|
||||
class PhpZipExtResourceTest extends ZipTestCase
|
||||
{
|
||||
/**
|
||||
* Bug #7214 (zip_entry_read() binary safe)
|
||||
* @see https://github.com/php/php-src/blob/master/ext/zip/tests/bug7214.phpt
|
||||
*/
|
||||
public function testBinaryNull()
|
||||
{
|
||||
$filename = __DIR__ . '/php-zip-ext-test-resources/binarynull.zip';
|
||||
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->openFile($filename);
|
||||
foreach ($zipFile as $name => $contents) {
|
||||
$info = $zipFile->getEntryInfo($name);
|
||||
self::assertEquals(strlen($contents), $info->getSize());
|
||||
}
|
||||
$zipFile->close();
|
||||
|
||||
self::assertCorrectZipArchive($filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bug #8009 (cannot add again same entry to an archive)
|
||||
* @see https://github.com/php/php-src/blob/master/ext/zip/tests/bug8009.phpt
|
||||
*/
|
||||
public function testBug8009()
|
||||
{
|
||||
$filename = __DIR__ . '/php-zip-ext-test-resources/bug8009.zip';
|
||||
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->openFile($filename);
|
||||
$zipFile->addFromString('2.txt', '=)');
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename);
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
self::assertCount(2, $zipFile);
|
||||
self::assertTrue(isset($zipFile['1.txt']));
|
||||
self::assertTrue(isset($zipFile['2.txt']));
|
||||
self::assertEquals($zipFile['2.txt'], $zipFile['1.txt']);
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Bug #40228 (extractTo does not create recursive empty path)
|
||||
* @see https://github.com/php/php-src/blob/master/ext/zip/tests/bug40228.phpt
|
||||
* @see https://github.com/php/php-src/blob/master/ext/zip/tests/bug40228-mb.phpt
|
||||
* @dataProvider provideBug40228
|
||||
* @param string $filename
|
||||
*/
|
||||
public function testBug40228($filename)
|
||||
{
|
||||
self::assertTrue(mkdir($this->outputDirname, 0755, true));
|
||||
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->openFile($filename);
|
||||
$zipFile->extractTo($this->outputDirname);
|
||||
$zipFile->close();
|
||||
|
||||
self::assertTrue(is_dir($this->outputDirname . '/test/empty'));
|
||||
}
|
||||
|
||||
public function provideBug40228()
|
||||
{
|
||||
return [
|
||||
[__DIR__ . '/php-zip-ext-test-resources/bug40228.zip'],
|
||||
[__DIR__ . '/php-zip-ext-test-resources/bug40228私はガラスを食べられます.zip'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Bug #49072 (feof never returns true for damaged file in zip)
|
||||
* @see https://github.com/php/php-src/blob/master/ext/zip/tests/bug49072.phpt
|
||||
* @expectedException \PhpZip\Exception\Crc32Exception
|
||||
* @expectedExceptionMessage file1 (expected CRC32 value 0xc935c834, but is actually 0x76301511)
|
||||
*/
|
||||
public function testBug49072()
|
||||
{
|
||||
$filename = __DIR__ . '/php-zip-ext-test-resources/bug49072.zip';
|
||||
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->openFile($filename);
|
||||
$zipFile->getEntryContents('file1');
|
||||
}
|
||||
|
||||
/**
|
||||
* Bug #70752 (Depacking with wrong password leaves 0 length files)
|
||||
* @see https://github.com/php/php-src/blob/master/ext/zip/tests/bug70752.phpt
|
||||
* @expectedException \PhpZip\Exception\ZipAuthenticationException
|
||||
* @expectedExceptionMessage Bad password for entry bug70752.txt
|
||||
*/
|
||||
public function testBug70752()
|
||||
{
|
||||
$filename = __DIR__ . '/php-zip-ext-test-resources/bug70752.zip';
|
||||
|
||||
self::assertTrue(mkdir($this->outputDirname, 0755, true));
|
||||
|
||||
$zipFile = new ZipFile();
|
||||
try {
|
||||
$zipFile->openFile($filename);
|
||||
$zipFile->setReadPassword('bar');
|
||||
$zipFile->extractTo($this->outputDirname);
|
||||
self::markTestIncomplete('failed test');
|
||||
} catch (ZipAuthenticationException $exception) {
|
||||
self::assertFalse(file_exists($this->outputDirname . '/bug70752.txt'));
|
||||
$zipFile->close();
|
||||
throw $exception;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Bug #12414 ( extracting files from damaged archives)
|
||||
* @see https://github.com/php/php-src/blob/master/ext/zip/tests/pecl12414.phpt
|
||||
*/
|
||||
public function testPecl12414()
|
||||
{
|
||||
$filename = __DIR__ . '/php-zip-ext-test-resources/pecl12414.zip';
|
||||
|
||||
$entryName = 'MYLOGOV2.GFX';
|
||||
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->openFile($filename);
|
||||
|
||||
$info = $zipFile->getEntryInfo($entryName);
|
||||
self::assertTrue($info->getSize() > 0);
|
||||
|
||||
$contents = $zipFile[$entryName];
|
||||
self::assertEquals(strlen($contents), $info->getSize());
|
||||
|
||||
$zipFile->close();
|
||||
}
|
||||
}
|
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace PhpZip;
|
||||
|
||||
use PhpZip\Util\Iterator\IgnoreFilesFilterIterator;
|
||||
@@ -9,7 +10,7 @@ use PhpZip\Util\Iterator\IgnoreFilesRecursiveFilterIterator;
|
||||
*/
|
||||
class ZipFileAddDirTest extends ZipTestCase
|
||||
{
|
||||
private static $files = [
|
||||
protected static $files = [
|
||||
'.hidden' => 'Hidden file',
|
||||
'text file.txt' => 'Text file',
|
||||
'Текстовый документ.txt' => 'Текстовый документ',
|
||||
@@ -52,7 +53,7 @@ class ZipFileAddDirTest extends ZipTestCase
|
||||
}
|
||||
}
|
||||
|
||||
protected static function assertFilesResult(ZipFile $zipFile, array $actualResultFiles = [], $localPath = '/')
|
||||
protected static function assertFilesResult(ZipFileInterface $zipFile, array $actualResultFiles = [], $localPath = '/')
|
||||
{
|
||||
$localPath = rtrim($localPath, '/');
|
||||
$localPath = empty($localPath) ? "" : $localPath . '/';
|
||||
@@ -134,6 +135,29 @@ class ZipFileAddDirTest extends ZipTestCase
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
public function testAddFilesFromIteratorEmptyLocalPath()
|
||||
{
|
||||
$localPath = '';
|
||||
|
||||
$directoryIterator = new \DirectoryIterator($this->outputDirname);
|
||||
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->addFilesFromIterator($directoryIterator, $localPath);
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename);
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
self::assertFilesResult($zipFile, [
|
||||
'.hidden',
|
||||
'text file.txt',
|
||||
'Текстовый документ.txt',
|
||||
'empty dir/',
|
||||
]);
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
public function testAddFilesFromRecursiveIterator()
|
||||
{
|
||||
$localPath = 'to/project';
|
||||
@@ -182,7 +206,8 @@ class ZipFileAddDirTest extends ZipTestCase
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
public function testAddFilesFromIteratorWithIgnoreFiles(){
|
||||
public function testAddFilesFromIteratorWithIgnoreFiles()
|
||||
{
|
||||
$localPath = 'to/project';
|
||||
$ignoreFiles = [
|
||||
'Текстовый документ.txt',
|
||||
@@ -207,7 +232,8 @@ class ZipFileAddDirTest extends ZipTestCase
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
public function testAddFilesFromRecursiveIteratorWithIgnoreFiles(){
|
||||
public function testAddFilesFromRecursiveIteratorWithIgnoreFiles()
|
||||
{
|
||||
$localPath = 'to/project';
|
||||
$ignoreFiles = [
|
||||
'.hidden',
|
||||
@@ -354,6 +380,4 @@ class ZipFileAddDirTest extends ZipTestCase
|
||||
self::assertFilesResult($zipFile, array_keys(self::$files), $localPath);
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -1,10 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace PhpZip;
|
||||
|
||||
use PhpZip\Exception\ZipAuthenticationException;
|
||||
use PhpZip\Model\ZipEntry;
|
||||
use PhpZip\Model\ZipInfo;
|
||||
use PhpZip\Util\CryptoUtil;
|
||||
use PhpZip\Util\FilesUtil;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
/**
|
||||
* ZipFile test
|
||||
@@ -121,9 +123,33 @@ class ZipFileTest extends ZipTestCase
|
||||
public function testOpenFromStreamInvalidResourceType()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
/** @noinspection PhpParamsInspection */
|
||||
$zipFile->openFromStream("stream resource");
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \PhpZip\Exception\InvalidArgumentException
|
||||
* @expectedExceptionMessage Invalid resource type - gd.
|
||||
*/
|
||||
public function testOpenFromStreamInvalidResourceType2()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
if (!extension_loaded("gd")) {
|
||||
$this->markTestSkipped('not extension gd');
|
||||
}
|
||||
$zipFile->openFromStream(imagecreate(1, 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \PhpZip\Exception\InvalidArgumentException
|
||||
* @expectedExceptionMessage Invalid stream type - dir.
|
||||
*/
|
||||
public function testOpenFromStreamInvalidResourceType3()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->openFromStream(opendir(__DIR__));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \PhpZip\Exception\InvalidArgumentException
|
||||
* @expectedExceptionMessage Resource cannot seekable stream.
|
||||
@@ -169,8 +195,8 @@ class ZipFileTest extends ZipTestCase
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile
|
||||
->addFromString('file', 'content')
|
||||
->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
->saveAsFile($this->outputFilename)
|
||||
->close();
|
||||
|
||||
$handle = fopen($this->outputFilename, 'rb');
|
||||
$zipFile->openFromStream($handle);
|
||||
@@ -186,16 +212,18 @@ class ZipFileTest extends ZipTestCase
|
||||
public function testEmptyArchive()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
$zipFile
|
||||
->saveAsFile($this->outputFilename)
|
||||
->close();
|
||||
|
||||
self::assertCorrectEmptyZip($this->outputFilename);
|
||||
self::assertTrue(mkdir($this->outputDirname, 0755, true));
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
self::assertEquals($zipFile->count(), 0);
|
||||
$zipFile->extractTo($this->outputDirname);
|
||||
$zipFile->close();
|
||||
$zipFile
|
||||
->extractTo($this->outputDirname)
|
||||
->close();
|
||||
|
||||
self::assertTrue(FilesUtil::isEmptyDir($this->outputDirname));
|
||||
}
|
||||
@@ -213,18 +241,23 @@ class ZipFileTest extends ZipTestCase
|
||||
$fileExpected = $this->outputDirname . DIRECTORY_SEPARATOR . 'file_expected.zip';
|
||||
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->addDirRecursive(__DIR__);
|
||||
$zipFile->saveAsFile($fileActual);
|
||||
$zipFile->addDirRecursive(__DIR__.'/../../src');
|
||||
$sourceCount = $zipFile->count();
|
||||
self::assertTrue($sourceCount > 0);
|
||||
$zipFile
|
||||
->saveAsFile($fileActual)
|
||||
->close();
|
||||
self::assertCorrectZipArchive($fileActual);
|
||||
$zipFile->close();
|
||||
|
||||
$zipFile->openFile($fileActual);
|
||||
$zipFile->saveAsFile($fileExpected);
|
||||
$zipFile
|
||||
->openFile($fileActual)
|
||||
->saveAsFile($fileExpected);
|
||||
self::assertCorrectZipArchive($fileExpected);
|
||||
|
||||
$zipFileExpected = new ZipFile();
|
||||
$zipFileExpected->openFile($fileExpected);
|
||||
|
||||
self::assertEquals($zipFile->count(), $sourceCount);
|
||||
self::assertEquals($zipFileExpected->count(), $zipFile->count());
|
||||
self::assertEquals($zipFileExpected->getListFiles(), $zipFile->getListFiles());
|
||||
|
||||
@@ -242,13 +275,13 @@ class ZipFileTest extends ZipTestCase
|
||||
* @see ZipOutputFile::addFromString()
|
||||
* @see ZipOutputFile::addFromFile()
|
||||
* @see ZipOutputFile::addFromStream()
|
||||
* @see ZipFile::getEntryContent()
|
||||
* @see ZipFile::getEntryContents()
|
||||
*/
|
||||
public function testCreateArchiveAndAddFiles()
|
||||
{
|
||||
$outputFromString = file_get_contents(__FILE__);
|
||||
$outputFromString2 = file_get_contents(dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'README.md');
|
||||
$outputFromFile = file_get_contents(dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'bootstrap.xml');
|
||||
$outputFromFile = file_get_contents(dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'phpunit.xml');
|
||||
$outputFromStream = file_get_contents(dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'composer.json');
|
||||
|
||||
$filenameFromString = basename(__FILE__);
|
||||
@@ -266,15 +299,18 @@ class ZipFileTest extends ZipTestCase
|
||||
fwrite($tempStream, $outputFromStream);
|
||||
|
||||
$zipFile = new ZipFile;
|
||||
$zipFile->addFromString($filenameFromString, $outputFromString);
|
||||
$zipFile->addFile($tempFile, $filenameFromFile);
|
||||
$zipFile->addFromStream($tempStream, $filenameFromStream);
|
||||
$zipFile->addEmptyDir($emptyDirName);
|
||||
$zipFile
|
||||
->addFromString($filenameFromString, $outputFromString)
|
||||
->addFile($tempFile, $filenameFromFile)
|
||||
->addFromStream($tempStream, $filenameFromStream)
|
||||
->addEmptyDir($emptyDirName);
|
||||
$zipFile[$filenameFromString2] = $outputFromString2;
|
||||
$zipFile[$emptyDirName2] = null;
|
||||
$zipFile[$emptyDirName3] = 'this content ignoring';
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
self::assertEquals(count($zipFile), 7);
|
||||
$zipFile
|
||||
->saveAsFile($this->outputFilename)
|
||||
->close();
|
||||
unlink($tempFile);
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename);
|
||||
@@ -304,6 +340,18 @@ class ZipFileTest extends ZipTestCase
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
public function testEmptyContent()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile['file'] = '';
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
self::assertEquals($zipFile['file'], '');
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test compression method from image file.
|
||||
*/
|
||||
@@ -330,7 +378,7 @@ class ZipFileTest extends ZipTestCase
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
$info = $zipFile->getEntryInfo($basename);
|
||||
self::assertEquals($info->getMethod(), 'No compression');
|
||||
self::assertEquals($info->getMethodName(), 'No compression');
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
@@ -343,7 +391,7 @@ class ZipFileTest extends ZipTestCase
|
||||
$newName = 'tests/' . $oldName;
|
||||
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->addDirRecursive(__DIR__);
|
||||
$zipFile->addDir(__DIR__);
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
@@ -405,7 +453,6 @@ class ZipFileTest extends ZipTestCase
|
||||
|
||||
/**
|
||||
* @expectedException \PhpZip\Exception\ZipNotFoundEntry
|
||||
* @expectedExceptionMessage Not found entry
|
||||
*/
|
||||
public function testRenameEntryNotFound()
|
||||
{
|
||||
@@ -465,7 +512,6 @@ class ZipFileTest extends ZipTestCase
|
||||
|
||||
/**
|
||||
* @expectedException \PhpZip\Exception\ZipNotFoundEntry
|
||||
* @expectedExceptionMessage Not found entry entry
|
||||
*/
|
||||
public function testDeleteFromNameNotFoundEntry()
|
||||
{
|
||||
@@ -481,22 +527,32 @@ class ZipFileTest extends ZipTestCase
|
||||
$inputDir = dirname(dirname(__DIR__));
|
||||
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->addFilesFromGlobRecursive($inputDir, '**.{php,xml,json}', '/');
|
||||
$zipFile->addFilesFromGlobRecursive($inputDir, '**.{xml,json,md}', '/');
|
||||
self::assertTrue(isset($zipFile['composer.json']));
|
||||
self::assertTrue(isset($zipFile['phpunit.xml']));
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename);
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
self::assertTrue(isset($zipFile['composer.json']));
|
||||
self::assertTrue(isset($zipFile['phpunit.xml']));
|
||||
$zipFile->deleteFromGlob('**.{xml,json}');
|
||||
self::assertFalse(isset($zipFile['composer.json']));
|
||||
self::assertFalse(isset($zipFile['phpunit.xml']));
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename);
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
self::assertFalse(isset($zipFile['composer.json']));
|
||||
self::assertFalse(isset($zipFile['bootstrap.xml']));
|
||||
self::assertTrue($zipFile->count() > 0);
|
||||
|
||||
foreach ($zipFile->getListFiles() as $name) {
|
||||
self::assertStringEndsWith('.md', $name);
|
||||
}
|
||||
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
@@ -528,7 +584,7 @@ class ZipFileTest extends ZipTestCase
|
||||
$inputDir = dirname(dirname(__DIR__));
|
||||
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->addFilesFromRegexRecursive($inputDir, '~\.(xml|php|json)$~i', 'Path');
|
||||
$zipFile->addFilesFromRegexRecursive($inputDir, '~\.(xml|json)$~i', 'Path');
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
@@ -546,7 +602,7 @@ class ZipFileTest extends ZipTestCase
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
self::assertFalse(isset($zipFile['Path/composer.json']));
|
||||
self::assertFalse(isset($zipFile['Path/test.txt']));
|
||||
self::assertTrue(isset($zipFile['Path/bootstrap.xml']));
|
||||
self::assertTrue(isset($zipFile['Path/phpunit.xml']));
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
@@ -576,7 +632,8 @@ class ZipFileTest extends ZipTestCase
|
||||
public function testDeleteAll()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->addDirRecursive(__DIR__);
|
||||
$zipFile->addDirRecursive(dirname(dirname(__DIR__)) .DIRECTORY_SEPARATOR. 'src');
|
||||
self::assertTrue($zipFile->count() > 0);
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
@@ -732,8 +789,7 @@ class ZipFileTest extends ZipTestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \PhpZip\Exception\ZipException
|
||||
* @expectedExceptionMessage Not found entry
|
||||
* @expectedException \PhpZip\Exception\ZipNotFoundEntry
|
||||
*/
|
||||
public function testSetEntryCommentNotFoundEntry()
|
||||
{
|
||||
@@ -749,19 +805,19 @@ class ZipFileTest extends ZipTestCase
|
||||
$entries = [
|
||||
'1' => [
|
||||
'data' => CryptoUtil::randomBytes(255),
|
||||
'method' => ZipFile::METHOD_STORED,
|
||||
'method' => ZipFileInterface::METHOD_STORED,
|
||||
'expected' => 'No compression',
|
||||
],
|
||||
'2' => [
|
||||
'data' => CryptoUtil::randomBytes(255),
|
||||
'method' => ZipFile::METHOD_DEFLATED,
|
||||
'method' => ZipFileInterface::METHOD_DEFLATED,
|
||||
'expected' => 'Deflate',
|
||||
],
|
||||
];
|
||||
if (extension_loaded("bz2")) {
|
||||
$entries['3'] = [
|
||||
'data' => CryptoUtil::randomBytes(255),
|
||||
'method' => ZipFile::METHOD_BZIP2,
|
||||
'method' => ZipFileInterface::METHOD_BZIP2,
|
||||
'expected' => 'Bzip2',
|
||||
];
|
||||
}
|
||||
@@ -776,12 +832,12 @@ class ZipFileTest extends ZipTestCase
|
||||
self::assertCorrectZipArchive($this->outputFilename);
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
$zipFile->setCompressionLevel(ZipFile::LEVEL_BEST_COMPRESSION);
|
||||
$zipFile->setCompressionLevel(ZipFileInterface::LEVEL_BEST_COMPRESSION);
|
||||
$zipAllInfo = $zipFile->getAllInfo();
|
||||
|
||||
foreach ($zipAllInfo as $entryName => $info) {
|
||||
self::assertEquals($zipFile[$entryName], $entries[$entryName]['data']);
|
||||
self::assertEquals($info->getMethod(), $entries[$entryName]['expected']);
|
||||
self::assertEquals($info->getMethodName(), $entries[$entryName]['expected']);
|
||||
$entryInfo = $zipFile->getEntryInfo($entryName);
|
||||
self::assertEquals($entryInfo, $info);
|
||||
}
|
||||
@@ -959,105 +1015,6 @@ class ZipFileTest extends ZipTestCase
|
||||
$zipFile->extractTo($this->outputDirname);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test archive password.
|
||||
*/
|
||||
public function testSetPassword()
|
||||
{
|
||||
$password = base64_encode(CryptoUtil::randomBytes(100));
|
||||
$badPassword = "sdgt43r23wefe";
|
||||
|
||||
// create encryption password with ZipCrypto
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->addDirRecursive(__DIR__);
|
||||
$zipFile->withNewPassword($password, ZipFile::ENCRYPTION_METHOD_TRADITIONAL);
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename, $password);
|
||||
|
||||
// check bad password for ZipCrypto
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
$zipFile->withReadPassword($badPassword);
|
||||
foreach ($zipFile->getListFiles() as $entryName) {
|
||||
try {
|
||||
$zipFile[$entryName];
|
||||
self::fail("Expected Exception has not been raised.");
|
||||
} catch (ZipAuthenticationException $ae) {
|
||||
self::assertNotNull($ae);
|
||||
}
|
||||
}
|
||||
|
||||
// check correct password for ZipCrypto
|
||||
$zipFile->withReadPassword($password);
|
||||
foreach ($zipFile->getAllInfo() as $info) {
|
||||
self::assertTrue($info->isEncrypted());
|
||||
self::assertContains('ZipCrypto', $info->getMethod());
|
||||
$decryptContent = $zipFile[$info->getPath()];
|
||||
self::assertNotEmpty($decryptContent);
|
||||
self::assertContains('<?php', $decryptContent);
|
||||
}
|
||||
|
||||
// change encryption method to WinZip Aes and update file
|
||||
$zipFile->withNewPassword($password, ZipFile::ENCRYPTION_METHOD_WINZIP_AES);
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename, $password);
|
||||
|
||||
// check from WinZip AES encryption
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
// set bad password WinZip AES
|
||||
$zipFile->withReadPassword($badPassword);
|
||||
foreach ($zipFile->getListFiles() as $entryName) {
|
||||
try {
|
||||
$zipFile[$entryName];
|
||||
self::fail("Expected Exception has not been raised.");
|
||||
} catch (ZipAuthenticationException $ae) {
|
||||
self::assertNotNull($ae);
|
||||
}
|
||||
}
|
||||
|
||||
// set correct password WinZip AES
|
||||
$zipFile->withReadPassword($password);
|
||||
foreach ($zipFile->getAllInfo() as $info) {
|
||||
self::assertTrue($info->isEncrypted());
|
||||
self::assertContains('WinZip', $info->getMethod());
|
||||
$decryptContent = $zipFile[$info->getPath()];
|
||||
self::assertNotEmpty($decryptContent);
|
||||
self::assertContains('<?php', $decryptContent);
|
||||
}
|
||||
|
||||
// clear password
|
||||
$zipFile->addFromString('file1', '');
|
||||
$zipFile->withoutPassword();
|
||||
$zipFile->addFromString('file2', '');
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename);
|
||||
|
||||
// check remove password
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
foreach ($zipFile->getAllInfo() as $info) {
|
||||
self::assertFalse($info->isEncrypted());
|
||||
}
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \PhpZip\Exception\ZipException
|
||||
* @expectedExceptionMessage Invalid encryption method
|
||||
*/
|
||||
public function testSetEncryptionMethodInvalid()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
$encryptionMethod = 9999;
|
||||
$zipFile->withNewPassword('pass', $encryptionMethod);
|
||||
$zipFile['entry'] = 'content';
|
||||
$zipFile->outputAsString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \PhpZip\Exception\InvalidArgumentException
|
||||
* @expectedExceptionMessage entryName is null
|
||||
@@ -1100,7 +1057,7 @@ class ZipFileTest extends ZipTestCase
|
||||
|
||||
/**
|
||||
* @expectedException \PhpZip\Exception\ZipUnsupportMethod
|
||||
* @expectedExceptionMessage Unsupported method
|
||||
* @expectedExceptionMessage Unsupported compression method
|
||||
*/
|
||||
public function testAddFromStringUnsupportedMethod()
|
||||
{
|
||||
@@ -1141,8 +1098,8 @@ class ZipFileTest extends ZipTestCase
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
$infoStored = $zipFile->getEntryInfo(basename($fileStored));
|
||||
$infoDeflated = $zipFile->getEntryInfo(basename($fileDeflated));
|
||||
self::assertEquals($infoStored->getMethod(), 'No compression');
|
||||
self::assertEquals($infoDeflated->getMethod(), 'Deflate');
|
||||
self::assertEquals($infoStored->getMethodName(), 'No compression');
|
||||
self::assertEquals($infoDeflated->getMethodName(), 'Deflate');
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
@@ -1153,6 +1110,7 @@ class ZipFileTest extends ZipTestCase
|
||||
public function testAddFromStreamInvalidResource()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
/** @noinspection PhpParamsInspection */
|
||||
$zipFile->addFromStream("invalid resource", "name");
|
||||
}
|
||||
|
||||
@@ -1206,8 +1164,8 @@ class ZipFileTest extends ZipTestCase
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
$infoStored = $zipFile->getEntryInfo(basename($fileStored));
|
||||
$infoDeflated = $zipFile->getEntryInfo(basename($fileDeflated));
|
||||
self::assertEquals($infoStored->getMethod(), 'No compression');
|
||||
self::assertEquals($infoDeflated->getMethod(), 'Deflate');
|
||||
self::assertEquals($infoStored->getMethodName(), 'No compression');
|
||||
self::assertEquals($infoDeflated->getMethodName(), 'Deflate');
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
@@ -1521,6 +1479,7 @@ class ZipFileTest extends ZipTestCase
|
||||
public function testSaveAsStreamBadStream()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
/** @noinspection PhpParamsInspection */
|
||||
$zipFile->saveAsStream("bad stream");
|
||||
}
|
||||
|
||||
@@ -1550,13 +1509,13 @@ class ZipFileTest extends ZipTestCase
|
||||
$files['file' . $i . '.txt'] = CryptoUtil::randomBytes(255);
|
||||
}
|
||||
|
||||
$methods = [ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED];
|
||||
$methods = [ZipFileInterface::METHOD_STORED, ZipFileInterface::METHOD_DEFLATED];
|
||||
if (extension_loaded("bz2")) {
|
||||
$methods[] = ZipFile::METHOD_BZIP2;
|
||||
$methods[] = ZipFileInterface::METHOD_BZIP2;
|
||||
}
|
||||
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->setCompressionLevel(ZipFile::LEVEL_BEST_SPEED);
|
||||
$zipFile->setCompressionLevel(ZipFileInterface::LEVEL_BEST_SPEED);
|
||||
foreach ($files as $entryName => $content) {
|
||||
$zipFile->addFromString($entryName, $content, $methods[array_rand($methods)]);
|
||||
}
|
||||
@@ -1627,18 +1586,43 @@ class ZipFileTest extends ZipTestCase
|
||||
public function testArrayAccessAddFile()
|
||||
{
|
||||
$entryName = 'path/to/file.dat';
|
||||
$entryNameStream = 'path/to/' . basename(__FILE__);
|
||||
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile[$entryName] = new \SplFileInfo(__FILE__);
|
||||
$zipFile[$entryNameStream] = fopen(__FILE__, 'r');
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename);
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
self::assertEquals(sizeof($zipFile), 1);
|
||||
self::assertEquals(sizeof($zipFile), 2);
|
||||
self::assertTrue(isset($zipFile[$entryName]));
|
||||
self::assertTrue(isset($zipFile[$entryNameStream]));
|
||||
self::assertEquals($zipFile[$entryName], file_get_contents(__FILE__));
|
||||
self::assertEquals($zipFile[$entryNameStream], file_get_contents(__FILE__));
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
public function testUnknownCompressionMethod()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
|
||||
$zipFile->addFromString('file', 'content', ZipEntry::UNKNOWN);
|
||||
$zipFile->addFromString('file2', base64_encode(CryptoUtil::randomBytes(512)), ZipEntry::UNKNOWN);
|
||||
|
||||
self::assertEquals($zipFile->getEntryInfo('file')->getMethodName(), 'Unknown');
|
||||
self::assertEquals($zipFile->getEntryInfo('file2')->getMethodName(), 'Unknown');
|
||||
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
|
||||
self::assertEquals($zipFile->getEntryInfo('file')->getMethodName(), 'No compression');
|
||||
self::assertEquals($zipFile->getEntryInfo('file2')->getMethodName(), 'Deflate');
|
||||
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
@@ -1700,8 +1684,10 @@ class ZipFileTest extends ZipTestCase
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile['file'] = 'content';
|
||||
$zipFile['file2'] = 'content2';
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
self::assertEquals(count($zipFile), 2);
|
||||
$zipFile
|
||||
->saveAsFile($this->outputFilename)
|
||||
->close();
|
||||
|
||||
$md5file = md5_file($this->outputFilename);
|
||||
|
||||
@@ -1710,7 +1696,7 @@ class ZipFileTest extends ZipTestCase
|
||||
self::assertTrue(isset($zipFile['file']));
|
||||
self::assertTrue(isset($zipFile['file2']));
|
||||
$zipFile['file3'] = 'content3';
|
||||
self::assertEquals(count($zipFile), 2);
|
||||
self::assertEquals(count($zipFile), 3);
|
||||
$zipFile = $zipFile->rewrite();
|
||||
self::assertEquals(count($zipFile), 3);
|
||||
self::assertTrue(isset($zipFile['file']));
|
||||
@@ -1758,14 +1744,14 @@ class ZipFileTest extends ZipTestCase
|
||||
/**
|
||||
* Test zip alignment.
|
||||
*/
|
||||
public function testZipAlign()
|
||||
public function testZipAlignSourceZip()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
for ($i = 0; $i < 100; $i++) {
|
||||
$zipFile->addFromString(
|
||||
'entry' . $i . '.txt',
|
||||
CryptoUtil::randomBytes(mt_rand(100, 4096)),
|
||||
ZipFile::METHOD_STORED
|
||||
ZipFileInterface::METHOD_STORED
|
||||
);
|
||||
}
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
@@ -1774,7 +1760,9 @@ class ZipFileTest extends ZipTestCase
|
||||
self::assertCorrectZipArchive($this->outputFilename);
|
||||
|
||||
$result = self::doZipAlignVerify($this->outputFilename);
|
||||
if ($result === null) return; // zip align not installed
|
||||
if ($result === null) {
|
||||
return;
|
||||
} // zip align not installed
|
||||
|
||||
// check not zip align
|
||||
self::assertFalse($result);
|
||||
@@ -1791,13 +1779,16 @@ class ZipFileTest extends ZipTestCase
|
||||
|
||||
// check zip align
|
||||
self::assertTrue($result);
|
||||
}
|
||||
|
||||
public function testZipAlignNewFiles()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
for ($i = 0; $i < 100; $i++) {
|
||||
$zipFile->addFromString(
|
||||
'entry' . $i . '.txt',
|
||||
CryptoUtil::randomBytes(mt_rand(100, 4096)),
|
||||
ZipFile::METHOD_STORED
|
||||
ZipFileInterface::METHOD_STORED
|
||||
);
|
||||
}
|
||||
$zipFile->setZipAlign(4);
|
||||
@@ -1807,10 +1798,306 @@ class ZipFileTest extends ZipTestCase
|
||||
self::assertCorrectZipArchive($this->outputFilename);
|
||||
|
||||
$result = self::doZipAlignVerify($this->outputFilename);
|
||||
if ($result === null) {
|
||||
return;
|
||||
} // zip align not installed
|
||||
// check not zip align
|
||||
self::assertTrue($result);
|
||||
}
|
||||
|
||||
public function testZipAlignFromModifiedZipArchive()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
for ($i = 0; $i < 100; $i++) {
|
||||
$zipFile->addFromString(
|
||||
'entry' . $i . '.txt',
|
||||
CryptoUtil::randomBytes(mt_rand(100, 4096)),
|
||||
ZipFileInterface::METHOD_STORED
|
||||
);
|
||||
}
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename);
|
||||
|
||||
$result = self::doZipAlignVerify($this->outputFilename);
|
||||
if ($result === null) {
|
||||
return;
|
||||
} // zip align not installed
|
||||
|
||||
// check not zip align
|
||||
self::assertFalse($result);
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
$zipFile->deleteFromRegex("~entry2[\d]+\.txt$~s");
|
||||
for ($i = 0; $i < 100; $i++) {
|
||||
$isStored = (bool)mt_rand(0, 1);
|
||||
|
||||
$zipFile->addFromString(
|
||||
'entry_new_' . ($isStored ? 'stored' : 'deflated') . '_' . $i . '.txt',
|
||||
CryptoUtil::randomBytes(mt_rand(100, 4096)),
|
||||
$isStored ?
|
||||
ZipFileInterface::METHOD_STORED :
|
||||
ZipFileInterface::METHOD_DEFLATED
|
||||
);
|
||||
}
|
||||
$zipFile->setZipAlign(4);
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename);
|
||||
|
||||
$result = self::doZipAlignVerify($this->outputFilename, true);
|
||||
self::assertNotNull($result);
|
||||
|
||||
// check zip align
|
||||
self::assertTrue($result);
|
||||
}
|
||||
|
||||
public function testFilename0()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile[0] = 0;
|
||||
self::assertTrue(isset($zipFile[0]));
|
||||
self::assertTrue(isset($zipFile['0']));
|
||||
self::assertCount(1, $zipFile);
|
||||
$zipFile
|
||||
->saveAsFile($this->outputFilename)
|
||||
->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename);
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
self::assertTrue(isset($zipFile[0]));
|
||||
self::assertTrue(isset($zipFile['0']));
|
||||
self::assertEquals($zipFile['0'], '0');
|
||||
self::assertCount(1, $zipFile);
|
||||
$zipFile->close();
|
||||
|
||||
self::assertTrue(unlink($this->outputFilename));
|
||||
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->addFromString(0, 0);
|
||||
self::assertTrue(isset($zipFile[0]));
|
||||
self::assertTrue(isset($zipFile['0']));
|
||||
self::assertCount(1, $zipFile);
|
||||
$zipFile
|
||||
->saveAsFile($this->outputFilename)
|
||||
->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename);
|
||||
}
|
||||
|
||||
public function testPsrResponse()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
for ($i = 0; $i < 10; $i++) {
|
||||
$zipFile[$i] = $i;
|
||||
}
|
||||
$filename = 'file.jar';
|
||||
$response = $this->getMock(ResponseInterface::class);
|
||||
$response = $zipFile->outputAsResponse($response, $filename);
|
||||
$this->assertInstanceOf(ResponseInterface::class, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \PhpZip\Exception\InvalidArgumentException
|
||||
* @expectedExceptionMessage Output filename is empty.
|
||||
*/
|
||||
public function testInvalidPsrResponse()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile['file'] = 'content';
|
||||
$response = $this->getMock(ResponseInterface::class);
|
||||
$zipFile->outputAsResponse($response, '');
|
||||
}
|
||||
|
||||
public function testCompressionLevel()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile
|
||||
->addFromString('file', 'content', ZipFileInterface::METHOD_DEFLATED)
|
||||
->setCompressionLevelEntry('file', ZipFileInterface::LEVEL_BEST_COMPRESSION)
|
||||
->addFromString('file2', 'content', ZipFileInterface::METHOD_DEFLATED)
|
||||
->setCompressionLevelEntry('file2', ZipFileInterface::LEVEL_FAST)
|
||||
->addFromString('file3', 'content', ZipFileInterface::METHOD_DEFLATED)
|
||||
->setCompressionLevelEntry('file3', ZipFileInterface::LEVEL_SUPER_FAST)
|
||||
->addFromString('file4', 'content', ZipFileInterface::METHOD_DEFLATED)
|
||||
->setCompressionLevelEntry('file4', ZipFileInterface::LEVEL_DEFAULT_COMPRESSION)
|
||||
->saveAsFile($this->outputFilename)
|
||||
->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename);
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
self::assertEquals($zipFile->getEntryInfo('file')
|
||||
->getCompressionLevel(), ZipFileInterface::LEVEL_BEST_COMPRESSION);
|
||||
self::assertEquals($zipFile->getEntryInfo('file2')
|
||||
->getCompressionLevel(), ZipFileInterface::LEVEL_FAST);
|
||||
self::assertEquals($zipFile->getEntryInfo('file3')
|
||||
->getCompressionLevel(), ZipFileInterface::LEVEL_SUPER_FAST);
|
||||
self::assertEquals($zipFile->getEntryInfo('file4')
|
||||
->getCompressionLevel(), ZipFileInterface::LEVEL_DEFAULT_COMPRESSION);
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \PhpZip\Exception\InvalidArgumentException
|
||||
* @expectedExceptionMessage Invalid compression level
|
||||
*/
|
||||
public function testInvalidCompressionLevel()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->addFromString('file', 'content');
|
||||
$zipFile->setCompressionLevel(15);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \PhpZip\Exception\InvalidArgumentException
|
||||
* @expectedExceptionMessage Invalid compression level
|
||||
*/
|
||||
public function testInvalidCompressionLevelEntry()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->addFromString('file', 'content');
|
||||
$zipFile->setCompressionLevelEntry('file', 15);
|
||||
}
|
||||
|
||||
public function testCompressionGlobal()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
for ($i = 0; $i < 10; $i++) {
|
||||
$zipFile->addFromString('file' . $i, 'content', ZipFileInterface::METHOD_DEFLATED);
|
||||
}
|
||||
$zipFile
|
||||
->setCompressionLevel(ZipFileInterface::LEVEL_BEST_SPEED)
|
||||
->saveAsFile($this->outputFilename)
|
||||
->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename);
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
$infoList = $zipFile->getAllInfo();
|
||||
array_walk($infoList, function (ZipInfo $zipInfo) {
|
||||
self::assertEquals($zipInfo->getCompressionLevel(), ZipFileInterface::LEVEL_BEST_SPEED);
|
||||
});
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
public function testCompressionMethodEntry()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->addFromString('file', 'content', ZipFileInterface::METHOD_STORED);
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
self::assertEquals($zipFile->getEntryInfo('file')->getMethodName(), 'No compression');
|
||||
$zipFile->setCompressionMethodEntry('file', ZipFileInterface::METHOD_DEFLATED);
|
||||
self::assertEquals($zipFile->getEntryInfo('file')->getMethodName(), 'Deflate');
|
||||
|
||||
$zipFile->rewrite();
|
||||
self::assertEquals($zipFile->getEntryInfo('file')->getMethodName(), 'Deflate');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \PhpZip\Exception\ZipUnsupportMethod
|
||||
* @expectedExceptionMessage Unsupported method
|
||||
*/
|
||||
public function testInvalidCompressionMethodEntry()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->addFromString('file', 'content', ZipFileInterface::METHOD_STORED);
|
||||
$zipFile->setCompressionMethodEntry('file', 99);
|
||||
}
|
||||
|
||||
public function testUnchangeAll()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
for ($i = 0; $i < 10; $i++) {
|
||||
$zipFile[$i] = $i;
|
||||
}
|
||||
$zipFile->setArchiveComment('comment');
|
||||
self::assertCount(10, $zipFile);
|
||||
self::assertEquals($zipFile->getArchiveComment(), 'comment');
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
|
||||
$zipFile->unchangeAll();
|
||||
self::assertCount(0, $zipFile);
|
||||
self::assertEquals($zipFile->getArchiveComment(), null);
|
||||
$zipFile->close();
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
self::assertCount(10, $zipFile);
|
||||
self::assertEquals($zipFile->getArchiveComment(), 'comment');
|
||||
|
||||
for ($i = 10; $i < 100; $i++) {
|
||||
$zipFile[$i] = $i;
|
||||
}
|
||||
$zipFile->setArchiveComment('comment 2');
|
||||
self::assertCount(100, $zipFile);
|
||||
self::assertEquals($zipFile->getArchiveComment(), 'comment 2');
|
||||
|
||||
$zipFile->unchangeAll();
|
||||
self::assertCount(10, $zipFile);
|
||||
self::assertEquals($zipFile->getArchiveComment(), 'comment');
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
public function testUnchangeArchiveComment()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
for ($i = 0; $i < 10; $i++) {
|
||||
$zipFile[$i] = $i;
|
||||
}
|
||||
$zipFile->setArchiveComment('comment');
|
||||
self::assertEquals($zipFile->getArchiveComment(), 'comment');
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
|
||||
$zipFile->unchangeArchiveComment();
|
||||
self::assertEquals($zipFile->getArchiveComment(), null);
|
||||
$zipFile->close();
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
self::assertEquals($zipFile->getArchiveComment(), 'comment');
|
||||
$zipFile->setArchiveComment('comment 2');
|
||||
self::assertEquals($zipFile->getArchiveComment(), 'comment 2');
|
||||
|
||||
$zipFile->unchangeArchiveComment();
|
||||
self::assertEquals($zipFile->getArchiveComment(), 'comment');
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
public function testUnchangeEntry()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile['file 1'] = 'content 1';
|
||||
$zipFile['file 2'] = 'content 2';
|
||||
$zipFile
|
||||
->saveAsFile($this->outputFilename)
|
||||
->close();
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
|
||||
$zipFile['file 1'] = 'modify content 1';
|
||||
$zipFile->setPasswordEntry('file 1', 'password');
|
||||
|
||||
self::assertEquals($zipFile['file 1'], 'modify content 1');
|
||||
self::assertTrue($zipFile->getEntryInfo('file 1')->isEncrypted());
|
||||
|
||||
self::assertEquals($zipFile['file 2'], 'content 2');
|
||||
self::assertFalse($zipFile->getEntryInfo('file 2')->isEncrypted());
|
||||
|
||||
$zipFile->unchangeEntry('file 1');
|
||||
|
||||
self::assertEquals($zipFile['file 1'], 'content 1');
|
||||
self::assertFalse($zipFile->getEntryInfo('file 1')->isEncrypted());
|
||||
|
||||
self::assertEquals($zipFile['file 2'], 'content 2');
|
||||
self::assertFalse($zipFile->getEntryInfo('file 2')->isEncrypted());
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test support ZIP64 ext (slow test - normal).
|
||||
* Create > 65535 files in archive and open and extract to /dev/null.
|
||||
@@ -1830,10 +2117,12 @@ class ZipFileTest extends ZipTestCase
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
self::assertEquals($zipFile->count(), $countFiles);
|
||||
$i = 0;
|
||||
foreach ($zipFile as $entry => $content) {
|
||||
|
||||
self::assertEquals($entry, $i . '.txt');
|
||||
self::assertEquals($content, $i);
|
||||
$i++;
|
||||
}
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
111
tests/PhpZip/ZipMatcherTest.php
Normal file
111
tests/PhpZip/ZipMatcherTest.php
Normal file
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
namespace PhpZip;
|
||||
|
||||
use PhpZip\Model\ZipEntryMatcher;
|
||||
use PhpZip\Model\ZipInfo;
|
||||
use PhpZip\Util\CryptoUtil;
|
||||
|
||||
class ZipMatcherTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testMatcher()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
for ($i = 0; $i < 100; $i++) {
|
||||
$zipFile[$i] = $i;
|
||||
}
|
||||
|
||||
$matcher = $zipFile->matcher();
|
||||
self::assertInstanceOf(ZipEntryMatcher::class, $matcher);
|
||||
|
||||
$this->assertTrue(is_array($matcher->getMatches()));
|
||||
$this->assertCount(0, $matcher);
|
||||
|
||||
$matcher->add(1)->add(10)->add(20);
|
||||
$this->assertCount(3, $matcher);
|
||||
$this->assertEquals($matcher->getMatches(), ['1', '10', '20']);
|
||||
|
||||
$matcher->delete();
|
||||
$this->assertCount(97, $zipFile);
|
||||
$this->assertCount(0, $matcher);
|
||||
|
||||
$matcher->match('~^[2][1-5]|[3][6-9]|40$~s');
|
||||
$this->assertCount(10, $matcher);
|
||||
$actualMatches = [
|
||||
'21', '22', '23', '24', '25',
|
||||
'36', '37', '38', '39',
|
||||
'40'
|
||||
];
|
||||
$this->assertEquals($matcher->getMatches(), $actualMatches);
|
||||
$matcher->setPassword('qwerty');
|
||||
$info = $zipFile->getAllInfo();
|
||||
array_walk($info, function (ZipInfo $zipInfo) use ($actualMatches) {
|
||||
self::assertEquals($zipInfo->isEncrypted(), in_array($zipInfo->getName(), $actualMatches));
|
||||
});
|
||||
|
||||
$matcher->all();
|
||||
$this->assertCount(count($zipFile), $matcher);
|
||||
|
||||
$expectedNames = [];
|
||||
$matcher->invoke(function ($entryName) use (&$expectedNames) {
|
||||
$expectedNames[] = $entryName;
|
||||
});
|
||||
$this->assertEquals($expectedNames, $matcher->getMatches());
|
||||
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
public function testDocsExample()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
for ($i = 0; $i < 100; $i++) {
|
||||
$zipFile['file_'.$i.'.jpg'] = CryptoUtil::randomBytes(100);
|
||||
}
|
||||
|
||||
$renameEntriesArray = [
|
||||
'file_10.jpg',
|
||||
'file_11.jpg',
|
||||
'file_12.jpg',
|
||||
'file_13.jpg',
|
||||
'file_14.jpg',
|
||||
'file_15.jpg',
|
||||
'file_16.jpg',
|
||||
'file_17.jpg',
|
||||
'file_18.jpg',
|
||||
'file_19.jpg',
|
||||
'file_50.jpg',
|
||||
'file_51.jpg',
|
||||
'file_52.jpg',
|
||||
'file_53.jpg',
|
||||
'file_54.jpg',
|
||||
'file_55.jpg',
|
||||
'file_56.jpg',
|
||||
'file_57.jpg',
|
||||
'file_58.jpg',
|
||||
'file_59.jpg',
|
||||
];
|
||||
|
||||
foreach ($renameEntriesArray as $name) {
|
||||
self::assertTrue(isset($zipFile[$name]));
|
||||
}
|
||||
|
||||
$matcher = $zipFile->matcher();
|
||||
$matcher->match('~^file_(1|5)\d+~');
|
||||
self::assertEquals($matcher->getMatches(), $renameEntriesArray);
|
||||
|
||||
$matcher->invoke(function ($entryName) use ($zipFile) {
|
||||
$newName = preg_replace('~\.(jpe?g)$~i', '.no_optimize.$1', $entryName);
|
||||
$zipFile->rename($entryName, $newName);
|
||||
});
|
||||
|
||||
foreach ($renameEntriesArray as $name) {
|
||||
self::assertFalse(isset($zipFile[$name]));
|
||||
|
||||
$pathInfo = pathinfo($name);
|
||||
$newName = $pathInfo['filename'].'.no_optimize.'.$pathInfo['extension'];
|
||||
self::assertTrue(isset($zipFile[$newName]));
|
||||
}
|
||||
|
||||
$zipFile->close();
|
||||
}
|
||||
}
|
330
tests/PhpZip/ZipPasswordTest.php
Normal file
330
tests/PhpZip/ZipPasswordTest.php
Normal file
@@ -0,0 +1,330 @@
|
||||
<?php
|
||||
|
||||
namespace PhpZip;
|
||||
|
||||
use PhpZip\Exception\ZipAuthenticationException;
|
||||
use PhpZip\Model\ZipInfo;
|
||||
use PhpZip\Util\CryptoUtil;
|
||||
|
||||
class ZipPasswordTest extends ZipFileAddDirTest
|
||||
{
|
||||
/**
|
||||
* Test archive password.
|
||||
*/
|
||||
public function testSetPassword()
|
||||
{
|
||||
$password = base64_encode(CryptoUtil::randomBytes(100));
|
||||
$badPassword = "sdgt43r23wefe";
|
||||
|
||||
// create encryption password with ZipCrypto
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->addDir(__DIR__);
|
||||
$zipFile->setPassword($password, ZipFileInterface::ENCRYPTION_METHOD_TRADITIONAL);
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename, $password);
|
||||
|
||||
// check bad password for ZipCrypto
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
$zipFile->setReadPassword($badPassword);
|
||||
foreach ($zipFile->getListFiles() as $entryName) {
|
||||
try {
|
||||
$zipFile[$entryName];
|
||||
self::fail("Expected Exception has not been raised.");
|
||||
} catch (ZipAuthenticationException $ae) {
|
||||
self::assertNotNull($ae);
|
||||
}
|
||||
}
|
||||
|
||||
// check correct password for ZipCrypto
|
||||
$zipFile->setReadPassword($password);
|
||||
foreach ($zipFile->getAllInfo() as $info) {
|
||||
self::assertTrue($info->isEncrypted());
|
||||
self::assertContains('ZipCrypto', $info->getMethodName());
|
||||
$decryptContent = $zipFile[$info->getName()];
|
||||
self::assertNotEmpty($decryptContent);
|
||||
self::assertContains('<?php', $decryptContent);
|
||||
}
|
||||
|
||||
// change encryption method to WinZip Aes and update file
|
||||
$zipFile->setPassword($password, ZipFileInterface::ENCRYPTION_METHOD_WINZIP_AES);
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename, $password);
|
||||
|
||||
// check from WinZip AES encryption
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
// set bad password WinZip AES
|
||||
$zipFile->setReadPassword($badPassword);
|
||||
foreach ($zipFile->getListFiles() as $entryName) {
|
||||
try {
|
||||
$zipFile[$entryName];
|
||||
self::fail("Expected Exception has not been raised.");
|
||||
} catch (ZipAuthenticationException $ae) {
|
||||
self::assertNotNull($ae);
|
||||
}
|
||||
}
|
||||
|
||||
// set correct password WinZip AES
|
||||
$zipFile->setReadPassword($password);
|
||||
foreach ($zipFile->getAllInfo() as $info) {
|
||||
self::assertTrue($info->isEncrypted());
|
||||
self::assertContains('WinZip', $info->getMethodName());
|
||||
$decryptContent = $zipFile[$info->getName()];
|
||||
self::assertNotEmpty($decryptContent);
|
||||
self::assertContains('<?php', $decryptContent);
|
||||
}
|
||||
|
||||
// clear password
|
||||
$zipFile->addFromString('file1', '');
|
||||
$zipFile->removePassword();
|
||||
$zipFile->addFromString('file2', '');
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename);
|
||||
|
||||
// check remove password
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
foreach ($zipFile->getAllInfo() as $info) {
|
||||
self::assertFalse($info->isEncrypted());
|
||||
}
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
public function testTraditionalEncryption()
|
||||
{
|
||||
$password = base64_encode(CryptoUtil::randomBytes(50));
|
||||
|
||||
$zip = new ZipFile();
|
||||
$zip->addDirRecursive($this->outputDirname);
|
||||
$zip->setPassword($password, ZipFileInterface::ENCRYPTION_METHOD_TRADITIONAL);
|
||||
$zip->saveAsFile($this->outputFilename);
|
||||
$zip->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename, $password);
|
||||
|
||||
$zip->openFile($this->outputFilename);
|
||||
$zip->setReadPassword($password);
|
||||
self::assertFilesResult($zip, array_keys(self::$files));
|
||||
foreach ($zip->getAllInfo() as $info) {
|
||||
if (!$info->isFolder()) {
|
||||
self::assertTrue($info->isEncrypted());
|
||||
self::assertContains('ZipCrypto', $info->getMethodName());
|
||||
}
|
||||
}
|
||||
$zip->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider winZipKeyStrengthProvider
|
||||
* @param int $encryptionMethod
|
||||
* @param int $bitSize
|
||||
*/
|
||||
public function testWinZipAesEncryption($encryptionMethod, $bitSize)
|
||||
{
|
||||
$password = base64_encode(CryptoUtil::randomBytes(50));
|
||||
|
||||
$zip = new ZipFile();
|
||||
$zip->addDirRecursive($this->outputDirname);
|
||||
$zip->setPassword($password, $encryptionMethod);
|
||||
$zip->saveAsFile($this->outputFilename);
|
||||
$zip->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename, $password);
|
||||
|
||||
$zip->openFile($this->outputFilename);
|
||||
$zip->setReadPassword($password);
|
||||
self::assertFilesResult($zip, array_keys(self::$files));
|
||||
foreach ($zip->getAllInfo() as $info) {
|
||||
if (!$info->isFolder()) {
|
||||
self::assertTrue($info->isEncrypted());
|
||||
self::assertEquals($info->getEncryptionMethod(), $encryptionMethod);
|
||||
self::assertContains('WinZip AES-' . $bitSize, $info->getMethodName());
|
||||
}
|
||||
}
|
||||
$zip->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function winZipKeyStrengthProvider()
|
||||
{
|
||||
return [
|
||||
[ZipFileInterface::ENCRYPTION_METHOD_WINZIP_AES_128, 128],
|
||||
[ZipFileInterface::ENCRYPTION_METHOD_WINZIP_AES_192, 192],
|
||||
[ZipFileInterface::ENCRYPTION_METHOD_WINZIP_AES, 256],
|
||||
[ZipFileInterface::ENCRYPTION_METHOD_WINZIP_AES_256, 256],
|
||||
];
|
||||
}
|
||||
|
||||
public function testEncryptionEntries()
|
||||
{
|
||||
$password1 = '353442434235424234';
|
||||
$password2 = 'adgerhvrwjhqqehtqhkbqrgewg';
|
||||
|
||||
$zip = new ZipFile();
|
||||
$zip->addDir($this->outputDirname);
|
||||
$zip->setPasswordEntry('.hidden', $password1, ZipFileInterface::ENCRYPTION_METHOD_TRADITIONAL);
|
||||
$zip->setPasswordEntry('text file.txt', $password2, ZipFileInterface::ENCRYPTION_METHOD_WINZIP_AES);
|
||||
$zip->saveAsFile($this->outputFilename);
|
||||
$zip->close();
|
||||
|
||||
$zip->openFile($this->outputFilename);
|
||||
$zip->setReadPasswordEntry('.hidden', $password1);
|
||||
$zip->setReadPasswordEntry('text file.txt', $password2);
|
||||
self::assertFilesResult($zip, [
|
||||
'.hidden',
|
||||
'text file.txt',
|
||||
'Текстовый документ.txt',
|
||||
'empty dir/',
|
||||
]);
|
||||
|
||||
$info = $zip->getEntryInfo('.hidden');
|
||||
self::assertTrue($info->isEncrypted());
|
||||
self::assertContains('ZipCrypto', $info->getMethodName());
|
||||
|
||||
$info = $zip->getEntryInfo('text file.txt');
|
||||
self::assertTrue($info->isEncrypted());
|
||||
self::assertContains('WinZip AES', $info->getMethodName());
|
||||
|
||||
self::assertFalse($zip->getEntryInfo('Текстовый документ.txt')->isEncrypted());
|
||||
self::assertFalse($zip->getEntryInfo('empty dir/')->isEncrypted());
|
||||
|
||||
$zip->close();
|
||||
}
|
||||
|
||||
public function testEncryptionEntriesWithDefaultPassword()
|
||||
{
|
||||
$password1 = '353442434235424234';
|
||||
$password2 = 'adgerhvrwjhqqehtqhkbqrgewg';
|
||||
$defaultPassword = ' f f f f f ffff f5 ';
|
||||
|
||||
$zip = new ZipFile();
|
||||
$zip->addDir($this->outputDirname);
|
||||
$zip->setPassword($defaultPassword);
|
||||
$zip->setPasswordEntry('.hidden', $password1, ZipFileInterface::ENCRYPTION_METHOD_TRADITIONAL);
|
||||
$zip->setPasswordEntry('text file.txt', $password2, ZipFileInterface::ENCRYPTION_METHOD_WINZIP_AES);
|
||||
$zip->saveAsFile($this->outputFilename);
|
||||
$zip->close();
|
||||
|
||||
$zip->openFile($this->outputFilename);
|
||||
$zip->setReadPassword($defaultPassword);
|
||||
$zip->setReadPasswordEntry('.hidden', $password1);
|
||||
$zip->setReadPasswordEntry('text file.txt', $password2);
|
||||
self::assertFilesResult($zip, [
|
||||
'.hidden',
|
||||
'text file.txt',
|
||||
'Текстовый документ.txt',
|
||||
'empty dir/',
|
||||
]);
|
||||
|
||||
$info = $zip->getEntryInfo('.hidden');
|
||||
self::assertTrue($info->isEncrypted());
|
||||
self::assertContains('ZipCrypto', $info->getMethodName());
|
||||
|
||||
$info = $zip->getEntryInfo('text file.txt');
|
||||
self::assertTrue($info->isEncrypted());
|
||||
self::assertContains('WinZip AES', $info->getMethodName());
|
||||
|
||||
$info = $zip->getEntryInfo('Текстовый документ.txt');
|
||||
self::assertTrue($info->isEncrypted());
|
||||
self::assertContains('WinZip AES', $info->getMethodName());
|
||||
|
||||
self::assertFalse($zip->getEntryInfo('empty dir/')->isEncrypted());
|
||||
|
||||
$zip->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \PhpZip\Exception\ZipException
|
||||
* @expectedExceptionMessage Invalid encryption method
|
||||
*/
|
||||
public function testSetEncryptionMethodInvalid()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
$encryptionMethod = 9999;
|
||||
$zipFile->setPassword('pass', $encryptionMethod);
|
||||
$zipFile['entry'] = 'content';
|
||||
$zipFile->outputAsString();
|
||||
}
|
||||
|
||||
public function testEntryPassword()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->setPassword('pass');
|
||||
$zipFile['file'] = 'content';
|
||||
self::assertFalse($zipFile->getEntryInfo('file')->isEncrypted());
|
||||
for ($i = 1; $i <= 10; $i++) {
|
||||
$zipFile['file' . $i] = 'content';
|
||||
if ($i < 6) {
|
||||
$zipFile->setPasswordEntry('file' . $i, 'pass');
|
||||
self::assertTrue($zipFile->getEntryInfo('file' . $i)->isEncrypted());
|
||||
} else {
|
||||
self::assertFalse($zipFile->getEntryInfo('file' . $i)->isEncrypted());
|
||||
}
|
||||
}
|
||||
$zipFile->removePasswordEntry('file3');
|
||||
self::assertFalse($zipFile->getEntryInfo('file3')->isEncrypted());
|
||||
self::asserttrue($zipFile->getEntryInfo('file2')->isEncrypted());
|
||||
$zipFile->removePassword();
|
||||
$infoList = $zipFile->getAllInfo();
|
||||
array_walk($infoList, function (ZipInfo $zipInfo) {
|
||||
self::assertFalse($zipInfo->isEncrypted());
|
||||
});
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \PhpZip\Exception\ZipException
|
||||
* @expectedExceptionMessage Invalid encryption method
|
||||
*/
|
||||
public function testInvalidEncryptionMethodEntry()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->addFromString('file', 'content', ZipFileInterface::METHOD_STORED);
|
||||
$zipFile->setPasswordEntry('file', 'pass', 99);
|
||||
}
|
||||
|
||||
public function testArchivePasswordUpdateWithoutSetReadPassword()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile['file1'] = 'content';
|
||||
$zipFile['file2'] = 'content';
|
||||
$zipFile['file3'] = 'content';
|
||||
$zipFile->setPassword('password');
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename, 'password');
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
self::assertCount(3, $zipFile);
|
||||
foreach ($zipFile->getAllInfo() as $info) {
|
||||
self::assertTrue($info->isEncrypted());
|
||||
}
|
||||
unset($zipFile['file3']);
|
||||
$zipFile['file4'] = 'content';
|
||||
$zipFile->rewrite();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename, 'password');
|
||||
|
||||
self::assertCount(3, $zipFile);
|
||||
self::assertFalse(isset($zipFile['file3']));
|
||||
self::assertTrue(isset($zipFile['file4']));
|
||||
self::assertTrue($zipFile->getEntryInfo('file1')->isEncrypted());
|
||||
self::assertTrue($zipFile->getEntryInfo('file2')->isEncrypted());
|
||||
self::assertFalse($zipFile->getEntryInfo('file4')->isEncrypted());
|
||||
self::assertEquals($zipFile['file4'], 'content');
|
||||
|
||||
$zipFile->extractTo($this->outputDirname, ['file4']);
|
||||
|
||||
self::assertTrue(file_exists($this->outputDirname . DIRECTORY_SEPARATOR . 'file4'));
|
||||
self::assertEquals(file_get_contents($this->outputDirname . DIRECTORY_SEPARATOR . 'file4'), $zipFile['file4']);
|
||||
|
||||
$zipFile->close();
|
||||
}
|
||||
}
|
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace PhpZip;
|
||||
|
||||
use PhpZip\Model\EndOfCentralDirectory;
|
||||
@@ -27,8 +28,14 @@ class ZipTestCase extends \PHPUnit_Framework_TestCase
|
||||
parent::setUp();
|
||||
|
||||
$id = uniqid('phpzip');
|
||||
$this->outputFilename = sys_get_temp_dir() . '/' . $id . '.zip';
|
||||
$this->outputDirname = sys_get_temp_dir() . '/' . $id;
|
||||
$tempDir = sys_get_temp_dir() . '/phpunit-phpzip';
|
||||
if (!is_dir($tempDir)) {
|
||||
if (!mkdir($tempDir, 0755, true)) {
|
||||
throw new \RuntimeException("Dir " . $tempDir . " can't created");
|
||||
}
|
||||
}
|
||||
$this->outputFilename = $tempDir . '/' . $id . '.zip';
|
||||
$this->outputDirname = $tempDir . '/' . $id;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -118,12 +125,13 @@ class ZipTestCase extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
if (DIRECTORY_SEPARATOR !== '\\' && `which zipalign`) {
|
||||
exec("zipalign -c -v 4 " . escapeshellarg($filename), $output, $returnCode);
|
||||
if ($showErrors && $returnCode !== 0) fwrite(STDERR, implode(PHP_EOL, $output));
|
||||
if ($showErrors && $returnCode !== 0) {
|
||||
fwrite(STDERR, implode(PHP_EOL, $output));
|
||||
}
|
||||
return $returnCode === 0;
|
||||
} else {
|
||||
fwrite(STDERR, 'Can not find program "zipalign" for test');
|
||||
fwrite(STDERR, 'Can not find program "zipalign" for test' . PHP_EOL);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
BIN
tests/PhpZip/php-zip-ext-test-resources/binarynull.zip
Normal file
BIN
tests/PhpZip/php-zip-ext-test-resources/binarynull.zip
Normal file
Binary file not shown.
BIN
tests/PhpZip/php-zip-ext-test-resources/bug40228.zip
Normal file
BIN
tests/PhpZip/php-zip-ext-test-resources/bug40228.zip
Normal file
Binary file not shown.
BIN
tests/PhpZip/php-zip-ext-test-resources/bug40228私はガラスを食べられます.zip
Normal file
BIN
tests/PhpZip/php-zip-ext-test-resources/bug40228私はガラスを食べられます.zip
Normal file
Binary file not shown.
BIN
tests/PhpZip/php-zip-ext-test-resources/bug49072.zip
Normal file
BIN
tests/PhpZip/php-zip-ext-test-resources/bug49072.zip
Normal file
Binary file not shown.
BIN
tests/PhpZip/php-zip-ext-test-resources/bug70752.zip
Normal file
BIN
tests/PhpZip/php-zip-ext-test-resources/bug70752.zip
Normal file
Binary file not shown.
BIN
tests/PhpZip/php-zip-ext-test-resources/bug8009.zip
Normal file
BIN
tests/PhpZip/php-zip-ext-test-resources/bug8009.zip
Normal file
Binary file not shown.
BIN
tests/PhpZip/php-zip-ext-test-resources/pecl12414.zip
Normal file
BIN
tests/PhpZip/php-zip-ext-test-resources/pecl12414.zip
Normal file
Binary file not shown.
Reference in New Issue
Block a user