ZipStreamTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. <?php
  2. declare(strict_types=1);
  3. namespace ZipStreamTest;
  4. use GuzzleHttp\Psr7\Response;
  5. use org\bovigo\vfs\vfsStream;
  6. use PHPUnit\Framework\TestCase;
  7. use RecursiveDirectoryIterator;
  8. use RecursiveIteratorIterator;
  9. use ReflectionClass;
  10. use ZipArchive;
  11. use ZipStream\File;
  12. use ZipStream\Option\Archive as ArchiveOptions;
  13. use ZipStream\Option\File as FileOptions;
  14. use ZipStream\Option\Method;
  15. use ZipStream\Stream;
  16. use ZipStream\ZipStream;
  17. /**
  18. * Test Class for the Main ZipStream CLass
  19. */
  20. class ZipStreamTest extends TestCase
  21. {
  22. public function testFileNotFoundException(): void
  23. {
  24. $this->expectException(\ZipStream\Exception\FileNotFoundException::class);
  25. // Get ZipStream Object
  26. $zip = new ZipStream();
  27. // Trigger error by adding a file which doesn't exist
  28. $zip->addFileFromPath('foobar.php', '/foo/bar/foobar.php');
  29. }
  30. public function testFileNotReadableException(): void
  31. {
  32. // create new virtual filesystem
  33. $root = vfsStream::setup('vfs');
  34. // create a virtual file with no permissions
  35. $file = vfsStream::newFile('foo.txt', 0)->at($root)->setContent('bar');
  36. $zip = new ZipStream();
  37. $this->expectException(\ZipStream\Exception\FileNotReadableException::class);
  38. $zip->addFileFromPath('foo.txt', $file->url());
  39. }
  40. public function testDostime(): void
  41. {
  42. // Allows testing of protected method
  43. $class = new ReflectionClass(File::class);
  44. $method = $class->getMethod('dostime');
  45. $method->setAccessible(true);
  46. $this->assertSame($method->invoke(null, 1416246368), 1165069764);
  47. // January 1 1980 - DOS Epoch.
  48. $this->assertSame($method->invoke(null, 315532800), 2162688);
  49. // January 1 1970 -> January 1 1980 due to minimum DOS Epoch. @todo Throw Exception?
  50. $this->assertSame($method->invoke(null, 0), 2162688);
  51. }
  52. public function testAddFile(): void
  53. {
  54. [$tmp, $stream] = $this->getTmpFileStream();
  55. $options = new ArchiveOptions();
  56. $options->setOutputStream($stream);
  57. $zip = new ZipStream(null, $options);
  58. $zip->addFile('sample.txt', 'Sample String Data');
  59. $zip->addFile('test/sample.txt', 'More Simple Sample Data');
  60. $zip->finish();
  61. fclose($stream);
  62. $tmpDir = $this->validateAndExtractZip($tmp);
  63. $files = $this->getRecursiveFileList($tmpDir);
  64. $this->assertSame(['sample.txt', 'test' . DIRECTORY_SEPARATOR . 'sample.txt'], $files);
  65. $this->assertStringEqualsFile($tmpDir . '/sample.txt', 'Sample String Data');
  66. $this->assertStringEqualsFile($tmpDir . '/test/sample.txt', 'More Simple Sample Data');
  67. }
  68. public function testAddFileUtf8NameComment(): void
  69. {
  70. [$tmp, $stream] = $this->getTmpFileStream();
  71. $options = new ArchiveOptions();
  72. $options->setOutputStream($stream);
  73. $zip = new ZipStream(null, $options);
  74. $name = 'árvíztűrő tükörfúrógép.txt';
  75. $content = 'Sample String Data';
  76. $comment =
  77. 'Filename has every special characters ' .
  78. 'from Hungarian language in lowercase. ' .
  79. 'In uppercase: ÁÍŰŐÜÖÚÓÉ';
  80. $fileOptions = new FileOptions();
  81. $fileOptions->setComment($comment);
  82. $zip->addFile($name, $content, $fileOptions);
  83. $zip->finish();
  84. fclose($stream);
  85. $tmpDir = $this->validateAndExtractZip($tmp);
  86. $files = $this->getRecursiveFileList($tmpDir);
  87. $this->assertSame([$name], $files);
  88. $this->assertStringEqualsFile($tmpDir . '/' . $name, $content);
  89. $zipArch = new ZipArchive();
  90. $zipArch->open($tmp);
  91. $this->assertSame($comment, $zipArch->getCommentName($name));
  92. }
  93. public function testAddFileUtf8NameNonUtfComment(): void
  94. {
  95. [$tmp, $stream] = $this->getTmpFileStream();
  96. $options = new ArchiveOptions();
  97. $options->setOutputStream($stream);
  98. $zip = new ZipStream(null, $options);
  99. $name = 'á.txt';
  100. $content = 'any';
  101. $comment = mb_convert_encoding('á', 'ISO-8859-2', 'UTF-8');
  102. // @see https://libzip.org/documentation/zip_file_get_comment.html
  103. //
  104. // mb_convert_encoding hasn't CP437.
  105. // nearly CP850 (DOS-Latin-1)
  106. $guessComment = mb_convert_encoding($comment, 'UTF-8', 'CP850');
  107. $fileOptions = new FileOptions();
  108. $fileOptions->setComment($comment);
  109. $zip->addFile($name, $content, $fileOptions);
  110. $zip->finish();
  111. fclose($stream);
  112. $zipArch = new ZipArchive();
  113. $zipArch->open($tmp);
  114. $this->assertSame($guessComment, $zipArch->getCommentName($name));
  115. $this->assertSame($comment, $zipArch->getCommentName($name, ZipArchive::FL_ENC_RAW));
  116. }
  117. public function testAddFileNonUtf8NameUtfComment(): void
  118. {
  119. [$tmp, $stream] = $this->getTmpFileStream();
  120. $options = new ArchiveOptions();
  121. $options->setOutputStream($stream);
  122. $zip = new ZipStream(null, $options);
  123. $name = mb_convert_encoding('á.txt', 'ISO-8859-2', 'UTF-8');
  124. $content = 'any';
  125. $comment = 'á';
  126. // @see https://libzip.org/documentation/zip_get_name.html
  127. //
  128. // mb_convert_encoding hasn't CP437.
  129. // nearly CP850 (DOS-Latin-1)
  130. $guessName = mb_convert_encoding($name, 'UTF-8', 'CP850');
  131. $fileOptions = new FileOptions();
  132. $fileOptions->setComment($comment);
  133. $zip->addFile($name, $content, $fileOptions);
  134. $zip->finish();
  135. fclose($stream);
  136. $tmpDir = $this->validateAndExtractZip($tmp);
  137. $files = $this->getRecursiveFileList($tmpDir);
  138. $this->assertNotSame([$name], $files);
  139. $this->assertSame([$guessName], $files);
  140. $this->assertStringEqualsFile($tmpDir . '/' . $guessName, $content);
  141. $zipArch = new ZipArchive();
  142. $zipArch->open($tmp);
  143. $this->assertSame($guessName, $zipArch->getNameIndex(0));
  144. $this->assertSame($name, $zipArch->getNameIndex(0, ZipArchive::FL_ENC_RAW));
  145. $this->assertSame($comment, $zipArch->getCommentName($guessName));
  146. }
  147. public function testAddFileWithStorageMethod(): void
  148. {
  149. [$tmp, $stream] = $this->getTmpFileStream();
  150. $options = new ArchiveOptions();
  151. $options->setOutputStream($stream);
  152. $zip = new ZipStream(null, $options);
  153. $fileOptions = new FileOptions();
  154. $fileOptions->setMethod(Method::STORE());
  155. $zip->addFile('sample.txt', 'Sample String Data', $fileOptions);
  156. $zip->addFile('test/sample.txt', 'More Simple Sample Data');
  157. $zip->finish();
  158. fclose($stream);
  159. $zipArch = new ZipArchive();
  160. $zipArch->open($tmp);
  161. $sample1 = $zipArch->statName('sample.txt');
  162. $sample12 = $zipArch->statName('test/sample.txt');
  163. $this->assertSame($sample1['comp_method'], Method::STORE);
  164. $this->assertSame($sample12['comp_method'], Method::DEFLATE);
  165. $zipArch->close();
  166. }
  167. public function testAddFileFromPath(): void
  168. {
  169. [$tmp, $stream] = $this->getTmpFileStream();
  170. $options = new ArchiveOptions();
  171. $options->setOutputStream($stream);
  172. $zip = new ZipStream(null, $options);
  173. [$tmpExample, $streamExample] = $this->getTmpFileStream();
  174. fwrite($streamExample, 'Sample String Data');
  175. fclose($streamExample);
  176. $zip->addFileFromPath('sample.txt', $tmpExample);
  177. [$tmpExample, $streamExample] = $this->getTmpFileStream();
  178. fwrite($streamExample, 'More Simple Sample Data');
  179. fclose($streamExample);
  180. $zip->addFileFromPath('test/sample.txt', $tmpExample);
  181. $zip->finish();
  182. fclose($stream);
  183. $tmpDir = $this->validateAndExtractZip($tmp);
  184. $files = $this->getRecursiveFileList($tmpDir);
  185. $this->assertSame(['sample.txt', 'test' . DIRECTORY_SEPARATOR . 'sample.txt'], $files);
  186. $this->assertStringEqualsFile($tmpDir . '/sample.txt', 'Sample String Data');
  187. $this->assertStringEqualsFile($tmpDir . '/test/sample.txt', 'More Simple Sample Data');
  188. }
  189. public function testAddFileFromPathWithStorageMethod(): void
  190. {
  191. [$tmp, $stream] = $this->getTmpFileStream();
  192. $options = new ArchiveOptions();
  193. $options->setOutputStream($stream);
  194. $zip = new ZipStream(null, $options);
  195. $fileOptions = new FileOptions();
  196. $fileOptions->setMethod(Method::STORE());
  197. [$tmpExample, $streamExample] = $this->getTmpFileStream();
  198. fwrite($streamExample, 'Sample String Data');
  199. fclose($streamExample);
  200. $zip->addFileFromPath('sample.txt', $tmpExample, $fileOptions);
  201. [$tmpExample, $streamExample] = $this->getTmpFileStream();
  202. fwrite($streamExample, 'More Simple Sample Data');
  203. fclose($streamExample);
  204. $zip->addFileFromPath('test/sample.txt', $tmpExample);
  205. $zip->finish();
  206. fclose($stream);
  207. $zipArch = new ZipArchive();
  208. $zipArch->open($tmp);
  209. $sample1 = $zipArch->statName('sample.txt');
  210. $this->assertSame(Method::STORE, $sample1['comp_method']);
  211. $sample2 = $zipArch->statName('test/sample.txt');
  212. $this->assertSame(Method::DEFLATE, $sample2['comp_method']);
  213. $zipArch->close();
  214. }
  215. public function testAddLargeFileFromPath(): void
  216. {
  217. $methods = [Method::DEFLATE(), Method::STORE()];
  218. $falseTrue = [false, true];
  219. foreach ($methods as $method) {
  220. foreach ($falseTrue as $zeroHeader) {
  221. foreach ($falseTrue as $zip64) {
  222. if ($zeroHeader && $method->equals(Method::DEFLATE())) {
  223. continue;
  224. }
  225. $this->addLargeFileFileFromPath($method, $zeroHeader, $zip64);
  226. }
  227. }
  228. }
  229. }
  230. public function testAddFileFromStream(): void
  231. {
  232. [$tmp, $stream] = $this->getTmpFileStream();
  233. $options = new ArchiveOptions();
  234. $options->setOutputStream($stream);
  235. $zip = new ZipStream(null, $options);
  236. // In this test we can't use temporary stream to feed data
  237. // because zlib.deflate filter gives empty string before PHP 7
  238. // it works fine with file stream
  239. $streamExample = fopen(__FILE__, 'rb');
  240. $zip->addFileFromStream('sample.txt', $streamExample);
  241. // fclose($streamExample);
  242. $fileOptions = new FileOptions();
  243. $fileOptions->setMethod(Method::STORE());
  244. $streamExample2 = fopen('php://temp', 'wb+');
  245. fwrite($streamExample2, 'More Simple Sample Data');
  246. rewind($streamExample2); // move the pointer back to the beginning of file.
  247. $zip->addFileFromStream('test/sample.txt', $streamExample2, $fileOptions);
  248. // fclose($streamExample2);
  249. $zip->finish();
  250. fclose($stream);
  251. $tmpDir = $this->validateAndExtractZip($tmp);
  252. $files = $this->getRecursiveFileList($tmpDir);
  253. $this->assertSame(['sample.txt', 'test' . DIRECTORY_SEPARATOR . 'sample.txt'], $files);
  254. $this->assertStringEqualsFile(__FILE__, file_get_contents($tmpDir . '/sample.txt'));
  255. $this->assertStringEqualsFile($tmpDir . '/test/sample.txt', 'More Simple Sample Data');
  256. }
  257. public function testAddFileFromStreamWithStorageMethod(): void
  258. {
  259. [$tmp, $stream] = $this->getTmpFileStream();
  260. $options = new ArchiveOptions();
  261. $options->setOutputStream($stream);
  262. $zip = new ZipStream(null, $options);
  263. $fileOptions = new FileOptions();
  264. $fileOptions->setMethod(Method::STORE());
  265. $streamExample = fopen('php://temp', 'wb+');
  266. fwrite($streamExample, 'Sample String Data');
  267. rewind($streamExample); // move the pointer back to the beginning of file.
  268. $zip->addFileFromStream('sample.txt', $streamExample, $fileOptions);
  269. // fclose($streamExample);
  270. $streamExample2 = fopen('php://temp', 'bw+');
  271. fwrite($streamExample2, 'More Simple Sample Data');
  272. rewind($streamExample2); // move the pointer back to the beginning of file.
  273. $zip->addFileFromStream('test/sample.txt', $streamExample2);
  274. // fclose($streamExample2);
  275. $zip->finish();
  276. fclose($stream);
  277. $zipArch = new ZipArchive();
  278. $zipArch->open($tmp);
  279. $sample1 = $zipArch->statName('sample.txt');
  280. $this->assertSame(Method::STORE, $sample1['comp_method']);
  281. $sample2 = $zipArch->statName('test/sample.txt');
  282. $this->assertSame(Method::DEFLATE, $sample2['comp_method']);
  283. $zipArch->close();
  284. }
  285. public function testAddFileFromPsr7Stream(): void
  286. {
  287. [$tmp, $stream] = $this->getTmpFileStream();
  288. $options = new ArchiveOptions();
  289. $options->setOutputStream($stream);
  290. $zip = new ZipStream(null, $options);
  291. $body = 'Sample String Data';
  292. $response = new Response(200, [], $body);
  293. $fileOptions = new FileOptions();
  294. $fileOptions->setMethod(Method::STORE());
  295. $zip->addFileFromPsr7Stream('sample.json', $response->getBody(), $fileOptions);
  296. $zip->finish();
  297. fclose($stream);
  298. $tmpDir = $this->validateAndExtractZip($tmp);
  299. $files = $this->getRecursiveFileList($tmpDir);
  300. $this->assertSame(['sample.json'], $files);
  301. $this->assertStringEqualsFile($tmpDir . '/sample.json', $body);
  302. }
  303. public function testAddFileFromPsr7StreamWithOutputToPsr7Stream(): void
  304. {
  305. [$tmp, $resource] = $this->getTmpFileStream();
  306. $psr7OutputStream = new Stream($resource);
  307. $options = new ArchiveOptions();
  308. $options->setOutputStream($psr7OutputStream);
  309. $zip = new ZipStream(null, $options);
  310. $body = 'Sample String Data';
  311. $response = new Response(200, [], $body);
  312. $fileOptions = new FileOptions();
  313. $fileOptions->setMethod(Method::STORE());
  314. $zip->addFileFromPsr7Stream('sample.json', $response->getBody(), $fileOptions);
  315. $zip->finish();
  316. $psr7OutputStream->close();
  317. $tmpDir = $this->validateAndExtractZip($tmp);
  318. $files = $this->getRecursiveFileList($tmpDir);
  319. $this->assertSame(['sample.json'], $files);
  320. $this->assertStringEqualsFile($tmpDir . '/sample.json', $body);
  321. }
  322. public function testAddFileFromPsr7StreamWithFileSizeSet(): void
  323. {
  324. [$tmp, $stream] = $this->getTmpFileStream();
  325. $options = new ArchiveOptions();
  326. $options->setOutputStream($stream);
  327. $zip = new ZipStream(null, $options);
  328. $body = 'Sample String Data';
  329. $fileSize = strlen($body);
  330. // Add fake padding
  331. $fakePadding = "\0\0\0\0\0\0";
  332. $response = new Response(200, [], $body . $fakePadding);
  333. $fileOptions = new FileOptions();
  334. $fileOptions->setMethod(Method::STORE());
  335. $fileOptions->setSize($fileSize);
  336. $zip->addFileFromPsr7Stream('sample.json', $response->getBody(), $fileOptions);
  337. $zip->finish();
  338. fclose($stream);
  339. $tmpDir = $this->validateAndExtractZip($tmp);
  340. $files = $this->getRecursiveFileList($tmpDir);
  341. $this->assertSame(['sample.json'], $files);
  342. $this->assertStringEqualsFile($tmpDir . '/sample.json', $body);
  343. }
  344. public function testCreateArchiveWithFlushOptionSet(): void
  345. {
  346. [$tmp, $stream] = $this->getTmpFileStream();
  347. $options = new ArchiveOptions();
  348. $options->setOutputStream($stream);
  349. $options->setFlushOutput(true);
  350. $zip = new ZipStream(null, $options);
  351. $zip->addFile('sample.txt', 'Sample String Data');
  352. $zip->addFile('test/sample.txt', 'More Simple Sample Data');
  353. $zip->finish();
  354. fclose($stream);
  355. $tmpDir = $this->validateAndExtractZip($tmp);
  356. $files = $this->getRecursiveFileList($tmpDir);
  357. $this->assertSame(['sample.txt', 'test' . DIRECTORY_SEPARATOR . 'sample.txt'], $files);
  358. $this->assertStringEqualsFile($tmpDir . '/sample.txt', 'Sample String Data');
  359. $this->assertStringEqualsFile($tmpDir . '/test/sample.txt', 'More Simple Sample Data');
  360. }
  361. public function testCreateArchiveWithOutputBufferingOffAndFlushOptionSet(): void
  362. {
  363. // WORKAROUND (1/2): remove phpunit's output buffer in order to run test without any buffering
  364. ob_end_flush();
  365. $this->assertSame(0, ob_get_level());
  366. [$tmp, $stream] = $this->getTmpFileStream();
  367. $options = new ArchiveOptions();
  368. $options->setOutputStream($stream);
  369. $options->setFlushOutput(true);
  370. $zip = new ZipStream(null, $options);
  371. $zip->addFile('sample.txt', 'Sample String Data');
  372. $zip->finish();
  373. fclose($stream);
  374. $tmpDir = $this->validateAndExtractZip($tmp);
  375. $this->assertStringEqualsFile($tmpDir . '/sample.txt', 'Sample String Data');
  376. // WORKAROUND (2/2): add back output buffering so that PHPUnit doesn't complain that it is missing
  377. ob_start();
  378. }
  379. /**
  380. * @return array
  381. */
  382. protected function getTmpFileStream(): array
  383. {
  384. $tmp = tempnam(sys_get_temp_dir(), 'zipstreamtest');
  385. $stream = fopen($tmp, 'wb+');
  386. return [$tmp, $stream];
  387. }
  388. /**
  389. * @param string $tmp
  390. * @return string
  391. */
  392. protected function validateAndExtractZip($tmp): string
  393. {
  394. $tmpDir = $this->getTmpDir();
  395. $zipArch = new ZipArchive();
  396. $res = $zipArch->open($tmp);
  397. if ($res !== true) {
  398. $this->fail("Failed to open {$tmp}. Code: $res");
  399. return $tmpDir;
  400. }
  401. $this->assertSame(0, $zipArch->status);
  402. $this->assertSame(0, $zipArch->statusSys);
  403. $zipArch->extractTo($tmpDir);
  404. $zipArch->close();
  405. return $tmpDir;
  406. }
  407. protected function getTmpDir(): string
  408. {
  409. $tmp = tempnam(sys_get_temp_dir(), 'zipstreamtest');
  410. unlink($tmp);
  411. mkdir($tmp) or $this->fail('Failed to make directory');
  412. return $tmp;
  413. }
  414. /**
  415. * @param string $path
  416. * @return string[]
  417. */
  418. protected function getRecursiveFileList(string $path): array
  419. {
  420. $data = [];
  421. $path = (string)realpath($path);
  422. $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
  423. $pathLen = strlen($path);
  424. foreach ($files as $file) {
  425. $filePath = $file->getRealPath();
  426. if (!is_dir($filePath)) {
  427. $data[] = substr($filePath, $pathLen + 1);
  428. }
  429. }
  430. sort($data);
  431. return $data;
  432. }
  433. protected function addLargeFileFileFromPath($method, $zeroHeader, $zip64): void
  434. {
  435. [$tmp, $stream] = $this->getTmpFileStream();
  436. $options = new ArchiveOptions();
  437. $options->setOutputStream($stream);
  438. $options->setLargeFileMethod($method);
  439. $options->setLargeFileSize(5);
  440. $options->setZeroHeader($zeroHeader);
  441. $options->setEnableZip64($zip64);
  442. $zip = new ZipStream(null, $options);
  443. [$tmpExample, $streamExample] = $this->getTmpFileStream();
  444. for ($i = 0; $i <= 10000; $i++) {
  445. fwrite($streamExample, sha1((string)$i));
  446. if ($i % 100 === 0) {
  447. fwrite($streamExample, "\n");
  448. }
  449. }
  450. fclose($streamExample);
  451. $shaExample = sha1_file($tmpExample);
  452. $zip->addFileFromPath('sample.txt', $tmpExample);
  453. unlink($tmpExample);
  454. $zip->finish();
  455. fclose($stream);
  456. $tmpDir = $this->validateAndExtractZip($tmp);
  457. $files = $this->getRecursiveFileList($tmpDir);
  458. $this->assertSame(['sample.txt'], $files);
  459. $this->assertSame(sha1_file($tmpDir . '/sample.txt'), $shaExample, "SHA-1 Mismatch Method: {$method}");
  460. }
  461. }