口腔客户管理系统
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ZipStreamTest.php 18KB

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