ZipFileTest.php 68 KB

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