| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- namespace PhpZip;
- use PhpZip\Exception\ZipException;
- use PhpZip\Util\CryptoUtil;
- /**
- * Test ZipAlign
- */
- class ZipAlignTest extends ZipTestCase
- {
- /**
- * @throws ZipException
- */
- public function testApkAlignedAndSetZipAlignAndReSave()
- {
- $filename = __DIR__ . '/resources/test.apk';
- $this->assertCorrectZipArchive($filename);
- $result = $this->assertVerifyZipAlign($filename);
- if (null !== $result) {
- $this->assertTrue($result);
- }
- $zipFile = new ZipFile();
- $zipFile->openFile($filename);
- $zipFile->setZipAlign(4);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- $this->assertCorrectZipArchive($this->outputFilename);
- $result = $this->assertVerifyZipAlign($this->outputFilename, true);
- if (null !== $result) {
- $this->assertTrue($result);
- }
- }
- /**
- * Test zip alignment.
- * @throws ZipException
- */
- public function testZipAlignSourceZip()
- {
- $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();
- $this->assertCorrectZipArchive($this->outputFilename);
- $result = $this->assertVerifyZipAlign($this->outputFilename);
- if ($result === null) {
- return;
- } // zip align not installed
- // check not zip align
- $this->assertFalse($result);
- $zipFile->openFile($this->outputFilename);
- $zipFile->setZipAlign(4);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- $this->assertCorrectZipArchive($this->outputFilename);
- $result = $this->assertVerifyZipAlign($this->outputFilename, true);
- $this->assertNotNull($result);
- // check zip align
- $this->assertTrue($result);
- }
- /**
- * @throws ZipException
- */
- public function testZipAlignNewFiles()
- {
- $zipFile = new ZipFile();
- for ($i = 0; $i < 100; $i++) {
- $zipFile->addFromString(
- 'entry' . $i . '.txt',
- CryptoUtil::randomBytes(mt_rand(100, 4096)),
- ZipFileInterface::METHOD_STORED
- );
- }
- $zipFile->setZipAlign(4);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- $this->assertCorrectZipArchive($this->outputFilename);
- $result = $this->assertVerifyZipAlign($this->outputFilename);
- if ($result === null) {
- return;
- } // zip align not installed
- // check not zip align
- $this->assertTrue($result);
- }
- /**
- * @throws ZipException
- */
- 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();
- $this->assertCorrectZipArchive($this->outputFilename);
- $result = $this->assertVerifyZipAlign($this->outputFilename);
- if ($result === null) {
- return;
- } // zip align not installed
- // check not zip align
- $this->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();
- $this->assertCorrectZipArchive($this->outputFilename);
- $result = $this->assertVerifyZipAlign($this->outputFilename, true);
- $this->assertNotNull($result);
- // check zip align
- $this->assertTrue($result);
- }
- }
|