TestZipFile.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. class TestZipFile extends \PHPUnit\Framework\TestCase
  3. {
  4. public function testCreate()
  5. {
  6. $output = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'test-create.zip';
  7. $extractOutputDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'test-create';
  8. $listFilename = 'files.txt';
  9. $listFileContent = implode(PHP_EOL, glob('*'));
  10. $dirName = 'src/';
  11. $archiveComment = 'Archive comment - 😀';
  12. $commentIndex0 = basename(__FILE__);
  13. $zip = new \Nelexa\Zip\ZipFile();
  14. $zip->create();
  15. $zip->addFile(__FILE__);
  16. $zip->addFromString($listFilename, $listFileContent);
  17. $zip->addEmptyDir($dirName);
  18. $zip->setArchiveComment($archiveComment);
  19. $zip->setCommentIndex(0, $commentIndex0);
  20. $zip->saveAs($output);
  21. $zip->close();
  22. $this->assertTrue(file_exists($output));
  23. $this->assertCorrectZipArchive($output);
  24. $zip = new \Nelexa\Zip\ZipFile();
  25. $zip->open($output);
  26. $listFiles = $zip->getListFiles();
  27. $this->assertEquals(sizeof($listFiles), 3);
  28. $filenameIndex0 = basename(__FILE__);
  29. $this->assertEquals($listFiles[0], $filenameIndex0);
  30. $this->assertEquals($listFiles[1], $listFilename);
  31. $this->assertEquals($listFiles[2], $dirName);
  32. $this->assertEquals($zip->getFromIndex(0), $zip->getFromName(basename(__FILE__)));
  33. $this->assertEquals($zip->getFromIndex(0), file_get_contents(__FILE__));
  34. $this->assertEquals($zip->getFromIndex(1), $zip->getFromName($listFilename));
  35. $this->assertEquals($zip->getFromIndex(1), $listFileContent);
  36. $this->assertEquals($zip->getArchiveComment(), $archiveComment);
  37. $this->assertEquals($zip->getCommentIndex(0), $commentIndex0);
  38. if (!file_exists($extractOutputDir)) {
  39. $this->assertTrue(mkdir($extractOutputDir, 0755, true));
  40. }
  41. $zip->extractTo($extractOutputDir);
  42. $this->assertTrue(file_exists($extractOutputDir . DIRECTORY_SEPARATOR . $filenameIndex0));
  43. $this->assertEquals(md5_file($extractOutputDir . DIRECTORY_SEPARATOR . $filenameIndex0), md5_file(__FILE__));
  44. $this->assertTrue(file_exists($extractOutputDir . DIRECTORY_SEPARATOR . $listFilename));
  45. $this->assertEquals(file_get_contents($extractOutputDir . DIRECTORY_SEPARATOR . $listFilename), $listFileContent);
  46. $zip->close();
  47. unlink($output);
  48. $files = new RecursiveIteratorIterator(
  49. new RecursiveDirectoryIterator($extractOutputDir, RecursiveDirectoryIterator::SKIP_DOTS),
  50. RecursiveIteratorIterator::CHILD_FIRST
  51. );
  52. foreach ($files as $fileInfo) {
  53. $todo = ($fileInfo->isDir() ? 'rmdir' : 'unlink');
  54. $todo($fileInfo->getRealPath());
  55. }
  56. rmdir($extractOutputDir);
  57. }
  58. /**
  59. *
  60. */
  61. public function testUpdate()
  62. {
  63. $file = __DIR__ . '/res/file.apk';
  64. $privateKey = __DIR__ . '/res/private.pem';
  65. $publicKey = __DIR__ . '/res/public.pem';
  66. $outputFile = sys_get_temp_dir() . '/test-update.apk';
  67. $zip = new \Nelexa\Zip\ZipFile($file);
  68. $zip->open($file);
  69. // signed apk file
  70. $certList = array();
  71. $manifestMf = new Manifest();
  72. $manifestMf->appendLine("Manifest-Version: 1.0");
  73. $manifestMf->appendLine("Created-By: 1.0 (Android)");
  74. $manifestMf->appendLine('');
  75. for ($i = 0, $length = $zip->getCountFiles(); $i < $length; $i++) {
  76. $name = $zip->getNameIndex($i);
  77. if ($name[strlen($name) - 1] === '/') continue; // is path
  78. $content = $zip->getFromIndex($i);
  79. $certManifest = $this->createSha1EncodeEntryManifest($name, $content);
  80. $manifestMf->appendManifest($certManifest);
  81. $certList[$name] = $certManifest;
  82. }
  83. $manifestMf = $manifestMf->getContent();
  84. $certSf = new Manifest();
  85. $certSf->appendLine('Signature-Version: 1.0');
  86. $certSf->appendLine('Created-By: 1.0 (Android)');
  87. $certSf->appendLine('SHA1-Digest-Manifest: ' . base64_encode(sha1($manifestMf, 1)));
  88. $certSf->appendLine('');
  89. foreach ($certList AS $filename => $content) {
  90. $certManifest = $this->createSha1EncodeEntryManifest($filename, $content->getContent());
  91. $certSf->appendManifest($certManifest);
  92. }
  93. $certSf = $certSf->getContent();
  94. unset($certList);
  95. $zip->addFromString('META-INF/MANIFEST.MF', $manifestMf);
  96. $zip->addFromString('META-INF/CERT.SF', $certSf);
  97. if (`which openssl`) {
  98. $openssl_cmd = 'printf ' . escapeshellarg($certSf) . ' | openssl smime -md sha1 -sign -inkey ' . escapeshellarg($privateKey) . ' -signer ' . $publicKey . ' -binary -outform DER -noattr';
  99. ob_start();
  100. passthru($openssl_cmd, $error);
  101. $rsaContent = ob_get_clean();
  102. $this->assertEquals($error, 0);
  103. $zip->addFromString('META-INF/CERT.RSA', $rsaContent);
  104. }
  105. $zip->saveAs($outputFile);
  106. $zip->close();
  107. $this->assertCorrectZipArchive($outputFile);
  108. if (`which jarsigner`) {
  109. ob_start();
  110. passthru('jarsigner -verify -verbose -certs ' . escapeshellarg($outputFile), $error);
  111. $verifedResult = ob_get_clean();
  112. $this->assertEquals($error, 0);
  113. $this->assertContains('jar verified', $verifedResult);
  114. }
  115. unlink($outputFile);
  116. }
  117. /**
  118. * @param $filename
  119. */
  120. private function assertCorrectZipArchive($filename)
  121. {
  122. exec("zip -T " . escapeshellarg($filename), $output, $returnCode);
  123. $this->assertEquals($returnCode, 0);
  124. }
  125. /**
  126. * @param string $filename
  127. * @param string $content
  128. * @return Manifest
  129. */
  130. private function createSha1EncodeEntryManifest($filename, $content)
  131. {
  132. $manifest = new Manifest();
  133. $manifest->appendLine('Name: ' . $filename);
  134. $manifest->appendLine('SHA1-Digest: ' . base64_encode(sha1($content, 1)));
  135. return $manifest;
  136. }
  137. }
  138. class Manifest
  139. {
  140. private $content;
  141. /**
  142. * @return mixed
  143. */
  144. public function getContent()
  145. {
  146. return trim($this->content) . "\r\n\r\n";
  147. }
  148. /**
  149. * Process a long manifest line and add continuation if required
  150. * @param $line string
  151. * @return Manifest
  152. */
  153. public function appendLine($line)
  154. {
  155. $begin = 0;
  156. $sb = '';
  157. $lineLength = mb_strlen($line, "UTF-8");
  158. for ($end = 70; $lineLength - $begin > 70; $end += 69) {
  159. $sb .= mb_substr($line, $begin, $end - $begin, "UTF-8") . "\r\n ";
  160. $begin = $end;
  161. }
  162. $this->content .= $sb . mb_substr($line, $begin, $lineLength, "UTF-8") . "\r\n";
  163. return $this;
  164. }
  165. public function appendManifest(Manifest $manifest)
  166. {
  167. $this->content .= $manifest->getContent();
  168. return $this;
  169. }
  170. public function clear()
  171. {
  172. $this->content = '';
  173. }
  174. /**
  175. * @param string $manifestContent
  176. * @return Manifest
  177. */
  178. public static function createFromManifest($manifestContent)
  179. {
  180. $manifestContent = trim($manifestContent);
  181. $lines = explode("\n", $manifestContent);
  182. // normalize manifest
  183. $content = '';
  184. $trim = array("\r", "\n");
  185. foreach ($lines AS $line) {
  186. $line = str_replace($trim, '', $line);
  187. if ($line[0] === ' ') {
  188. $content = rtrim($content, "\n\r");
  189. $line = ltrim($line);
  190. }
  191. $content .= $line . "\r\n";
  192. }
  193. $manifset = new self;
  194. $lines = explode("\n", $content);
  195. foreach ($lines AS $line) {
  196. $line = trim($line, "\n\r");
  197. $manifset->appendLine($line);
  198. }
  199. return $manifset;
  200. }
  201. }