ZipAlignTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace PhpZip\Tests;
  3. use PhpZip\Constants\ZipCompressionMethod;
  4. use PhpZip\Exception\ZipException;
  5. use PhpZip\ZipFile;
  6. /**
  7. * Test ZipAlign.
  8. *
  9. * @internal
  10. *
  11. * @small
  12. */
  13. class ZipAlignTest extends ZipTestCase
  14. {
  15. /**
  16. * @throws ZipException
  17. */
  18. public function testApkAlignedAndSetZipAlignAndReSave()
  19. {
  20. $filename = __DIR__ . '/resources/apk.zip';
  21. static::assertCorrectZipArchive($filename);
  22. $result = static::assertVerifyZipAlign($filename);
  23. if ($result !== null) {
  24. static::assertTrue($result);
  25. }
  26. $zipFile = new ZipFile();
  27. $zipFile->openFile($filename);
  28. $zipFile->setZipAlign(4);
  29. $zipFile->saveAsFile($this->outputFilename);
  30. $zipFile->close();
  31. static::assertCorrectZipArchive($this->outputFilename);
  32. $result = static::assertVerifyZipAlign($this->outputFilename, true);
  33. if ($result !== null) {
  34. static::assertTrue($result);
  35. }
  36. }
  37. /**
  38. * Test zip alignment.
  39. *
  40. * @throws ZipException
  41. * @throws \Exception
  42. */
  43. public function testZipAlignSourceZip()
  44. {
  45. $zipFile = new ZipFile();
  46. for ($i = 0; $i < 100; $i++) {
  47. $zipFile->addFromString(
  48. 'entry' . $i . '.txt',
  49. random_bytes(random_int(100, 4096)),
  50. ZipCompressionMethod::STORED
  51. );
  52. }
  53. $zipFile->saveAsFile($this->outputFilename);
  54. $zipFile->close();
  55. static::assertCorrectZipArchive($this->outputFilename);
  56. $result = static::assertVerifyZipAlign($this->outputFilename);
  57. if ($result === null) {
  58. return;
  59. } // zip align not installed
  60. // check not zip align
  61. static::assertFalse($result);
  62. $zipFile->openFile($this->outputFilename);
  63. $zipFile->setZipAlign(4);
  64. $zipFile->saveAsFile($this->outputFilename);
  65. $zipFile->close();
  66. static::assertCorrectZipArchive($this->outputFilename);
  67. $result = static::assertVerifyZipAlign($this->outputFilename, true);
  68. static::assertNotNull($result);
  69. // check zip align
  70. static::assertTrue($result);
  71. }
  72. /**
  73. * @throws ZipException
  74. * @throws \Exception
  75. */
  76. public function testZipAlignNewFiles()
  77. {
  78. $zipFile = new ZipFile();
  79. for ($i = 0; $i < 100; $i++) {
  80. $zipFile->addFromString(
  81. 'entry' . $i . '.txt',
  82. random_bytes(random_int(100, 4096)),
  83. ZipCompressionMethod::STORED
  84. );
  85. }
  86. $zipFile->setZipAlign(4);
  87. $zipFile->saveAsFile($this->outputFilename);
  88. $zipFile->close();
  89. static::assertCorrectZipArchive($this->outputFilename);
  90. $result = static::assertVerifyZipAlign($this->outputFilename);
  91. if ($result === null) {
  92. return;
  93. } // zip align not installed
  94. // check not zip align
  95. static::assertTrue($result);
  96. }
  97. /**
  98. * @throws ZipException
  99. * @throws \Exception
  100. */
  101. public function testZipAlignFromModifiedZipArchive()
  102. {
  103. $zipFile = new ZipFile();
  104. for ($i = 0; $i < 100; $i++) {
  105. $zipFile->addFromString(
  106. 'entry' . $i . '.txt',
  107. random_bytes(random_int(100, 4096)),
  108. ZipCompressionMethod::STORED
  109. );
  110. }
  111. $zipFile->saveAsFile($this->outputFilename);
  112. $zipFile->close();
  113. static::assertCorrectZipArchive($this->outputFilename);
  114. $result = static::assertVerifyZipAlign($this->outputFilename);
  115. if ($result === null) {
  116. return;
  117. } // zip align not installed
  118. // check not zip align
  119. static::assertFalse($result);
  120. $zipFile->openFile($this->outputFilename);
  121. $zipFile->deleteFromRegex('~entry2[\\d]+\\.txt$~s');
  122. for ($i = 0; $i < 100; $i++) {
  123. $isStored = (bool) random_int(0, 1);
  124. $zipFile->addFromString(
  125. 'entry_new_' . ($isStored ? 'stored' : 'deflated') . '_' . $i . '.txt',
  126. random_bytes(random_int(100, 4096)),
  127. $isStored
  128. ? ZipCompressionMethod::STORED
  129. : ZipCompressionMethod::DEFLATED
  130. );
  131. }
  132. $zipFile->setZipAlign(4);
  133. $zipFile->saveAsFile($this->outputFilename);
  134. $zipFile->close();
  135. static::assertCorrectZipArchive($this->outputFilename);
  136. $result = static::assertVerifyZipAlign($this->outputFilename, true);
  137. static::assertNotNull($result);
  138. // check zip align
  139. static::assertTrue($result);
  140. }
  141. }