ZipFileTest.php 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196
  1. <?php
  2. namespace PhpZip;
  3. use PhpZip\Exception\InvalidArgumentException;
  4. use PhpZip\Exception\ZipEntryNotFoundException;
  5. use PhpZip\Exception\ZipException;
  6. use PhpZip\Exception\ZipUnsupportMethodException;
  7. use PhpZip\Model\ZipEntry;
  8. use PhpZip\Model\ZipInfo;
  9. use PhpZip\Util\CryptoUtil;
  10. use PhpZip\Util\FilesUtil;
  11. use Psr\Http\Message\ResponseInterface;
  12. use Zend\Diactoros\Response;
  13. /**
  14. * ZipFile test.
  15. *
  16. * @internal
  17. *
  18. * @small
  19. * @covers
  20. */
  21. class ZipFileTest extends ZipTestCase
  22. {
  23. /**
  24. * @throws ZipException
  25. */
  26. public function testOpenFileCantExists()
  27. {
  28. $this->setExpectedException(ZipException::class, 'does not exist');
  29. $zipFile = new ZipFile();
  30. $zipFile->openFile(uniqid('', true));
  31. }
  32. /**
  33. * @throws ZipException
  34. */
  35. public function testOpenFileCantOpen()
  36. {
  37. $this->setExpectedException(ZipException::class, 'can\'t open');
  38. /** @noinspection PhpComposerExtensionStubsInspection */
  39. if (posix_getuid() === 0) {
  40. static::markTestSkipped('Skip the test for a user with root privileges');
  41. }
  42. static::assertNotFalse(file_put_contents($this->outputFilename, 'content'));
  43. static::assertTrue(chmod($this->outputFilename, 0222));
  44. $zipFile = new ZipFile();
  45. $zipFile->openFile($this->outputFilename);
  46. }
  47. /**
  48. * @throws ZipException
  49. */
  50. public function testOpenFileEmptyFile()
  51. {
  52. $this->setExpectedException(ZipException::class, 'Invalid zip file');
  53. static::assertNotFalse(touch($this->outputFilename));
  54. $zipFile = new ZipFile();
  55. $zipFile->openFile($this->outputFilename);
  56. }
  57. /**
  58. * @throws ZipException
  59. */
  60. public function testOpenFileInvalidZip()
  61. {
  62. $this->setExpectedException(
  63. ZipException::class,
  64. 'Expected Local File Header or (ZIP64) End Of Central Directory Record'
  65. );
  66. static::assertNotFalse(file_put_contents($this->outputFilename, CryptoUtil::randomBytes(255)));
  67. $zipFile = new ZipFile();
  68. $zipFile->openFile($this->outputFilename);
  69. }
  70. /**
  71. * @throws ZipException
  72. */
  73. public function testOpenFromStringNullString()
  74. {
  75. $this->setExpectedException(InvalidArgumentException::class, 'Empty string passed');
  76. $zipFile = new ZipFile();
  77. $zipFile->openFromString(null);
  78. }
  79. /**
  80. * @throws ZipException
  81. */
  82. public function testOpenFromStringEmptyString()
  83. {
  84. $this->setExpectedException(InvalidArgumentException::class, 'Empty string passed');
  85. $zipFile = new ZipFile();
  86. $zipFile->openFromString('');
  87. }
  88. /**
  89. * @throws ZipException
  90. */
  91. public function testOpenFromStringInvalidZip()
  92. {
  93. $this->setExpectedException(
  94. ZipException::class,
  95. 'Expected Local File Header or (ZIP64) End Of Central Directory Record'
  96. );
  97. $zipFile = new ZipFile();
  98. $zipFile->openFromString(CryptoUtil::randomBytes(255));
  99. }
  100. /**
  101. * @throws ZipException
  102. */
  103. public function testOpenFromString()
  104. {
  105. $zipFile = new ZipFile();
  106. $zipFile->addFromString('file', 'content');
  107. $zipFile['file2'] = 'content 2';
  108. $zipContents = $zipFile->outputAsString();
  109. $zipFile->close();
  110. $zipFile->openFromString($zipContents);
  111. static::assertSame($zipFile->count(), 2);
  112. static::assertTrue(isset($zipFile['file']));
  113. static::assertTrue(isset($zipFile['file2']));
  114. static::assertSame($zipFile['file'], 'content');
  115. static::assertSame($zipFile['file2'], 'content 2');
  116. $zipFile->close();
  117. }
  118. /**
  119. * @throws ZipException
  120. */
  121. public function testOpenFromStreamNullStream()
  122. {
  123. $this->setExpectedException(InvalidArgumentException::class, 'Invalid stream resource');
  124. $zipFile = new ZipFile();
  125. $zipFile->openFromStream(null);
  126. }
  127. /**
  128. * @throws ZipException
  129. */
  130. public function testOpenFromStreamInvalidResourceType()
  131. {
  132. $this->setExpectedException(InvalidArgumentException::class, 'Invalid stream resource');
  133. $zipFile = new ZipFile();
  134. /** @noinspection PhpParamsInspection */
  135. $zipFile->openFromStream('stream resource');
  136. }
  137. /**
  138. * @throws ZipException
  139. */
  140. public function testOpenFromStreamInvalidResourceType2()
  141. {
  142. $this->setExpectedException(InvalidArgumentException::class, 'Invalid resource type - gd.');
  143. $zipFile = new ZipFile();
  144. if (!\extension_loaded('gd')) {
  145. static::markTestSkipped('not extension gd');
  146. }
  147. /** @noinspection PhpComposerExtensionStubsInspection */
  148. $zipFile->openFromStream(imagecreate(1, 1));
  149. }
  150. /**
  151. * @throws ZipException
  152. */
  153. public function testOpenFromStreamInvalidResourceType3()
  154. {
  155. $this->setExpectedException(InvalidArgumentException::class, 'Invalid stream type - dir.');
  156. $zipFile = new ZipFile();
  157. $zipFile->openFromStream(opendir(__DIR__));
  158. }
  159. /**
  160. * @throws ZipException
  161. * @noinspection PhpUsageOfSilenceOperatorInspection
  162. * @noinspection NestedPositiveIfStatementsInspection
  163. */
  164. public function testOpenFromStreamNoSeekable()
  165. {
  166. $this->setExpectedException(InvalidArgumentException::class, 'Resource cannot seekable stream.');
  167. if (!$fp = @fopen('http://localhost', 'rb')) {
  168. if (!$fp = @fopen('http://example.org', 'rb')) {
  169. static::markTestSkipped('not connected to localhost or remote host');
  170. return;
  171. }
  172. }
  173. $zipFile = new ZipFile();
  174. $zipFile->openFromStream($fp);
  175. }
  176. /**
  177. * @throws ZipException
  178. */
  179. public function testOpenFromStreamEmptyContents()
  180. {
  181. $this->setExpectedException(ZipException::class, 'Invalid zip file');
  182. $fp = fopen($this->outputFilename, 'w+b');
  183. $zipFile = new ZipFile();
  184. $zipFile->openFromStream($fp);
  185. }
  186. /**
  187. * @throws ZipException
  188. */
  189. public function testOpenFromStreamInvalidZip()
  190. {
  191. $this->setExpectedException(
  192. ZipException::class,
  193. 'Expected Local File Header or (ZIP64) End Of Central Directory Record'
  194. );
  195. $fp = fopen($this->outputFilename, 'w+b');
  196. fwrite($fp, CryptoUtil::randomBytes(255));
  197. $zipFile = new ZipFile();
  198. $zipFile->openFromStream($fp);
  199. }
  200. /**
  201. * @throws ZipException
  202. */
  203. public function testOpenFromStream()
  204. {
  205. $zipFile = new ZipFile();
  206. $zipFile
  207. ->addFromString('file', 'content')
  208. ->saveAsFile($this->outputFilename)
  209. ->close()
  210. ;
  211. $handle = fopen($this->outputFilename, 'rb');
  212. $zipFile->openFromStream($handle);
  213. static::assertSame($zipFile->count(), 1);
  214. static::assertTrue(isset($zipFile['file']));
  215. static::assertSame($zipFile['file'], 'content');
  216. $zipFile->close();
  217. }
  218. /**
  219. * Test create, open and extract empty archive.
  220. *
  221. * @throws ZipException
  222. */
  223. public function testEmptyArchive()
  224. {
  225. $zipFile = new ZipFile();
  226. $zipFile
  227. ->saveAsFile($this->outputFilename)
  228. ->close()
  229. ;
  230. static::assertCorrectEmptyZip($this->outputFilename);
  231. static::assertTrue(mkdir($this->outputDirname, 0755, true));
  232. $zipFile->openFile($this->outputFilename);
  233. static::assertSame($zipFile->count(), 0);
  234. $zipFile
  235. ->extractTo($this->outputDirname)
  236. ->close()
  237. ;
  238. static::assertTrue(FilesUtil::isEmptyDir($this->outputDirname));
  239. }
  240. /**
  241. * No modified archive.
  242. *
  243. * @throws ZipException
  244. *
  245. * @see ZipOutputFile::create()
  246. */
  247. public function testNoModifiedArchive()
  248. {
  249. static::assertTrue(mkdir($this->outputDirname, 0755, true));
  250. $fileActual = $this->outputDirname . \DIRECTORY_SEPARATOR . 'file_actual.zip';
  251. $fileExpected = $this->outputDirname . \DIRECTORY_SEPARATOR . 'file_expected.zip';
  252. $zipFile = new ZipFile();
  253. $zipFile->addDirRecursive(__DIR__ . '/../../src');
  254. $sourceCount = $zipFile->count();
  255. static::assertTrue($sourceCount > 0);
  256. $zipFile
  257. ->saveAsFile($fileActual)
  258. ->close()
  259. ;
  260. static::assertCorrectZipArchive($fileActual);
  261. $zipFile
  262. ->openFile($fileActual)
  263. ->saveAsFile($fileExpected)
  264. ;
  265. static::assertCorrectZipArchive($fileExpected);
  266. $zipFileExpected = new ZipFile();
  267. $zipFileExpected->openFile($fileExpected);
  268. static::assertSame($zipFile->count(), $sourceCount);
  269. static::assertSame($zipFileExpected->count(), $zipFile->count());
  270. static::assertSame($zipFileExpected->getListFiles(), $zipFile->getListFiles());
  271. foreach ($zipFile as $entryName => $content) {
  272. static::assertSame($zipFileExpected[$entryName], $content);
  273. }
  274. $zipFileExpected->close();
  275. $zipFile->close();
  276. }
  277. /**
  278. * Create archive and add files.
  279. *
  280. * @throws ZipException
  281. *
  282. * @see ZipOutputFile::addFromFile()
  283. * @see ZipOutputFile::addFromStream()
  284. * @see ZipFile::getEntryContents()
  285. * @see ZipOutputFile::addFromString()
  286. */
  287. public function testCreateArchiveAndAddFiles()
  288. {
  289. $outputFromString = file_get_contents(__FILE__);
  290. $outputFromString2 = file_get_contents(\dirname(\dirname(__DIR__)) . \DIRECTORY_SEPARATOR . 'README.md');
  291. $outputFromFile = file_get_contents(\dirname(\dirname(__DIR__)) . \DIRECTORY_SEPARATOR . 'phpunit.xml');
  292. $outputFromStream = file_get_contents(\dirname(\dirname(__DIR__)) . \DIRECTORY_SEPARATOR . 'composer.json');
  293. $filenameFromString = basename(__FILE__);
  294. $filenameFromString2 = 'test_file.txt';
  295. $filenameFromFile = 'data/test file.txt';
  296. $filenameFromStream = 'data/ডিরেক্টরি/αρχείο.json';
  297. $emptyDirName = 'empty dir/пустой каталог/空目錄/ไดเรกทอรีที่ว่างเปล่า/';
  298. $emptyDirName2 = 'empty dir/пустой каталог/';
  299. $emptyDirName3 = 'empty dir/пустой каталог/ещё один пустой каталог/';
  300. $tempFile = tempnam(sys_get_temp_dir(), 'txt');
  301. file_put_contents($tempFile, $outputFromFile);
  302. $tempStream = tmpfile();
  303. fwrite($tempStream, $outputFromStream);
  304. $zipFile = new ZipFile();
  305. $zipFile
  306. ->addFromString($filenameFromString, $outputFromString)
  307. ->addFile($tempFile, $filenameFromFile)
  308. ->addFromStream($tempStream, $filenameFromStream)
  309. ->addEmptyDir($emptyDirName)
  310. ;
  311. $zipFile[$filenameFromString2] = $outputFromString2;
  312. $zipFile[$emptyDirName2] = null;
  313. $zipFile[$emptyDirName3] = 'this content ignoring';
  314. static::assertSame(\count($zipFile), 7);
  315. $zipFile
  316. ->saveAsFile($this->outputFilename)
  317. ->close()
  318. ;
  319. unlink($tempFile);
  320. static::assertCorrectZipArchive($this->outputFilename);
  321. $zipFile->openFile($this->outputFilename);
  322. static::assertSame(\count($zipFile), 7);
  323. static::assertSame($zipFile[$filenameFromString], $outputFromString);
  324. static::assertSame($zipFile[$filenameFromFile], $outputFromFile);
  325. static::assertSame($zipFile[$filenameFromStream], $outputFromStream);
  326. static::assertSame($zipFile[$filenameFromString2], $outputFromString2);
  327. static::assertTrue(isset($zipFile[$emptyDirName]));
  328. static::assertTrue(isset($zipFile[$emptyDirName2]));
  329. static::assertTrue(isset($zipFile[$emptyDirName3]));
  330. static::assertTrue($zipFile->isDirectory($emptyDirName));
  331. static::assertTrue($zipFile->isDirectory($emptyDirName2));
  332. static::assertTrue($zipFile->isDirectory($emptyDirName3));
  333. $listFiles = $zipFile->getListFiles();
  334. static::assertSame($listFiles[0], $filenameFromString);
  335. static::assertSame($listFiles[1], $filenameFromFile);
  336. static::assertSame($listFiles[2], $filenameFromStream);
  337. static::assertSame($listFiles[3], $emptyDirName);
  338. static::assertSame($listFiles[4], $filenameFromString2);
  339. static::assertSame($listFiles[5], $emptyDirName2);
  340. static::assertSame($listFiles[6], $emptyDirName3);
  341. $zipFile->close();
  342. }
  343. /**
  344. * @throws ZipException
  345. */
  346. public function testEmptyContent()
  347. {
  348. $zipFile = new ZipFile();
  349. $zipFile['file'] = '';
  350. $zipFile->saveAsFile($this->outputFilename);
  351. $zipFile->close();
  352. $zipFile->openFile($this->outputFilename);
  353. static::assertSame($zipFile['file'], '');
  354. $zipFile->close();
  355. }
  356. /**
  357. * Test compression method from image file.
  358. *
  359. * @throws ZipException
  360. */
  361. public function testCompressionMethodFromImageMimeType()
  362. {
  363. if (!\function_exists('mime_content_type')) {
  364. static::markTestSkipped('Function mime_content_type not exists');
  365. }
  366. $outputFilename = $this->outputFilename;
  367. $this->outputFilename .= '.gif';
  368. static::assertNotFalse(
  369. file_put_contents(
  370. $this->outputFilename,
  371. base64_decode('R0lGODlhAQABAJAAAP8AAAAAACH5BAUQAAAALAAAAAABAAEAAAICBAEAOw==')
  372. )
  373. );
  374. $basename = basename($this->outputFilename);
  375. $zipFile = new ZipFile();
  376. $zipFile->addFile($this->outputFilename, $basename);
  377. $zipFile->saveAsFile($outputFilename);
  378. unlink($this->outputFilename);
  379. $this->outputFilename = $outputFilename;
  380. $zipFile->openFile($this->outputFilename);
  381. $info = $zipFile->getEntryInfo($basename);
  382. static::assertSame($info->getMethodName(), 'No compression');
  383. $zipFile->close();
  384. }
  385. /**
  386. * Rename zip entry name.
  387. *
  388. * @throws ZipException
  389. */
  390. public function testRename()
  391. {
  392. $oldName = basename(__FILE__);
  393. $newName = 'tests/' . $oldName;
  394. $zipFile = new ZipFile();
  395. $zipFile->addDir(__DIR__);
  396. $zipFile->saveAsFile($this->outputFilename);
  397. $zipFile->close();
  398. static::assertCorrectZipArchive($this->outputFilename);
  399. $zipFile->openFile($this->outputFilename);
  400. $zipFile->rename($oldName, $newName);
  401. $zipFile->addFromString('file1.txt', 'content');
  402. $zipFile->addFromString('file2.txt', 'content');
  403. $zipFile->addFromString('file3.txt', 'content');
  404. $zipFile->rename('file1.txt', 'file_long_name.txt');
  405. $zipFile->rename('file2.txt', 'file4.txt');
  406. $zipFile->rename('file3.txt', 'fi.txt');
  407. $zipFile->saveAsFile($this->outputFilename);
  408. $zipFile->close();
  409. static::assertCorrectZipArchive($this->outputFilename);
  410. $zipFile->openFile($this->outputFilename);
  411. static::assertFalse(isset($zipFile[$oldName]));
  412. static::assertTrue(isset($zipFile[$newName]));
  413. static::assertFalse(isset($zipFile['file1.txt']));
  414. static::assertFalse(isset($zipFile['file2.txt']));
  415. static::assertFalse(isset($zipFile['file3.txt']));
  416. static::assertTrue(isset($zipFile['file_long_name.txt']));
  417. static::assertTrue(isset($zipFile['file4.txt']));
  418. static::assertTrue(isset($zipFile['fi.txt']));
  419. $zipFile->close();
  420. }
  421. /**
  422. * @throws ZipException
  423. */
  424. public function testRenameEntryNull()
  425. {
  426. $this->setExpectedException(InvalidArgumentException::class, 'name is null');
  427. $zipFile = new ZipFile();
  428. $zipFile->rename(null, 'new-file');
  429. }
  430. /**
  431. * @throws ZipException
  432. */
  433. public function testRenameEntryNull2()
  434. {
  435. $this->setExpectedException(InvalidArgumentException::class, 'name is null');
  436. $zipFile = new ZipFile();
  437. $zipFile->rename('old-file', null);
  438. }
  439. /**
  440. * @throws ZipException
  441. */
  442. public function testRenameEntryToExistsNewEntry()
  443. {
  444. $this->setExpectedException(InvalidArgumentException::class, 'is exists');
  445. $zipFile = new ZipFile();
  446. $zipFile['file'] = 'content';
  447. $zipFile['file2'] = 'content 2';
  448. $zipFile->saveAsFile($this->outputFilename);
  449. $zipFile->close();
  450. $zipFile = new ZipFile();
  451. $zipFile->openFile($this->outputFilename);
  452. $zipFile->rename('file2', 'file');
  453. }
  454. /**
  455. * @throws ZipException
  456. */
  457. public function testRenameEntryNotFound()
  458. {
  459. $this->setExpectedException(ZipEntryNotFoundException::class);
  460. $zipFile = new ZipFile();
  461. $zipFile['file'] = 'content';
  462. $zipFile['file2'] = 'content 2';
  463. $zipFile->saveAsFile($this->outputFilename);
  464. $zipFile->close();
  465. $zipFile = new ZipFile();
  466. $zipFile->openFile($this->outputFilename);
  467. $zipFile->rename('file2.bak', 'file3');
  468. }
  469. /**
  470. * Delete entry from name.
  471. *
  472. * @throws ZipException
  473. */
  474. public function testDeleteFromName()
  475. {
  476. $inputDir = \dirname(\dirname(__DIR__)) . \DIRECTORY_SEPARATOR;
  477. $deleteEntryName = 'composer.json';
  478. $zipFile = new ZipFile();
  479. $zipFile->addDir($inputDir);
  480. $zipFile->saveAsFile($this->outputFilename);
  481. $zipFile->close();
  482. static::assertCorrectZipArchive($this->outputFilename);
  483. $zipFile->openFile($this->outputFilename);
  484. $zipFile->deleteFromName($deleteEntryName);
  485. $zipFile->saveAsFile($this->outputFilename);
  486. $zipFile->close();
  487. static::assertCorrectZipArchive($this->outputFilename);
  488. $zipFile->openFile($this->outputFilename);
  489. static::assertFalse(isset($zipFile[$deleteEntryName]));
  490. $zipFile->close();
  491. }
  492. /**
  493. * @throws Exception\ZipEntryNotFoundException
  494. * @throws ZipException
  495. */
  496. public function testDeleteNewEntry()
  497. {
  498. $zipFile = new ZipFile();
  499. $zipFile['entry1'] = '';
  500. $zipFile['entry2'] = '';
  501. $zipFile->deleteFromName('entry2');
  502. $zipFile->saveAsFile($this->outputFilename);
  503. $zipFile->close();
  504. $zipFile->openFile($this->outputFilename);
  505. static::assertSame(\count($zipFile), 1);
  506. static::assertTrue(isset($zipFile['entry1']));
  507. static::assertFalse(isset($zipFile['entry2']));
  508. $zipFile->close();
  509. }
  510. /**
  511. * @throws ZipEntryNotFoundException
  512. */
  513. public function testDeleteFromNameNotFoundEntry()
  514. {
  515. $this->setExpectedException(ZipEntryNotFoundException::class);
  516. $zipFile = new ZipFile();
  517. $zipFile->deleteFromName('entry');
  518. }
  519. /**
  520. * Delete zip entries from glob pattern.
  521. *
  522. * @throws ZipException
  523. */
  524. public function testDeleteFromGlob()
  525. {
  526. $inputDir = \dirname(\dirname(__DIR__));
  527. $zipFile = new ZipFile();
  528. $zipFile->addFilesFromGlobRecursive($inputDir, '**.{xml,json,md}', '/');
  529. static::assertTrue(isset($zipFile['composer.json']));
  530. static::assertTrue(isset($zipFile['phpunit.xml']));
  531. $zipFile->saveAsFile($this->outputFilename);
  532. $zipFile->close();
  533. static::assertCorrectZipArchive($this->outputFilename);
  534. $zipFile->openFile($this->outputFilename);
  535. static::assertTrue(isset($zipFile['composer.json']));
  536. static::assertTrue(isset($zipFile['phpunit.xml']));
  537. $zipFile->deleteFromGlob('**.{xml,json}');
  538. static::assertFalse(isset($zipFile['composer.json']));
  539. static::assertFalse(isset($zipFile['phpunit.xml']));
  540. $zipFile->saveAsFile($this->outputFilename);
  541. $zipFile->close();
  542. static::assertCorrectZipArchive($this->outputFilename);
  543. $zipFile->openFile($this->outputFilename);
  544. static::assertTrue($zipFile->count() > 0);
  545. foreach ($zipFile->getListFiles() as $name) {
  546. static::assertStringEndsWith('.md', $name);
  547. }
  548. $zipFile->close();
  549. }
  550. public function testDeleteFromGlobFailNull()
  551. {
  552. $this->setExpectedException(InvalidArgumentException::class, 'The glob pattern is not specified');
  553. $zipFile = new ZipFile();
  554. $zipFile->deleteFromGlob(null);
  555. }
  556. public function testDeleteFromGlobFailEmpty()
  557. {
  558. $this->setExpectedException(InvalidArgumentException::class, 'The glob pattern is not specified');
  559. $zipFile = new ZipFile();
  560. $zipFile->deleteFromGlob('');
  561. }
  562. /**
  563. * Delete entries from regex pattern.
  564. *
  565. * @throws ZipException
  566. */
  567. public function testDeleteFromRegex()
  568. {
  569. $inputDir = \dirname(\dirname(__DIR__));
  570. $zipFile = new ZipFile();
  571. $zipFile->addFilesFromRegexRecursive($inputDir, '~\.(xml|json)$~i', 'Path');
  572. $zipFile->saveAsFile($this->outputFilename);
  573. $zipFile->close();
  574. static::assertCorrectZipArchive($this->outputFilename);
  575. $zipFile->openFile($this->outputFilename);
  576. $zipFile->deleteFromRegex('~\.(json)$~i');
  577. $zipFile->addFromString('test.txt', 'content');
  578. $zipFile->deleteFromRegex('~\.txt$~');
  579. $zipFile->saveAsFile($this->outputFilename);
  580. $zipFile->close();
  581. static::assertCorrectZipArchive($this->outputFilename);
  582. $zipFile->openFile($this->outputFilename);
  583. static::assertFalse(isset($zipFile['Path/composer.json']));
  584. static::assertFalse(isset($zipFile['Path/test.txt']));
  585. static::assertTrue(isset($zipFile['Path/phpunit.xml']));
  586. $zipFile->close();
  587. }
  588. public function testDeleteFromRegexFailNull()
  589. {
  590. $this->setExpectedException(InvalidArgumentException::class, 'The regex pattern is not specified');
  591. $zipFile = new ZipFile();
  592. $zipFile->deleteFromRegex(null);
  593. }
  594. public function testDeleteFromRegexFailEmpty()
  595. {
  596. $this->setExpectedException(InvalidArgumentException::class, 'The regex pattern is not specified');
  597. $zipFile = new ZipFile();
  598. $zipFile->deleteFromRegex('');
  599. }
  600. /**
  601. * Delete all entries.
  602. *
  603. * @throws ZipException
  604. */
  605. public function testDeleteAll()
  606. {
  607. $zipFile = new ZipFile();
  608. $zipFile->addDirRecursive(\dirname(\dirname(__DIR__)) . \DIRECTORY_SEPARATOR . 'src');
  609. static::assertTrue($zipFile->count() > 0);
  610. $zipFile->saveAsFile($this->outputFilename);
  611. $zipFile->close();
  612. static::assertCorrectZipArchive($this->outputFilename);
  613. $zipFile->openFile($this->outputFilename);
  614. static::assertTrue($zipFile->count() > 0);
  615. $zipFile->deleteAll();
  616. $zipFile->saveAsFile($this->outputFilename);
  617. $zipFile->close();
  618. static::assertCorrectEmptyZip($this->outputFilename);
  619. $zipFile->openFile($this->outputFilename);
  620. static::assertSame($zipFile->count(), 0);
  621. $zipFile->close();
  622. }
  623. /**
  624. * Test zip archive comment.
  625. *
  626. * @throws ZipException
  627. */
  628. public function testArchiveComment()
  629. {
  630. $comment = 'This zip file comment' . \PHP_EOL
  631. . 'Αυτό το σχόλιο αρχείο zip' . \PHP_EOL
  632. . 'Это комментарий zip архива' . \PHP_EOL
  633. . '這個ZIP文件註釋' . \PHP_EOL
  634. . 'ეს zip ფაილის კომენტარი' . \PHP_EOL
  635. . 'このzipファイルにコメント' . \PHP_EOL
  636. . 'ความคิดเห็นนี้ไฟล์ซิป';
  637. $zipFile = new ZipFile();
  638. $zipFile->setArchiveComment($comment);
  639. $zipFile->addFile(__FILE__);
  640. $zipFile->saveAsFile($this->outputFilename);
  641. $zipFile->close();
  642. static::assertCorrectZipArchive($this->outputFilename);
  643. $zipFile->openFile($this->outputFilename);
  644. static::assertSame($zipFile->getArchiveComment(), $comment);
  645. $zipFile->setArchiveComment(null); // remove archive comment
  646. $zipFile->saveAsFile($this->outputFilename);
  647. $zipFile->close();
  648. static::assertCorrectZipArchive($this->outputFilename);
  649. // check empty comment
  650. $zipFile->openFile($this->outputFilename);
  651. static::assertNull($zipFile->getArchiveComment());
  652. $zipFile->close();
  653. }
  654. /**
  655. * Test very long archive comment.
  656. */
  657. public function testVeryLongArchiveComment()
  658. {
  659. $this->setExpectedException(InvalidArgumentException::class);
  660. $comment = 'Very long comment' . \PHP_EOL .
  661. 'Очень длинный комментарий' . \PHP_EOL;
  662. $comment = str_repeat($comment, ceil(0xffff / \strlen($comment)) + \strlen($comment) + 1);
  663. $zipFile = new ZipFile();
  664. $zipFile->setArchiveComment($comment);
  665. }
  666. /**
  667. * Test zip entry comment.
  668. *
  669. * @throws ZipException
  670. */
  671. public function testEntryComment()
  672. {
  673. $entries = [
  674. '文件1.txt' => [
  675. 'data' => CryptoUtil::randomBytes(255),
  676. 'comment' => '這是註釋的條目。',
  677. ],
  678. 'file2.txt' => [
  679. 'data' => CryptoUtil::randomBytes(255),
  680. 'comment' => null,
  681. ],
  682. 'file3.txt' => [
  683. 'data' => CryptoUtil::randomBytes(255),
  684. 'comment' => CryptoUtil::randomBytes(255),
  685. ],
  686. 'file4.txt' => [
  687. 'data' => CryptoUtil::randomBytes(255),
  688. 'comment' => 'Комментарий файла',
  689. ],
  690. 'file5.txt' => [
  691. 'data' => CryptoUtil::randomBytes(255),
  692. 'comment' => 'ไฟล์แสดงความคิดเห็น',
  693. ],
  694. 'file6 emoji 🙍🏼.txt' => [
  695. 'data' => CryptoUtil::randomBytes(255),
  696. 'comment' => 'Emoji comment file - 😀 ⛈ ❤️ 🤴🏽',
  697. ],
  698. ];
  699. // create archive with entry comments
  700. $zipFile = new ZipFile();
  701. foreach ($entries as $entryName => $item) {
  702. $zipFile->addFromString($entryName, $item['data']);
  703. $zipFile->setEntryComment($entryName, $item['comment']);
  704. }
  705. $zipFile->saveAsFile($this->outputFilename);
  706. $zipFile->close();
  707. static::assertCorrectZipArchive($this->outputFilename);
  708. // check and modify comments
  709. $zipFile->openFile($this->outputFilename);
  710. foreach ($zipFile->getListFiles() as $entryName) {
  711. $entriesItem = $entries[$entryName];
  712. static::assertNotEmpty($entriesItem);
  713. static::assertSame($zipFile[$entryName], $entriesItem['data']);
  714. static::assertSame($zipFile->getEntryComment($entryName), (string) $entriesItem['comment']);
  715. }
  716. // modify comment
  717. $entries['file5.txt']['comment'] = mt_rand(1, 100000000);
  718. $zipFile->setEntryComment('file5.txt', $entries['file5.txt']['comment']);
  719. $zipFile->saveAsFile($this->outputFilename);
  720. $zipFile->close();
  721. static::assertCorrectZipArchive($this->outputFilename);
  722. // check modify comments
  723. $zipFile->openFile($this->outputFilename);
  724. foreach ($entries as $entryName => $entriesItem) {
  725. static::assertTrue(isset($zipFile[$entryName]));
  726. static::assertSame($zipFile->getEntryComment($entryName), (string) $entriesItem['comment']);
  727. static::assertSame($zipFile[$entryName], $entriesItem['data']);
  728. }
  729. $zipFile->close();
  730. }
  731. /**
  732. * Test zip entry very long comment.
  733. *
  734. * @throws ZipException
  735. */
  736. public function testVeryLongEntryComment()
  737. {
  738. $this->setExpectedException(ZipException::class, 'Comment too long');
  739. $comment = 'Very long comment' . \PHP_EOL .
  740. 'Очень длинный комментарий' . \PHP_EOL;
  741. $comment = str_repeat($comment, ceil(0xffff / \strlen($comment)) + \strlen($comment) + 1);
  742. $zipFile = new ZipFile();
  743. $zipFile->addFile(__FILE__, 'test');
  744. $zipFile->setEntryComment('test', $comment);
  745. }
  746. /**
  747. * @throws ZipException
  748. */
  749. public function testSetEntryCommentNotFoundEntry()
  750. {
  751. $this->setExpectedException(ZipEntryNotFoundException::class);
  752. $zipFile = new ZipFile();
  753. $zipFile->setEntryComment('test', 'comment');
  754. }
  755. /**
  756. * Test all available support compression methods.
  757. *
  758. * @throws ZipException
  759. */
  760. public function testCompressionMethod()
  761. {
  762. $entries = [
  763. '1' => [
  764. 'data' => CryptoUtil::randomBytes(255),
  765. 'method' => ZipFileInterface::METHOD_STORED,
  766. 'expected' => 'No compression',
  767. ],
  768. '2' => [
  769. 'data' => CryptoUtil::randomBytes(255),
  770. 'method' => ZipFileInterface::METHOD_DEFLATED,
  771. 'expected' => 'Deflate',
  772. ],
  773. ];
  774. if (\extension_loaded('bz2')) {
  775. $entries['3'] = [
  776. 'data' => CryptoUtil::randomBytes(255),
  777. 'method' => ZipFileInterface::METHOD_BZIP2,
  778. 'expected' => 'Bzip2',
  779. ];
  780. }
  781. $zipFile = new ZipFile();
  782. foreach ($entries as $entryName => $item) {
  783. $zipFile->addFromString($entryName, $item['data'], $item['method']);
  784. }
  785. $zipFile->saveAsFile($this->outputFilename);
  786. $zipFile->close();
  787. static::assertCorrectZipArchive($this->outputFilename);
  788. $zipFile->openFile($this->outputFilename);
  789. $zipFile->setCompressionLevel(ZipFileInterface::LEVEL_BEST_COMPRESSION);
  790. $zipAllInfo = $zipFile->getAllInfo();
  791. foreach ($zipAllInfo as $entryName => $info) {
  792. static::assertSame($zipFile[$entryName], $entries[$entryName]['data']);
  793. static::assertSame($info->getMethodName(), $entries[$entryName]['expected']);
  794. $entryInfo = $zipFile->getEntryInfo($entryName);
  795. static::assertEquals($entryInfo, $info);
  796. }
  797. $zipFile->close();
  798. }
  799. public function testSetInvalidCompressionLevel()
  800. {
  801. $this->setExpectedException(
  802. InvalidArgumentException::class,
  803. 'Invalid compression level. Minimum level -1. Maximum level 9'
  804. );
  805. $zipFile = new ZipFile();
  806. $zipFile->setCompressionLevel(-2);
  807. }
  808. public function testSetInvalidCompressionLevel2()
  809. {
  810. $this->setExpectedException(
  811. InvalidArgumentException::class,
  812. 'Invalid compression level. Minimum level -1. Maximum level 9'
  813. );
  814. $zipFile = new ZipFile();
  815. $zipFile->setCompressionLevel(10);
  816. }
  817. /**
  818. * Test extract all files.
  819. *
  820. * @throws ZipException
  821. */
  822. public function testExtract()
  823. {
  824. $entries = [
  825. 'test1.txt' => CryptoUtil::randomBytes(255),
  826. 'test2.txt' => CryptoUtil::randomBytes(255),
  827. 'test/test 2/test3.txt' => CryptoUtil::randomBytes(255),
  828. 'test empty/dir' => null,
  829. ];
  830. $zipFile = new ZipFile();
  831. foreach ($entries as $entryName => $value) {
  832. if ($value === null) {
  833. $zipFile->addEmptyDir($entryName);
  834. } else {
  835. $zipFile->addFromString($entryName, $value);
  836. }
  837. }
  838. $zipFile->saveAsFile($this->outputFilename);
  839. $zipFile->close();
  840. static::assertTrue(mkdir($this->outputDirname, 0755, true));
  841. $zipFile->openFile($this->outputFilename);
  842. $zipFile->extractTo($this->outputDirname);
  843. foreach ($entries as $entryName => $value) {
  844. $fullExtractedFilename = $this->outputDirname . \DIRECTORY_SEPARATOR . $entryName;
  845. if ($value === null) {
  846. static::assertTrue(is_dir($fullExtractedFilename));
  847. static::assertTrue(FilesUtil::isEmptyDir($fullExtractedFilename));
  848. } else {
  849. static::assertTrue(is_file($fullExtractedFilename));
  850. $contents = file_get_contents($fullExtractedFilename);
  851. static::assertSame($contents, $value);
  852. }
  853. }
  854. $zipFile->close();
  855. }
  856. /**
  857. * Test extract some files.
  858. *
  859. * @throws ZipException
  860. */
  861. public function testExtractSomeFiles()
  862. {
  863. $entries = [
  864. 'test1.txt' => CryptoUtil::randomBytes(255),
  865. 'test2.txt' => CryptoUtil::randomBytes(255),
  866. 'test3.txt' => CryptoUtil::randomBytes(255),
  867. 'test4.txt' => CryptoUtil::randomBytes(255),
  868. 'test5.txt' => CryptoUtil::randomBytes(255),
  869. 'test/test/test.txt' => CryptoUtil::randomBytes(255),
  870. 'test/test/test 2.txt' => CryptoUtil::randomBytes(255),
  871. 'test empty/dir/' => null,
  872. 'test empty/dir2/' => null,
  873. ];
  874. $extractEntries = [
  875. 'test1.txt',
  876. 'test3.txt',
  877. 'test5.txt',
  878. 'test/test/test 2.txt',
  879. 'test empty/dir2/',
  880. ];
  881. static::assertTrue(mkdir($this->outputDirname, 0755, true));
  882. $zipFile = new ZipFile();
  883. $zipFile->addAll($entries);
  884. $zipFile->saveAsFile($this->outputFilename);
  885. $zipFile->close();
  886. $zipFile->openFile($this->outputFilename);
  887. $zipFile->extractTo($this->outputDirname, $extractEntries);
  888. foreach ($entries as $entryName => $value) {
  889. $fullExtractFilename = $this->outputDirname . \DIRECTORY_SEPARATOR . $entryName;
  890. if (\in_array($entryName, $extractEntries, true)) {
  891. if ($value === null) {
  892. static::assertTrue(is_dir($fullExtractFilename));
  893. static::assertTrue(FilesUtil::isEmptyDir($fullExtractFilename));
  894. } else {
  895. static::assertTrue(is_file($fullExtractFilename));
  896. $contents = file_get_contents($fullExtractFilename);
  897. static::assertEquals($contents, $value);
  898. }
  899. } elseif ($value === null) {
  900. static::assertFalse(is_dir($fullExtractFilename));
  901. } else {
  902. static::assertFalse(is_file($fullExtractFilename));
  903. }
  904. }
  905. static::assertFalse(is_file($this->outputDirname . \DIRECTORY_SEPARATOR . 'test/test/test.txt'));
  906. $zipFile->extractTo($this->outputDirname, 'test/test/test.txt');
  907. static::assertTrue(is_file($this->outputDirname . \DIRECTORY_SEPARATOR . 'test/test/test.txt'));
  908. $zipFile->close();
  909. }
  910. /**
  911. * @throws ZipException
  912. */
  913. public function testExtractFail()
  914. {
  915. $this->setExpectedException(ZipException::class, 'not found');
  916. $zipFile = new ZipFile();
  917. $zipFile['file'] = 'content';
  918. $zipFile->saveAsFile($this->outputFilename);
  919. $zipFile->close();
  920. $zipFile->openFile($this->outputFilename);
  921. $zipFile->extractTo('path/to/path');
  922. }
  923. /**
  924. * @throws ZipException
  925. */
  926. public function testExtractFail2()
  927. {
  928. $this->setExpectedException(ZipException::class, 'Destination is not directory');
  929. $zipFile = new ZipFile();
  930. $zipFile['file'] = 'content';
  931. $zipFile->saveAsFile($this->outputFilename);
  932. $zipFile->close();
  933. $zipFile->openFile($this->outputFilename);
  934. $zipFile->extractTo($this->outputFilename);
  935. }
  936. /**
  937. * @throws ZipException
  938. */
  939. public function testExtractFail3()
  940. {
  941. $this->setExpectedException(ZipException::class, 'Destination is not writable directory');
  942. /** @noinspection PhpComposerExtensionStubsInspection */
  943. if (posix_getuid() === 0) {
  944. static::markTestSkipped('Skip the test for a user with root privileges');
  945. }
  946. $zipFile = new ZipFile();
  947. $zipFile['file'] = 'content';
  948. $zipFile->saveAsFile($this->outputFilename);
  949. $zipFile->close();
  950. static::assertTrue(mkdir($this->outputDirname, 0444, true));
  951. static::assertTrue(chmod($this->outputDirname, 0444));
  952. $zipFile->openFile($this->outputFilename);
  953. $zipFile->extractTo($this->outputDirname);
  954. }
  955. /**
  956. * @noinspection OnlyWritesOnParameterInspection
  957. */
  958. public function testAddFromArrayAccessNullName()
  959. {
  960. $this->setExpectedException(InvalidArgumentException::class, 'entryName is null');
  961. $zipFile = new ZipFile();
  962. $zipFile[null] = 'content';
  963. }
  964. /**
  965. * @noinspection OnlyWritesOnParameterInspection
  966. */
  967. public function testAddFromArrayAccessEmptyName()
  968. {
  969. $this->setExpectedException(InvalidArgumentException::class, 'entryName is empty');
  970. $zipFile = new ZipFile();
  971. $zipFile[''] = 'content';
  972. }
  973. /**
  974. * @throws ZipException
  975. */
  976. public function testAddFromStringNullContents()
  977. {
  978. $this->setExpectedException(InvalidArgumentException::class, 'Contents is null');
  979. $zipFile = new ZipFile();
  980. $zipFile->addFromString('file', null);
  981. }
  982. /**
  983. * @throws ZipException
  984. */
  985. public function testAddFromStringNullEntryName()
  986. {
  987. $this->setExpectedException(InvalidArgumentException::class, 'Entry name is null');
  988. $zipFile = new ZipFile();
  989. $zipFile->addFromString(null, 'contents');
  990. }
  991. /**
  992. * @throws ZipException
  993. */
  994. public function testAddFromStringUnsupportedMethod()
  995. {
  996. $this->setExpectedException(ZipUnsupportMethodException::class, 'Unsupported compression method');
  997. $zipFile = new ZipFile();
  998. $zipFile->addFromString('file', 'contents', ZipEntry::METHOD_WINZIP_AES);
  999. }
  1000. /**
  1001. * @throws ZipException
  1002. */
  1003. public function testAddFromStringEmptyEntryName()
  1004. {
  1005. $this->setExpectedException(InvalidArgumentException::class, 'Empty entry name');
  1006. $zipFile = new ZipFile();
  1007. $zipFile->addFromString('', 'contents');
  1008. }
  1009. /**
  1010. * Test compression method from add string.
  1011. *
  1012. * @throws ZipException
  1013. */
  1014. public function testAddFromStringCompressionMethod()
  1015. {
  1016. $fileStored = sys_get_temp_dir() . '/zip-stored.txt';
  1017. $fileDeflated = sys_get_temp_dir() . '/zip-deflated.txt';
  1018. static::assertNotFalse(file_put_contents($fileStored, 'content'));
  1019. static::assertNotFalse(file_put_contents($fileDeflated, str_repeat('content', 200)));
  1020. $zipFile = new ZipFile();
  1021. $zipFile->addFromString(basename($fileStored), file_get_contents($fileStored));
  1022. $zipFile->addFromString(basename($fileDeflated), file_get_contents($fileDeflated));
  1023. $zipFile->saveAsFile($this->outputFilename);
  1024. $zipFile->close();
  1025. unlink($fileStored);
  1026. unlink($fileDeflated);
  1027. $zipFile->openFile($this->outputFilename);
  1028. $infoStored = $zipFile->getEntryInfo(basename($fileStored));
  1029. $infoDeflated = $zipFile->getEntryInfo(basename($fileDeflated));
  1030. static::assertSame($infoStored->getMethodName(), 'No compression');
  1031. static::assertSame($infoDeflated->getMethodName(), 'Deflate');
  1032. $zipFile->close();
  1033. }
  1034. /**
  1035. * @throws ZipException
  1036. */
  1037. public function testAddFromStreamInvalidResource()
  1038. {
  1039. $this->setExpectedException(InvalidArgumentException::class, 'Stream is not resource');
  1040. $zipFile = new ZipFile();
  1041. /** @noinspection PhpParamsInspection */
  1042. $zipFile->addFromStream('invalid resource', 'name');
  1043. }
  1044. /**
  1045. * @throws ZipException
  1046. */
  1047. public function testAddFromStreamEmptyEntryName()
  1048. {
  1049. $this->setExpectedException(InvalidArgumentException::class, 'Empty entry name');
  1050. $handle = fopen(__FILE__, 'rb');
  1051. $zipFile = new ZipFile();
  1052. $zipFile->addFromStream($handle, '');
  1053. }
  1054. /**
  1055. * @throws ZipException
  1056. */
  1057. public function testAddFromStreamUnsupportedMethod()
  1058. {
  1059. $this->setExpectedException(ZipUnsupportMethodException::class, 'Unsupported method');
  1060. $handle = fopen(__FILE__, 'rb');
  1061. $zipFile = new ZipFile();
  1062. $zipFile->addFromStream($handle, basename(__FILE__), ZipEntry::METHOD_WINZIP_AES);
  1063. }
  1064. /**
  1065. * Test compression method from add stream.
  1066. *
  1067. * @throws ZipException
  1068. */
  1069. public function testAddFromStreamCompressionMethod()
  1070. {
  1071. $fileStored = sys_get_temp_dir() . '/zip-stored.txt';
  1072. $fileDeflated = sys_get_temp_dir() . '/zip-deflated.txt';
  1073. static::assertNotFalse(file_put_contents($fileStored, 'content'));
  1074. static::assertNotFalse(file_put_contents($fileDeflated, str_repeat('content', 200)));
  1075. $fpStored = fopen($fileStored, 'rb');
  1076. $fpDeflated = fopen($fileDeflated, 'rb');
  1077. $zipFile = new ZipFile();
  1078. $zipFile->addFromStream($fpStored, basename($fileStored));
  1079. $zipFile->addFromStream($fpDeflated, basename($fileDeflated));
  1080. $zipFile->saveAsFile($this->outputFilename);
  1081. $zipFile->close();
  1082. unlink($fileStored);
  1083. unlink($fileDeflated);
  1084. $zipFile->openFile($this->outputFilename);
  1085. $infoStored = $zipFile->getEntryInfo(basename($fileStored));
  1086. $infoDeflated = $zipFile->getEntryInfo(basename($fileDeflated));
  1087. static::assertSame($infoStored->getMethodName(), 'No compression');
  1088. static::assertSame($infoDeflated->getMethodName(), 'Deflate');
  1089. $zipFile->close();
  1090. }
  1091. /**
  1092. * @throws ZipException
  1093. */
  1094. public function testAddFileNullFileName()
  1095. {
  1096. $this->setExpectedException(InvalidArgumentException::class, 'file is null');
  1097. $zipFile = new ZipFile();
  1098. $zipFile->addFile(null);
  1099. }
  1100. /**
  1101. * @throws ZipException
  1102. */
  1103. public function testAddFileCantExists()
  1104. {
  1105. $this->setExpectedException(ZipException::class, 'does not exist');
  1106. $zipFile = new ZipFile();
  1107. $zipFile->addFile('path/to/file');
  1108. }
  1109. /**
  1110. * @throws ZipException
  1111. */
  1112. public function testAddFileUnsupportedMethod()
  1113. {
  1114. $this->setExpectedException(ZipUnsupportMethodException::class, 'Unsupported compression method 99');
  1115. $zipFile = new ZipFile();
  1116. $zipFile->addFile(__FILE__, null, ZipEntry::METHOD_WINZIP_AES);
  1117. }
  1118. /**
  1119. * @throws ZipException
  1120. */
  1121. public function testAddFileCantOpen()
  1122. {
  1123. $this->setExpectedException(ZipException::class, 'file could not be read');
  1124. /** @noinspection PhpComposerExtensionStubsInspection */
  1125. if (posix_getuid() === 0) {
  1126. static::markTestSkipped('Skip the test for a user with root privileges');
  1127. }
  1128. static::assertNotFalse(file_put_contents($this->outputFilename, ''));
  1129. static::assertTrue(chmod($this->outputFilename, 0244));
  1130. $zipFile = new ZipFile();
  1131. $zipFile->addFile($this->outputFilename);
  1132. }
  1133. /**
  1134. * @throws ZipException
  1135. */
  1136. public function testAddDirNullDirname()
  1137. {
  1138. $this->setExpectedException(InvalidArgumentException::class, 'Input dir is null');
  1139. $zipFile = new ZipFile();
  1140. $zipFile->addDir(null);
  1141. }
  1142. /**
  1143. * @throws ZipException
  1144. */
  1145. public function testAddDirEmptyDirname()
  1146. {
  1147. $this->setExpectedException(InvalidArgumentException::class, 'The input directory is not specified');
  1148. $zipFile = new ZipFile();
  1149. $zipFile->addDir('');
  1150. }
  1151. /**
  1152. * @throws ZipException
  1153. */
  1154. public function testAddDirCantExists()
  1155. {
  1156. $this->setExpectedException(InvalidArgumentException::class, 'does not exist');
  1157. $zipFile = new ZipFile();
  1158. $zipFile->addDir(uniqid('', true));
  1159. }
  1160. /**
  1161. * @throws ZipException
  1162. */
  1163. public function testAddDirRecursiveNullDirname()
  1164. {
  1165. $this->setExpectedException(InvalidArgumentException::class, 'Input dir is null');
  1166. $zipFile = new ZipFile();
  1167. $zipFile->addDirRecursive(null);
  1168. }
  1169. /**
  1170. * @throws ZipException
  1171. */
  1172. public function testAddDirRecursiveEmptyDirname()
  1173. {
  1174. $this->setExpectedException(InvalidArgumentException::class, 'The input directory is not specified');
  1175. $zipFile = new ZipFile();
  1176. $zipFile->addDirRecursive('');
  1177. }
  1178. /**
  1179. * @throws ZipException
  1180. */
  1181. public function testAddDirRecursiveCantExists()
  1182. {
  1183. $this->setExpectedException(InvalidArgumentException::class, 'does not exist');
  1184. $zipFile = new ZipFile();
  1185. $zipFile->addDirRecursive(uniqid('', true));
  1186. }
  1187. /**
  1188. * @throws ZipException
  1189. */
  1190. public function testAddFilesFromGlobNull()
  1191. {
  1192. $this->setExpectedException(InvalidArgumentException::class, 'Input dir is null');
  1193. $zipFile = new ZipFile();
  1194. $zipFile->addFilesFromGlob(null, '*.png');
  1195. }
  1196. /**
  1197. * @throws ZipException
  1198. */
  1199. public function testAddFilesFromGlobEmpty()
  1200. {
  1201. $this->setExpectedException(InvalidArgumentException::class, 'The input directory is not specified');
  1202. $zipFile = new ZipFile();
  1203. $zipFile->addFilesFromGlob('', '*.png');
  1204. }
  1205. /**
  1206. * @throws ZipException
  1207. */
  1208. public function testAddFilesFromGlobCantExists()
  1209. {
  1210. $this->setExpectedException(InvalidArgumentException::class, 'does not exist');
  1211. $zipFile = new ZipFile();
  1212. $zipFile->addFilesFromGlob('path/to/path', '*.png');
  1213. }
  1214. /**
  1215. * @throws ZipException
  1216. */
  1217. public function testAddFilesFromGlobNullPattern()
  1218. {
  1219. $this->setExpectedException(InvalidArgumentException::class, 'The glob pattern is not specified');
  1220. $zipFile = new ZipFile();
  1221. $zipFile->addFilesFromGlob(__DIR__, null);
  1222. }
  1223. /**
  1224. * @throws ZipException
  1225. */
  1226. public function testAddFilesFromGlobEmptyPattern()
  1227. {
  1228. $this->setExpectedException(InvalidArgumentException::class, 'The glob pattern is not specified');
  1229. $zipFile = new ZipFile();
  1230. $zipFile->addFilesFromGlob(__DIR__, '');
  1231. }
  1232. /**
  1233. * @throws ZipException
  1234. */
  1235. public function testAddFilesFromGlobRecursiveNull()
  1236. {
  1237. $this->setExpectedException(InvalidArgumentException::class, 'Input dir is null');
  1238. $zipFile = new ZipFile();
  1239. $zipFile->addFilesFromGlobRecursive(null, '*.png');
  1240. }
  1241. /**
  1242. * @throws ZipException
  1243. */
  1244. public function testAddFilesFromGlobRecursiveEmpty()
  1245. {
  1246. $this->setExpectedException(InvalidArgumentException::class, 'The input directory is not specified');
  1247. $zipFile = new ZipFile();
  1248. $zipFile->addFilesFromGlobRecursive('', '*.png');
  1249. }
  1250. /**
  1251. * @throws ZipException
  1252. */
  1253. public function testAddFilesFromGlobRecursiveCantExists()
  1254. {
  1255. $this->setExpectedException(InvalidArgumentException::class, 'does not exist');
  1256. $zipFile = new ZipFile();
  1257. $zipFile->addFilesFromGlobRecursive('path/to/path', '*.png');
  1258. }
  1259. /**
  1260. * @throws ZipException
  1261. */
  1262. public function testAddFilesFromGlobRecursiveNullPattern()
  1263. {
  1264. $this->setExpectedException(InvalidArgumentException::class, 'The glob pattern is not specified');
  1265. $zipFile = new ZipFile();
  1266. $zipFile->addFilesFromGlobRecursive(__DIR__, null);
  1267. }
  1268. /**
  1269. * @throws ZipException
  1270. */
  1271. public function testAddFilesFromGlobRecursiveEmptyPattern()
  1272. {
  1273. $this->setExpectedException(InvalidArgumentException::class, 'The glob pattern is not specified');
  1274. $zipFile = new ZipFile();
  1275. $zipFile->addFilesFromGlobRecursive(__DIR__, '');
  1276. }
  1277. /**
  1278. * @throws ZipException
  1279. */
  1280. public function testAddFilesFromRegexDirectoryNull()
  1281. {
  1282. $this->setExpectedException(InvalidArgumentException::class, 'The input directory is not specified');
  1283. $zipFile = new ZipFile();
  1284. $zipFile->addFilesFromRegex(null, '~\.png$~i');
  1285. }
  1286. /**
  1287. * @throws ZipException
  1288. */
  1289. public function testAddFilesFromRegexDirectoryEmpty()
  1290. {
  1291. $this->setExpectedException(InvalidArgumentException::class, 'The input directory is not specified');
  1292. $zipFile = new ZipFile();
  1293. $zipFile->addFilesFromRegex('', '~\.png$~i');
  1294. }
  1295. /**
  1296. * @throws ZipException
  1297. */
  1298. public function testAddFilesFromRegexCantExists()
  1299. {
  1300. $this->setExpectedException(InvalidArgumentException::class, 'does not exist');
  1301. $zipFile = new ZipFile();
  1302. $zipFile->addFilesFromRegex('path/to/path', '~\.png$~i');
  1303. }
  1304. /**
  1305. * @throws ZipException
  1306. */
  1307. public function testAddFilesFromRegexNullPattern()
  1308. {
  1309. $this->setExpectedException(InvalidArgumentException::class, 'The regex pattern is not specified');
  1310. $zipFile = new ZipFile();
  1311. $zipFile->addFilesFromRegex(__DIR__, null);
  1312. }
  1313. /**
  1314. * @throws ZipException
  1315. */
  1316. public function testAddFilesFromRegexEmptyPattern()
  1317. {
  1318. $this->setExpectedException(InvalidArgumentException::class, 'The regex pattern is not specified');
  1319. $zipFile = new ZipFile();
  1320. $zipFile->addFilesFromRegex(__DIR__, '');
  1321. }
  1322. /**
  1323. * @throws ZipException
  1324. */
  1325. public function testAddFilesFromRegexRecursiveDirectoryNull()
  1326. {
  1327. $this->setExpectedException(InvalidArgumentException::class, 'The input directory is not specified');
  1328. $zipFile = new ZipFile();
  1329. $zipFile->addFilesFromRegexRecursive(null, '~\.png$~i');
  1330. }
  1331. /**
  1332. * @throws ZipException
  1333. */
  1334. public function testAddFilesFromRegexRecursiveEmpty()
  1335. {
  1336. $this->setExpectedException(InvalidArgumentException::class, 'The input directory is not specified');
  1337. $zipFile = new ZipFile();
  1338. $zipFile->addFilesFromRegexRecursive('', '~\.png$~i');
  1339. }
  1340. /**
  1341. * @throws ZipException
  1342. */
  1343. public function testAddFilesFromRegexRecursiveCantExists()
  1344. {
  1345. $this->setExpectedException(InvalidArgumentException::class, 'does not exist');
  1346. $zipFile = new ZipFile();
  1347. $zipFile->addFilesFromGlobRecursive('path/to/path', '~\.png$~i');
  1348. }
  1349. /**
  1350. * @throws ZipException
  1351. */
  1352. public function testAddFilesFromRegexRecursiveNullPattern()
  1353. {
  1354. $this->setExpectedException(InvalidArgumentException::class, 'The regex pattern is not specified');
  1355. $zipFile = new ZipFile();
  1356. $zipFile->addFilesFromRegexRecursive(__DIR__, null);
  1357. }
  1358. /**
  1359. * @throws ZipException
  1360. */
  1361. public function testAddFilesFromRegexRecursiveEmptyPattern()
  1362. {
  1363. $this->setExpectedException(InvalidArgumentException::class, 'The regex pattern is not specified');
  1364. $zipFile = new ZipFile();
  1365. $zipFile->addFilesFromRegexRecursive(__DIR__, '');
  1366. }
  1367. /**
  1368. * @throws ZipException
  1369. */
  1370. public function testSaveAsStreamBadStream()
  1371. {
  1372. $this->setExpectedException(InvalidArgumentException::class, 'handle is not resource');
  1373. $zipFile = new ZipFile();
  1374. /** @noinspection PhpParamsInspection */
  1375. $zipFile->saveAsStream('bad stream');
  1376. }
  1377. /**
  1378. * @throws ZipException
  1379. */
  1380. public function testSaveAsFileNotWritable()
  1381. {
  1382. $this->setExpectedException(InvalidArgumentException::class, 'can not open from write');
  1383. /** @noinspection PhpComposerExtensionStubsInspection */
  1384. if (posix_getuid() === 0) {
  1385. static::markTestSkipped('Skip the test for a user with root privileges');
  1386. }
  1387. static::assertTrue(mkdir($this->outputDirname, 0444, true));
  1388. static::assertTrue(chmod($this->outputDirname, 0444));
  1389. $this->outputFilename = $this->outputDirname . \DIRECTORY_SEPARATOR . basename($this->outputFilename);
  1390. $zipFile = new ZipFile();
  1391. $zipFile->saveAsFile($this->outputFilename);
  1392. }
  1393. /**
  1394. * Test `ZipFile` implemented \ArrayAccess, \Countable and |iterator.
  1395. *
  1396. * @throws ZipException
  1397. */
  1398. public function testZipFileArrayAccessAndCountableAndIterator()
  1399. {
  1400. $files = [];
  1401. $numFiles = mt_rand(20, 100);
  1402. for ($i = 0; $i < $numFiles; $i++) {
  1403. $files['file' . $i . '.txt'] = CryptoUtil::randomBytes(255);
  1404. }
  1405. $methods = [ZipFileInterface::METHOD_STORED, ZipFileInterface::METHOD_DEFLATED];
  1406. if (\extension_loaded('bz2')) {
  1407. $methods[] = ZipFileInterface::METHOD_BZIP2;
  1408. }
  1409. $zipFile = new ZipFile();
  1410. $zipFile->setCompressionLevel(ZipFileInterface::LEVEL_BEST_SPEED);
  1411. foreach ($files as $entryName => $content) {
  1412. $zipFile->addFromString($entryName, $content, $methods[array_rand($methods)]);
  1413. }
  1414. $zipFile->saveAsFile($this->outputFilename);
  1415. $zipFile->close();
  1416. static::assertCorrectZipArchive($this->outputFilename);
  1417. $zipFile->openFile($this->outputFilename);
  1418. // Test \Countable
  1419. static::assertSame($zipFile->count(), $numFiles);
  1420. static::assertSame(\count($zipFile), $numFiles);
  1421. // Test \ArrayAccess
  1422. reset($files);
  1423. foreach ($zipFile as $entryName => $content) {
  1424. static::assertSame($entryName, key($files));
  1425. static::assertSame($content, current($files));
  1426. next($files);
  1427. }
  1428. // Test \Iterator
  1429. reset($files);
  1430. $iterator = new \ArrayIterator($zipFile);
  1431. $iterator->rewind();
  1432. while ($iterator->valid()) {
  1433. $key = $iterator->key();
  1434. $value = $iterator->current();
  1435. static::assertSame($key, key($files));
  1436. static::assertSame($value, current($files));
  1437. next($files);
  1438. $iterator->next();
  1439. }
  1440. $zipFile->close();
  1441. $zipFile = new ZipFile();
  1442. $zipFile['file1.txt'] = 'content 1';
  1443. $zipFile['dir/file2.txt'] = 'content 1';
  1444. $zipFile['dir/empty dir/'] = null;
  1445. $zipFile->saveAsFile($this->outputFilename);
  1446. $zipFile->close();
  1447. static::assertCorrectZipArchive($this->outputFilename);
  1448. $zipFile->openFile($this->outputFilename);
  1449. static::assertTrue(isset($zipFile['file1.txt']));
  1450. static::assertTrue(isset($zipFile['dir/file2.txt']));
  1451. static::assertTrue(isset($zipFile['dir/empty dir/']));
  1452. static::assertFalse(isset($zipFile['dir/empty dir/2/']));
  1453. $zipFile['dir/empty dir/2/'] = null;
  1454. unset($zipFile['dir/file2.txt'], $zipFile['dir/empty dir/']);
  1455. $zipFile->saveAsFile($this->outputFilename);
  1456. $zipFile->close();
  1457. static::assertCorrectZipArchive($this->outputFilename);
  1458. $zipFile->openFile($this->outputFilename);
  1459. static::assertTrue(isset($zipFile['file1.txt']));
  1460. static::assertFalse(isset($zipFile['dir/file2.txt']));
  1461. static::assertFalse(isset($zipFile['dir/empty dir/']));
  1462. static::assertTrue(isset($zipFile['dir/empty dir/2/']));
  1463. $zipFile->close();
  1464. }
  1465. /**
  1466. * @throws ZipException
  1467. */
  1468. public function testArrayAccessAddFile()
  1469. {
  1470. $entryName = 'path/to/file.dat';
  1471. $entryNameStream = 'path/to/' . basename(__FILE__);
  1472. $zipFile = new ZipFile();
  1473. $zipFile[$entryName] = new \SplFileInfo(__FILE__);
  1474. $zipFile[$entryNameStream] = fopen(__FILE__, 'rb');
  1475. $zipFile->saveAsFile($this->outputFilename);
  1476. $zipFile->close();
  1477. static::assertCorrectZipArchive($this->outputFilename);
  1478. $zipFile->openFile($this->outputFilename);
  1479. static::assertSame(\count($zipFile), 2);
  1480. static::assertTrue(isset($zipFile[$entryName]));
  1481. static::assertTrue(isset($zipFile[$entryNameStream]));
  1482. static::assertSame($zipFile[$entryName], file_get_contents(__FILE__));
  1483. static::assertSame($zipFile[$entryNameStream], file_get_contents(__FILE__));
  1484. $zipFile->close();
  1485. }
  1486. /**
  1487. * @throws Exception\ZipEntryNotFoundException
  1488. * @throws ZipException
  1489. */
  1490. public function testUnknownCompressionMethod()
  1491. {
  1492. $zipFile = new ZipFile();
  1493. $zipFile->addFromString('file', 'content', ZipEntry::UNKNOWN);
  1494. $zipFile->addFromString('file2', base64_encode(CryptoUtil::randomBytes(512)), ZipEntry::UNKNOWN);
  1495. static::assertSame($zipFile->getEntryInfo('file')->getMethodName(), 'Unknown');
  1496. static::assertSame($zipFile->getEntryInfo('file2')->getMethodName(), 'Unknown');
  1497. $zipFile->saveAsFile($this->outputFilename);
  1498. $zipFile->close();
  1499. $zipFile->openFile($this->outputFilename);
  1500. static::assertSame($zipFile->getEntryInfo('file')->getMethodName(), 'No compression');
  1501. static::assertSame($zipFile->getEntryInfo('file2')->getMethodName(), 'Deflate');
  1502. $zipFile->close();
  1503. }
  1504. /**
  1505. * @throws ZipException
  1506. */
  1507. public function testAddEmptyDirNullName()
  1508. {
  1509. $this->setExpectedException(InvalidArgumentException::class, 'Dir name is null');
  1510. $zipFile = new ZipFile();
  1511. $zipFile->addEmptyDir(null);
  1512. }
  1513. /**
  1514. * @throws ZipException
  1515. */
  1516. public function testAddEmptyDirEmptyName()
  1517. {
  1518. $this->setExpectedException(InvalidArgumentException::class, 'Empty dir name');
  1519. $zipFile = new ZipFile();
  1520. $zipFile->addEmptyDir('');
  1521. }
  1522. public function testNotFoundEntry()
  1523. {
  1524. $this->setExpectedException(ZipEntryNotFoundException::class, '"bad entry name"');
  1525. $zipFile = new ZipFile();
  1526. $zipFile['bad entry name'];
  1527. }
  1528. /**
  1529. * Test rewrite input file.
  1530. *
  1531. * @throws ZipException
  1532. */
  1533. public function testRewriteFile()
  1534. {
  1535. $zipFile = new ZipFile();
  1536. $zipFile['file'] = 'content';
  1537. $zipFile['file2'] = 'content2';
  1538. static::assertSame(\count($zipFile), 2);
  1539. $zipFile
  1540. ->saveAsFile($this->outputFilename)
  1541. ->close()
  1542. ;
  1543. $md5file = md5_file($this->outputFilename);
  1544. $zipFile->openFile($this->outputFilename);
  1545. static::assertSame(\count($zipFile), 2);
  1546. static::assertTrue(isset($zipFile['file']));
  1547. static::assertTrue(isset($zipFile['file2']));
  1548. $zipFile['file3'] = 'content3';
  1549. static::assertSame(\count($zipFile), 3);
  1550. $zipFile = $zipFile->rewrite();
  1551. static::assertSame(\count($zipFile), 3);
  1552. static::assertTrue(isset($zipFile['file']));
  1553. static::assertTrue(isset($zipFile['file2']));
  1554. static::assertTrue(isset($zipFile['file3']));
  1555. $zipFile->close();
  1556. static::assertNotSame(md5_file($this->outputFilename), $md5file);
  1557. }
  1558. /**
  1559. * Test rewrite for string.
  1560. *
  1561. * @throws ZipException
  1562. */
  1563. public function testRewriteString()
  1564. {
  1565. $zipFile = new ZipFile();
  1566. $zipFile['file'] = 'content';
  1567. $zipFile['file2'] = 'content2';
  1568. $zipFile->saveAsFile($this->outputFilename);
  1569. $zipFile->close();
  1570. $zipFile->openFromString(file_get_contents($this->outputFilename));
  1571. static::assertSame(\count($zipFile), 2);
  1572. static::assertTrue(isset($zipFile['file']));
  1573. static::assertTrue(isset($zipFile['file2']));
  1574. $zipFile['file3'] = 'content3';
  1575. $zipFile = $zipFile->rewrite();
  1576. static::assertSame(\count($zipFile), 3);
  1577. static::assertTrue(isset($zipFile['file']));
  1578. static::assertTrue(isset($zipFile['file2']));
  1579. static::assertTrue(isset($zipFile['file3']));
  1580. $zipFile->close();
  1581. }
  1582. /**
  1583. * @throws ZipException
  1584. */
  1585. public function testRewriteNullStream()
  1586. {
  1587. $this->setExpectedException(ZipException::class, 'input stream is null');
  1588. $zipFile = new ZipFile();
  1589. $zipFile->rewrite();
  1590. }
  1591. /**
  1592. * @throws ZipException
  1593. */
  1594. public function testFilename0()
  1595. {
  1596. $zipFile = new ZipFile();
  1597. $zipFile[0] = 0;
  1598. static::assertTrue(isset($zipFile[0]));
  1599. static::assertTrue(isset($zipFile['0']));
  1600. static::assertCount(1, $zipFile);
  1601. $zipFile
  1602. ->saveAsFile($this->outputFilename)
  1603. ->close()
  1604. ;
  1605. static::assertCorrectZipArchive($this->outputFilename);
  1606. $zipFile->openFile($this->outputFilename);
  1607. static::assertTrue(isset($zipFile[0]));
  1608. static::assertTrue(isset($zipFile['0']));
  1609. static::assertSame($zipFile['0'], '0');
  1610. static::assertCount(1, $zipFile);
  1611. $zipFile->close();
  1612. static::assertTrue(unlink($this->outputFilename));
  1613. $zipFile = new ZipFile();
  1614. $zipFile->addFromString(0, 0);
  1615. static::assertTrue(isset($zipFile[0]));
  1616. static::assertTrue(isset($zipFile['0']));
  1617. static::assertCount(1, $zipFile);
  1618. $zipFile
  1619. ->saveAsFile($this->outputFilename)
  1620. ->close()
  1621. ;
  1622. static::assertCorrectZipArchive($this->outputFilename);
  1623. }
  1624. /**
  1625. * @throws ZipException
  1626. */
  1627. public function testPsrResponse()
  1628. {
  1629. $zipFile = new ZipFile();
  1630. for ($i = 0; $i < 10; $i++) {
  1631. $zipFile[$i] = $i;
  1632. }
  1633. $filename = 'file.jar';
  1634. $response = $zipFile->outputAsResponse(new Response(), $filename);
  1635. static::assertInstanceOf(ResponseInterface::class, $response);
  1636. static::assertSame('application/java-archive', $response->getHeaderLine('content-type'));
  1637. static::assertSame('attachment; filename="file.jar"', $response->getHeaderLine('content-disposition'));
  1638. }
  1639. /**
  1640. * @throws ZipEntryNotFoundException
  1641. * @throws ZipException
  1642. */
  1643. public function testCompressionLevel()
  1644. {
  1645. $zipFile = new ZipFile();
  1646. $zipFile
  1647. ->addFromString('file', 'content', ZipFileInterface::METHOD_DEFLATED)
  1648. ->setCompressionLevelEntry('file', ZipFileInterface::LEVEL_BEST_COMPRESSION)
  1649. ->addFromString('file2', 'content', ZipFileInterface::METHOD_DEFLATED)
  1650. ->setCompressionLevelEntry('file2', ZipFileInterface::LEVEL_FAST)
  1651. ->addFromString('file3', 'content', ZipFileInterface::METHOD_DEFLATED)
  1652. ->setCompressionLevelEntry('file3', ZipFileInterface::LEVEL_SUPER_FAST)
  1653. ->addFromString('file4', 'content', ZipFileInterface::METHOD_DEFLATED)
  1654. ->setCompressionLevelEntry('file4', ZipFileInterface::LEVEL_DEFAULT_COMPRESSION)
  1655. ->saveAsFile($this->outputFilename)
  1656. ->close()
  1657. ;
  1658. static::assertCorrectZipArchive($this->outputFilename);
  1659. $zipFile->openFile($this->outputFilename);
  1660. static::assertSame(
  1661. $zipFile->getEntryInfo('file')
  1662. ->getCompressionLevel(),
  1663. ZipFileInterface::LEVEL_BEST_COMPRESSION
  1664. );
  1665. static::assertSame(
  1666. $zipFile->getEntryInfo('file2')
  1667. ->getCompressionLevel(),
  1668. ZipFileInterface::LEVEL_FAST
  1669. );
  1670. static::assertSame(
  1671. $zipFile->getEntryInfo('file3')
  1672. ->getCompressionLevel(),
  1673. ZipFileInterface::LEVEL_SUPER_FAST
  1674. );
  1675. static::assertSame(
  1676. $zipFile->getEntryInfo('file4')
  1677. ->getCompressionLevel(),
  1678. ZipFileInterface::LEVEL_DEFAULT_COMPRESSION
  1679. );
  1680. $zipFile->close();
  1681. }
  1682. /**
  1683. * @throws ZipException
  1684. */
  1685. public function testInvalidCompressionLevel()
  1686. {
  1687. $this->setExpectedException(InvalidArgumentException::class, 'Invalid compression level');
  1688. $zipFile = new ZipFile();
  1689. $zipFile->addFromString('file', 'content');
  1690. $zipFile->setCompressionLevel(15);
  1691. }
  1692. /**
  1693. * @throws ZipException
  1694. */
  1695. public function testInvalidCompressionLevelEntry()
  1696. {
  1697. $this->setExpectedException(InvalidArgumentException::class, 'Invalid compression level');
  1698. $zipFile = new ZipFile();
  1699. $zipFile->addFromString('file', 'content');
  1700. $zipFile->setCompressionLevelEntry('file', 15);
  1701. }
  1702. /**
  1703. * @throws ZipException
  1704. */
  1705. public function testCompressionGlobal()
  1706. {
  1707. $zipFile = new ZipFile();
  1708. for ($i = 0; $i < 10; $i++) {
  1709. $zipFile->addFromString('file' . $i, 'content', ZipFileInterface::METHOD_DEFLATED);
  1710. }
  1711. $zipFile
  1712. ->setCompressionLevel(ZipFileInterface::LEVEL_BEST_SPEED)
  1713. ->saveAsFile($this->outputFilename)
  1714. ->close()
  1715. ;
  1716. static::assertCorrectZipArchive($this->outputFilename);
  1717. $zipFile->openFile($this->outputFilename);
  1718. $infoList = $zipFile->getAllInfo();
  1719. array_walk(
  1720. $infoList,
  1721. function (ZipInfo $zipInfo) {
  1722. $this->assertSame($zipInfo->getCompressionLevel(), ZipFileInterface::LEVEL_BEST_SPEED);
  1723. }
  1724. );
  1725. $zipFile->close();
  1726. }
  1727. /**
  1728. * @throws ZipEntryNotFoundException
  1729. * @throws ZipException
  1730. */
  1731. public function testCompressionMethodEntry()
  1732. {
  1733. $zipFile = new ZipFile();
  1734. $zipFile->addFromString('file', 'content', ZipFileInterface::METHOD_STORED);
  1735. $zipFile->saveAsFile($this->outputFilename);
  1736. $zipFile->close();
  1737. $zipFile->openFile($this->outputFilename);
  1738. static::assertSame($zipFile->getEntryInfo('file')->getMethodName(), 'No compression');
  1739. $zipFile->setCompressionMethodEntry('file', ZipFileInterface::METHOD_DEFLATED);
  1740. static::assertSame($zipFile->getEntryInfo('file')->getMethodName(), 'Deflate');
  1741. $zipFile->rewrite();
  1742. static::assertSame($zipFile->getEntryInfo('file')->getMethodName(), 'Deflate');
  1743. }
  1744. /**
  1745. * @throws ZipException
  1746. */
  1747. public function testInvalidCompressionMethodEntry()
  1748. {
  1749. $this->setExpectedException(ZipUnsupportMethodException::class, 'Unsupported method');
  1750. $zipFile = new ZipFile();
  1751. $zipFile->addFromString('file', 'content', ZipFileInterface::METHOD_STORED);
  1752. $zipFile->setCompressionMethodEntry('file', 99);
  1753. }
  1754. /**
  1755. * @throws ZipException
  1756. */
  1757. public function testUnchangeAll()
  1758. {
  1759. $zipFile = new ZipFile();
  1760. for ($i = 0; $i < 10; $i++) {
  1761. $zipFile[$i] = $i;
  1762. }
  1763. $zipFile->setArchiveComment('comment');
  1764. static::assertCount(10, $zipFile);
  1765. static::assertSame($zipFile->getArchiveComment(), 'comment');
  1766. $zipFile->saveAsFile($this->outputFilename);
  1767. $zipFile->unchangeAll();
  1768. static::assertCount(0, $zipFile);
  1769. static::assertNull($zipFile->getArchiveComment());
  1770. $zipFile->close();
  1771. $zipFile->openFile($this->outputFilename);
  1772. static::assertCount(10, $zipFile);
  1773. static::assertSame($zipFile->getArchiveComment(), 'comment');
  1774. for ($i = 10; $i < 100; $i++) {
  1775. $zipFile[$i] = $i;
  1776. }
  1777. $zipFile->setArchiveComment('comment 2');
  1778. static::assertCount(100, $zipFile);
  1779. static::assertSame($zipFile->getArchiveComment(), 'comment 2');
  1780. $zipFile->unchangeAll();
  1781. static::assertCount(10, $zipFile);
  1782. static::assertSame($zipFile->getArchiveComment(), 'comment');
  1783. $zipFile->close();
  1784. }
  1785. /**
  1786. * @throws ZipException
  1787. */
  1788. public function testUnchangeArchiveComment()
  1789. {
  1790. $zipFile = new ZipFile();
  1791. for ($i = 0; $i < 10; $i++) {
  1792. $zipFile[$i] = $i;
  1793. }
  1794. $zipFile->setArchiveComment('comment');
  1795. static::assertSame($zipFile->getArchiveComment(), 'comment');
  1796. $zipFile->saveAsFile($this->outputFilename);
  1797. $zipFile->unchangeArchiveComment();
  1798. static::assertNull($zipFile->getArchiveComment());
  1799. $zipFile->close();
  1800. $zipFile->openFile($this->outputFilename);
  1801. static::assertSame($zipFile->getArchiveComment(), 'comment');
  1802. $zipFile->setArchiveComment('comment 2');
  1803. static::assertSame($zipFile->getArchiveComment(), 'comment 2');
  1804. $zipFile->unchangeArchiveComment();
  1805. static::assertSame($zipFile->getArchiveComment(), 'comment');
  1806. $zipFile->close();
  1807. }
  1808. /**
  1809. * @throws ZipEntryNotFoundException
  1810. * @throws ZipException
  1811. */
  1812. public function testUnchangeEntry()
  1813. {
  1814. $zipFile = new ZipFile();
  1815. $zipFile['file 1'] = 'content 1';
  1816. $zipFile['file 2'] = 'content 2';
  1817. $zipFile
  1818. ->saveAsFile($this->outputFilename)
  1819. ->close()
  1820. ;
  1821. $zipFile->openFile($this->outputFilename);
  1822. $zipFile['file 1'] = 'modify content 1';
  1823. $zipFile->setPasswordEntry('file 1', 'password');
  1824. static::assertSame($zipFile['file 1'], 'modify content 1');
  1825. static::assertTrue($zipFile->getEntryInfo('file 1')->isEncrypted());
  1826. static::assertSame($zipFile['file 2'], 'content 2');
  1827. static::assertFalse($zipFile->getEntryInfo('file 2')->isEncrypted());
  1828. $zipFile->unchangeEntry('file 1');
  1829. static::assertSame($zipFile['file 1'], 'content 1');
  1830. static::assertFalse($zipFile->getEntryInfo('file 1')->isEncrypted());
  1831. static::assertSame($zipFile['file 2'], 'content 2');
  1832. static::assertFalse($zipFile->getEntryInfo('file 2')->isEncrypted());
  1833. $zipFile->close();
  1834. }
  1835. }