ZipFileTest.php 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684
  1. <?php
  2. namespace PhpZip\Tests;
  3. use GuzzleHttp\Psr7\Response;
  4. use PhpZip\Constants\ZipCompressionLevel;
  5. use PhpZip\Constants\ZipCompressionMethod;
  6. use PhpZip\Constants\ZipPlatform;
  7. use PhpZip\Exception\InvalidArgumentException;
  8. use PhpZip\Exception\ZipEntryNotFoundException;
  9. use PhpZip\Exception\ZipException;
  10. use PhpZip\Exception\ZipUnsupportMethodException;
  11. use PhpZip\Model\Data\ZipFileData;
  12. use PhpZip\Model\ZipEntry;
  13. use PhpZip\Model\ZipInfo;
  14. use PhpZip\Util\FilesUtil;
  15. use PhpZip\ZipFile;
  16. use Psr\Http\Message\ResponseInterface;
  17. /**
  18. * ZipFile test.
  19. *
  20. * @internal
  21. *
  22. * @small
  23. */
  24. class ZipFileTest extends ZipTestCase
  25. {
  26. /**
  27. * @throws ZipException
  28. */
  29. public function testOpenFileCantExists()
  30. {
  31. $this->expectException(ZipException::class);
  32. $this->expectExceptionMessage('does not exist');
  33. $zipFile = new ZipFile();
  34. $zipFile->openFile(uniqid('', false));
  35. }
  36. /**
  37. * @throws ZipException
  38. */
  39. public function testOpenFileCantOpen()
  40. {
  41. static::skipTestForWindows();
  42. static::skipTestForRootUser();
  43. $this->expectException(ZipException::class);
  44. $this->expectExceptionMessage('can\'t open');
  45. static::assertNotFalse(file_put_contents($this->outputFilename, 'content'));
  46. static::assertTrue(chmod($this->outputFilename, 0222));
  47. $zipFile = new ZipFile();
  48. $zipFile->openFile($this->outputFilename);
  49. }
  50. /**
  51. * @throws ZipException
  52. */
  53. public function testOpenFileEmptyFile()
  54. {
  55. $this->expectException(ZipException::class);
  56. $this->expectExceptionMessage('Corrupt zip file');
  57. static::assertNotFalse(touch($this->outputFilename));
  58. $zipFile = new ZipFile();
  59. $zipFile->openFile($this->outputFilename);
  60. }
  61. /**
  62. * @throws ZipException
  63. * @throws \Exception
  64. */
  65. public function testOpenFileInvalidZip()
  66. {
  67. $this->expectException(
  68. ZipException::class
  69. );
  70. $this->expectExceptionMessage(
  71. 'Invalid zip file. The end of the central directory could not be found.'
  72. );
  73. static::assertNotFalse(file_put_contents($this->outputFilename, random_bytes(255)));
  74. $zipFile = new ZipFile();
  75. $zipFile->openFile($this->outputFilename);
  76. }
  77. /**
  78. * @throws ZipException
  79. */
  80. public function testOpenFromStringNullString()
  81. {
  82. $this->expectException(InvalidArgumentException::class);
  83. $this->expectExceptionMessage('Empty string passed');
  84. $zipFile = new ZipFile();
  85. $zipFile->openFromString(null);
  86. }
  87. /**
  88. * @throws ZipException
  89. */
  90. public function testOpenFromStringEmptyString()
  91. {
  92. $this->expectException(InvalidArgumentException::class);
  93. $this->expectExceptionMessage('Empty string passed');
  94. $zipFile = new ZipFile();
  95. $zipFile->openFromString('');
  96. }
  97. /**
  98. * @throws ZipException
  99. * @throws \Exception
  100. */
  101. public function testOpenFromStringInvalidZip()
  102. {
  103. $this->expectException(
  104. ZipException::class
  105. );
  106. $this->expectExceptionMessage(
  107. 'Invalid zip file. The end of the central directory could not be found.'
  108. );
  109. $zipFile = new ZipFile();
  110. $zipFile->openFromString(random_bytes(255));
  111. }
  112. /**
  113. * @throws ZipException
  114. */
  115. public function testOpenFromString()
  116. {
  117. $zipFile = new ZipFile();
  118. $zipFile->addFromString('file', 'content');
  119. $zipFile['file2'] = 'content 2';
  120. $zipContents = $zipFile->outputAsString();
  121. $zipFile->close();
  122. $zipFile->openFromString($zipContents);
  123. static::assertSame($zipFile->count(), 2);
  124. static::assertTrue(isset($zipFile['file']));
  125. static::assertTrue(isset($zipFile['file2']));
  126. static::assertSame($zipFile['file'], 'content');
  127. static::assertSame($zipFile['file2'], 'content 2');
  128. $zipFile->close();
  129. }
  130. /**
  131. * @throws ZipException
  132. */
  133. public function testOpenFromStreamNullStream()
  134. {
  135. $this->expectException(InvalidArgumentException::class);
  136. $this->expectExceptionMessage('Stream must be a resource');
  137. $zipFile = new ZipFile();
  138. $zipFile->openFromStream(null);
  139. }
  140. /**
  141. * @throws ZipException
  142. */
  143. public function testOpenFromStreamInvalidResourceType()
  144. {
  145. $this->expectException(InvalidArgumentException::class);
  146. $this->expectExceptionMessage('Stream must be a resource');
  147. $zipFile = new ZipFile();
  148. /** @noinspection PhpParamsInspection */
  149. $zipFile->openFromStream('stream resource');
  150. }
  151. /**
  152. * @throws ZipException
  153. *
  154. * @noinspection PhpComposerExtensionStubsInspection
  155. */
  156. public function testOpenFromStreamInvalidResourceType2()
  157. {
  158. $this->expectException(InvalidArgumentException::class);
  159. $exceptionMessage = \PHP_VERSION_ID < 80000
  160. ? 'Invalid resource type'
  161. : 'Stream must be a resource';
  162. $this->expectExceptionMessage($exceptionMessage);
  163. $zipFile = new ZipFile();
  164. if (!\extension_loaded('gd')) {
  165. static::markTestSkipped('not extension gd');
  166. }
  167. $zipFile->openFromStream(imagecreate(1, 1));
  168. }
  169. /**
  170. * @throws ZipException
  171. */
  172. public function testOpenFromStreamInvalidResourceType3()
  173. {
  174. $this->expectException(InvalidArgumentException::class);
  175. $this->expectExceptionMessage('Directory stream not supported');
  176. $zipFile = new ZipFile();
  177. $zipFile->openFromStream(opendir(__DIR__));
  178. }
  179. /**
  180. * @throws ZipException
  181. * @noinspection PhpUsageOfSilenceOperatorInspection
  182. * @noinspection NestedPositiveIfStatementsInspection
  183. */
  184. public function testOpenFromStreamNoSeekable()
  185. {
  186. $this->expectException(InvalidArgumentException::class);
  187. $this->expectExceptionMessage('The stream wrapper type "http" is not supported');
  188. if (!$fp = @fopen('http://localhost', 'rb')) {
  189. if (!$fp = @fopen('http://example.org', 'rb')) {
  190. static::markTestSkipped('not connected to localhost or remote host');
  191. return;
  192. }
  193. }
  194. $zipFile = new ZipFile();
  195. $zipFile->openFromStream($fp);
  196. }
  197. /**
  198. * @throws ZipException
  199. */
  200. public function testOpenFromStreamEmptyContents()
  201. {
  202. $this->expectException(ZipException::class);
  203. $this->expectExceptionMessage('Corrupt zip file');
  204. $fp = fopen($this->outputFilename, 'w+b');
  205. $zipFile = new ZipFile();
  206. $zipFile->openFromStream($fp);
  207. }
  208. /**
  209. * @throws ZipException
  210. * @throws \Exception
  211. */
  212. public function testOpenFromStreamInvalidZip()
  213. {
  214. $this->expectException(
  215. ZipException::class
  216. );
  217. $this->expectExceptionMessage(
  218. 'Invalid zip file. The end of the central directory could not be found.'
  219. );
  220. $fp = fopen($this->outputFilename, 'w+b');
  221. fwrite($fp, random_bytes(255));
  222. $zipFile = new ZipFile();
  223. $zipFile->openFromStream($fp);
  224. }
  225. /**
  226. * @throws ZipException
  227. */
  228. public function testOpenFromStream()
  229. {
  230. $zipFile = new ZipFile();
  231. $zipFile
  232. ->addFromString('file', 'content')
  233. ->saveAsFile($this->outputFilename)
  234. ->close()
  235. ;
  236. $handle = fopen($this->outputFilename, 'rb');
  237. $zipFile->openFromStream($handle);
  238. static::assertSame($zipFile->count(), 1);
  239. static::assertTrue(isset($zipFile['file']));
  240. static::assertSame($zipFile['file'], 'content');
  241. $zipFile->close();
  242. }
  243. /**
  244. * Test create, open and extract empty archive.
  245. *
  246. * @throws ZipException
  247. */
  248. public function testEmptyArchive()
  249. {
  250. $zipFile = new ZipFile();
  251. $zipFile
  252. ->saveAsFile($this->outputFilename)
  253. ->close()
  254. ;
  255. static::assertCorrectEmptyZip($this->outputFilename);
  256. static::assertTrue(mkdir($this->outputDirname, 0755, true));
  257. $zipFile->openFile($this->outputFilename);
  258. static::assertSame($zipFile->count(), 0);
  259. $zipFile
  260. ->extractTo($this->outputDirname)
  261. ->close()
  262. ;
  263. static::assertTrue(FilesUtil::isEmptyDir($this->outputDirname));
  264. }
  265. /**
  266. * No modified archive.
  267. *
  268. * @throws ZipException
  269. *
  270. * @see ZipOutputFile::create()
  271. */
  272. public function testNoModifiedArchive()
  273. {
  274. static::assertTrue(mkdir($this->outputDirname, 0755, true));
  275. $fileActual = $this->outputDirname . \DIRECTORY_SEPARATOR . 'file_actual.zip';
  276. $fileExpected = $this->outputDirname . \DIRECTORY_SEPARATOR . 'file_expected.zip';
  277. $zipFile = new ZipFile();
  278. $zipFile->addDirRecursive(__DIR__ . '/../src');
  279. $sourceCount = $zipFile->count();
  280. static::assertTrue($sourceCount > 0);
  281. $zipFile
  282. ->saveAsFile($fileActual)
  283. ->close()
  284. ;
  285. static::assertCorrectZipArchive($fileActual);
  286. $zipFile
  287. ->openFile($fileActual)
  288. ->saveAsFile($fileExpected)
  289. ;
  290. static::assertCorrectZipArchive($fileExpected);
  291. $zipFileExpected = new ZipFile();
  292. $zipFileExpected->openFile($fileExpected);
  293. static::assertSame($zipFile->count(), $sourceCount);
  294. static::assertSame($zipFileExpected->count(), $zipFile->count());
  295. static::assertSame($zipFileExpected->getListFiles(), $zipFile->getListFiles());
  296. foreach ($zipFile as $entryName => $content) {
  297. static::assertSame($zipFileExpected[$entryName], $content);
  298. }
  299. $zipFileExpected->close();
  300. $zipFile->close();
  301. }
  302. /**
  303. * Create archive and add files.
  304. *
  305. * @throws ZipException
  306. *
  307. * @see ZipOutputFile::addFromFile()
  308. * @see ZipOutputFile::addFromStream()
  309. * @see ZipFile::getEntryContents()
  310. * @see ZipOutputFile::addFromString()
  311. */
  312. public function testCreateArchiveAndAddFiles()
  313. {
  314. $outputFromString = file_get_contents(__FILE__);
  315. $outputFromString2 = file_get_contents(\dirname(__DIR__) . \DIRECTORY_SEPARATOR . 'README.md');
  316. $outputFromFile = file_get_contents(\dirname(__DIR__) . \DIRECTORY_SEPARATOR . 'phpunit.xml');
  317. $outputFromStream = file_get_contents(\dirname(__DIR__) . \DIRECTORY_SEPARATOR . 'composer.json');
  318. $filenameFromString = basename(__FILE__);
  319. $filenameFromString2 = 'test_file.txt';
  320. $filenameFromFile = 'data/test file.txt';
  321. $filenameFromStream = 'data/ডিরেক্টরি/αρχείο.json';
  322. $emptyDirName = 'empty dir/пустой каталог/空目錄/ไดเรกทอรีที่ว่างเปล่า/';
  323. $emptyDirName2 = 'empty dir/пустой каталог/';
  324. $emptyDirName3 = 'empty dir/пустой каталог/ещё один пустой каталог/';
  325. $tempFile = tempnam(sys_get_temp_dir(), 'txt');
  326. file_put_contents($tempFile, $outputFromFile);
  327. $tempStream = tmpfile();
  328. fwrite($tempStream, $outputFromStream);
  329. $zipFile = new ZipFile();
  330. $zipFile
  331. ->addFromString($filenameFromString, $outputFromString)
  332. ->addFile($tempFile, $filenameFromFile)
  333. ->addFromStream($tempStream, $filenameFromStream)
  334. ->addEmptyDir($emptyDirName)
  335. ;
  336. $zipFile[$filenameFromString2] = $outputFromString2;
  337. $zipFile[$emptyDirName2] = null;
  338. $zipFile[$emptyDirName3] = 'this content ignoring';
  339. static::assertSame(\count($zipFile), 7);
  340. $zipFile
  341. ->saveAsFile($this->outputFilename)
  342. ->close()
  343. ;
  344. unlink($tempFile);
  345. static::assertCorrectZipArchive($this->outputFilename);
  346. $zipFile->openFile($this->outputFilename);
  347. static::assertSame(\count($zipFile), 7);
  348. static::assertSame($zipFile[$filenameFromString], $outputFromString);
  349. static::assertSame($zipFile[$filenameFromFile], $outputFromFile);
  350. static::assertSame($zipFile[$filenameFromStream], $outputFromStream);
  351. static::assertSame($zipFile[$filenameFromString2], $outputFromString2);
  352. static::assertTrue(isset($zipFile[$emptyDirName]));
  353. static::assertTrue(isset($zipFile[$emptyDirName2]));
  354. static::assertTrue(isset($zipFile[$emptyDirName3]));
  355. static::assertTrue($zipFile->isDirectory($emptyDirName));
  356. static::assertTrue($zipFile->isDirectory($emptyDirName2));
  357. static::assertTrue($zipFile->isDirectory($emptyDirName3));
  358. $listFiles = $zipFile->getListFiles();
  359. static::assertSame($listFiles[0], $filenameFromString);
  360. static::assertSame($listFiles[1], $filenameFromFile);
  361. static::assertSame($listFiles[2], $filenameFromStream);
  362. static::assertSame($listFiles[3], $emptyDirName);
  363. static::assertSame($listFiles[4], $filenameFromString2);
  364. static::assertSame($listFiles[5], $emptyDirName2);
  365. static::assertSame($listFiles[6], $emptyDirName3);
  366. $zipFile->close();
  367. }
  368. /**
  369. * @throws ZipException
  370. */
  371. public function testEmptyContent()
  372. {
  373. $zipFile = new ZipFile();
  374. $zipFile['file'] = '';
  375. $zipFile->saveAsFile($this->outputFilename);
  376. $zipFile->close();
  377. $zipFile->openFile($this->outputFilename);
  378. static::assertSame($zipFile['file'], '');
  379. $zipFile->close();
  380. }
  381. /**
  382. * Test compression method from image file.
  383. *
  384. * @throws ZipException
  385. */
  386. public function testCompressionMethodFromImageMimeType()
  387. {
  388. if (!\function_exists('mime_content_type')) {
  389. static::markTestSkipped('Function mime_content_type not exists');
  390. }
  391. $outputFilename = $this->outputFilename;
  392. $this->outputFilename .= '.gif';
  393. static::assertNotFalse(
  394. file_put_contents(
  395. $this->outputFilename,
  396. base64_decode('R0lGODlhAQABAJAAAP8AAAAAACH5BAUQAAAALAAAAAABAAEAAAICBAEAOw==')
  397. )
  398. );
  399. $basename = basename($this->outputFilename);
  400. $zipFile = new ZipFile();
  401. $zipFile->addFile($this->outputFilename, $basename);
  402. $zipFile->saveAsFile($outputFilename);
  403. unlink($this->outputFilename);
  404. $this->outputFilename = $outputFilename;
  405. $zipFile->openFile($this->outputFilename);
  406. $info = $zipFile->getEntryInfo($basename);
  407. static::assertSame($info->getMethodName(), 'Stored');
  408. $zipFile->close();
  409. }
  410. /**
  411. * Rename zip entry name.
  412. *
  413. * @throws ZipException
  414. */
  415. public function testRename()
  416. {
  417. $oldName = basename(__FILE__);
  418. $newName = 'tests/' . $oldName;
  419. $zipFile = new ZipFile();
  420. $zipFile->addDir(__DIR__);
  421. $zipFile->saveAsFile($this->outputFilename);
  422. $zipFile->close();
  423. static::assertCorrectZipArchive($this->outputFilename);
  424. $zipFile->openFile($this->outputFilename);
  425. $zipFile->rename($oldName, $newName);
  426. $zipFile->addFromString('file1.txt', 'content');
  427. $zipFile->addFromString('file2.txt', 'content');
  428. $zipFile->addFromString('file3.txt', 'content');
  429. $zipFile->rename('file1.txt', 'file_long_name.txt');
  430. $zipFile->rename('file2.txt', 'file4.txt');
  431. $zipFile->rename('file3.txt', 'fi.txt');
  432. $zipFile->saveAsFile($this->outputFilename);
  433. $zipFile->close();
  434. static::assertCorrectZipArchive($this->outputFilename);
  435. $zipFile->openFile($this->outputFilename);
  436. static::assertFalse(isset($zipFile[$oldName]));
  437. static::assertTrue(isset($zipFile[$newName]));
  438. static::assertFalse(isset($zipFile['file1.txt']));
  439. static::assertFalse(isset($zipFile['file2.txt']));
  440. static::assertFalse(isset($zipFile['file3.txt']));
  441. static::assertTrue(isset($zipFile['file_long_name.txt']));
  442. static::assertTrue(isset($zipFile['file4.txt']));
  443. static::assertTrue(isset($zipFile['fi.txt']));
  444. $zipFile->close();
  445. }
  446. /**
  447. * @throws ZipException
  448. */
  449. public function testRenameEntryNull()
  450. {
  451. $this->expectException(InvalidArgumentException::class);
  452. $this->expectExceptionMessage('name is null');
  453. $zipFile = new ZipFile();
  454. $zipFile->rename(null, 'new-file');
  455. }
  456. /**
  457. * @throws ZipException
  458. */
  459. public function testRenameEntryNull2()
  460. {
  461. $this->expectException(InvalidArgumentException::class);
  462. $this->expectExceptionMessage('name is null');
  463. $zipFile = new ZipFile();
  464. $zipFile->rename('old-file', null);
  465. }
  466. /**
  467. * @throws ZipException
  468. */
  469. public function testRenameEntryToExistsNewEntry()
  470. {
  471. $this->expectException(InvalidArgumentException::class);
  472. $this->expectExceptionMessage('is exists');
  473. $zipFile = new ZipFile();
  474. $zipFile['file'] = 'content';
  475. $zipFile['file2'] = 'content 2';
  476. $zipFile->saveAsFile($this->outputFilename);
  477. $zipFile->close();
  478. $zipFile = new ZipFile();
  479. $zipFile->openFile($this->outputFilename);
  480. $zipFile->rename('file2', 'file');
  481. }
  482. /**
  483. * @throws ZipException
  484. */
  485. public function testRenameEntryNotFound()
  486. {
  487. $this->expectException(ZipEntryNotFoundException::class);
  488. $zipFile = new ZipFile();
  489. $zipFile['file'] = 'content';
  490. $zipFile['file2'] = 'content 2';
  491. $zipFile->saveAsFile($this->outputFilename);
  492. $zipFile->close();
  493. $zipFile = new ZipFile();
  494. $zipFile->openFile($this->outputFilename);
  495. $zipFile->rename('file2.bak', 'file3');
  496. }
  497. /**
  498. * Delete entry from name.
  499. *
  500. * @throws ZipException
  501. */
  502. public function testDeleteFromName()
  503. {
  504. $inputDir = \dirname(__DIR__) . \DIRECTORY_SEPARATOR;
  505. $deleteEntryName = 'composer.json';
  506. $zipFile = new ZipFile();
  507. $zipFile->addDir($inputDir);
  508. $zipFile->saveAsFile($this->outputFilename);
  509. $zipFile->close();
  510. static::assertCorrectZipArchive($this->outputFilename);
  511. $zipFile->openFile($this->outputFilename);
  512. $zipFile->deleteFromName($deleteEntryName);
  513. $zipFile->saveAsFile($this->outputFilename);
  514. $zipFile->close();
  515. static::assertCorrectZipArchive($this->outputFilename);
  516. $zipFile->openFile($this->outputFilename);
  517. static::assertFalse(isset($zipFile[$deleteEntryName]));
  518. $zipFile->close();
  519. }
  520. /**
  521. * @throws ZipEntryNotFoundException
  522. * @throws ZipException
  523. */
  524. public function testDeleteNewEntry()
  525. {
  526. $zipFile = new ZipFile();
  527. $zipFile['entry1'] = '';
  528. $zipFile['entry2'] = '';
  529. $zipFile->deleteFromName('entry2');
  530. $zipFile->saveAsFile($this->outputFilename);
  531. $zipFile->close();
  532. $zipFile->openFile($this->outputFilename);
  533. static::assertSame(\count($zipFile), 1);
  534. static::assertTrue(isset($zipFile['entry1']));
  535. static::assertFalse(isset($zipFile['entry2']));
  536. $zipFile->close();
  537. }
  538. /**
  539. * @throws ZipEntryNotFoundException
  540. */
  541. public function testDeleteFromNameNotFoundEntry()
  542. {
  543. $this->expectException(ZipEntryNotFoundException::class);
  544. $zipFile = new ZipFile();
  545. $zipFile->deleteFromName('entry');
  546. }
  547. public function testCatchNotFoundEntry()
  548. {
  549. $entryName = 'entry';
  550. $zipFile = new ZipFile();
  551. try {
  552. $zipFile->getEntry($entryName);
  553. } catch (ZipEntryNotFoundException $e) {
  554. static::assertSame($e->getEntryName(), $entryName);
  555. }
  556. }
  557. /**
  558. * Delete zip entries from glob pattern.
  559. *
  560. * @throws ZipException
  561. */
  562. public function testDeleteFromGlob()
  563. {
  564. $inputDir = \dirname(__DIR__);
  565. $zipFile = new ZipFile();
  566. $zipFile->addFilesFromGlobRecursive($inputDir, '**.{xml,json,md}', '/');
  567. static::assertTrue(isset($zipFile['composer.json']));
  568. static::assertTrue(isset($zipFile['phpunit.xml']));
  569. $zipFile->saveAsFile($this->outputFilename);
  570. $zipFile->close();
  571. static::assertCorrectZipArchive($this->outputFilename);
  572. $zipFile->openFile($this->outputFilename);
  573. static::assertTrue(isset($zipFile['composer.json']));
  574. static::assertTrue(isset($zipFile['phpunit.xml']));
  575. $zipFile->deleteFromGlob('**.{xml,json}');
  576. static::assertFalse(isset($zipFile['composer.json']));
  577. static::assertFalse(isset($zipFile['phpunit.xml']));
  578. $zipFile->saveAsFile($this->outputFilename);
  579. $zipFile->close();
  580. static::assertCorrectZipArchive($this->outputFilename);
  581. $zipFile->openFile($this->outputFilename);
  582. static::assertTrue($zipFile->count() > 0);
  583. foreach ($zipFile->getListFiles() as $name) {
  584. static::assertStringEndsWith('.md', $name);
  585. }
  586. $zipFile->close();
  587. }
  588. public function testDeleteFromGlobFailNull()
  589. {
  590. $this->expectException(InvalidArgumentException::class);
  591. $this->expectExceptionMessage('The glob pattern is not specified');
  592. $zipFile = new ZipFile();
  593. $zipFile->deleteFromGlob(null);
  594. }
  595. public function testDeleteFromGlobFailEmpty()
  596. {
  597. $this->expectException(InvalidArgumentException::class);
  598. $this->expectExceptionMessage('The glob pattern is not specified');
  599. $zipFile = new ZipFile();
  600. $zipFile->deleteFromGlob('');
  601. }
  602. /**
  603. * Delete entries from regex pattern.
  604. *
  605. * @throws ZipException
  606. */
  607. public function testDeleteFromRegex()
  608. {
  609. $inputDir = \dirname(__DIR__);
  610. $zipFile = new ZipFile();
  611. $zipFile->addFilesFromRegexRecursive($inputDir, '~\.(xml|json)$~i', 'Path');
  612. $zipFile->saveAsFile($this->outputFilename);
  613. $zipFile->close();
  614. static::assertCorrectZipArchive($this->outputFilename);
  615. $zipFile->openFile($this->outputFilename);
  616. $zipFile->deleteFromRegex('~\.(json)$~i');
  617. $zipFile->addFromString('test.txt', 'content');
  618. $zipFile->deleteFromRegex('~\.txt$~');
  619. $zipFile->saveAsFile($this->outputFilename);
  620. $zipFile->close();
  621. static::assertCorrectZipArchive($this->outputFilename);
  622. $zipFile->openFile($this->outputFilename);
  623. static::assertFalse(isset($zipFile['Path/composer.json']));
  624. static::assertFalse(isset($zipFile['Path/test.txt']));
  625. static::assertTrue(isset($zipFile['Path/phpunit.xml']));
  626. $zipFile->close();
  627. }
  628. public function testDeleteFromRegexFailNull()
  629. {
  630. $this->expectException(InvalidArgumentException::class);
  631. $this->expectExceptionMessage('The regex pattern is not specified');
  632. $zipFile = new ZipFile();
  633. $zipFile->deleteFromRegex(null);
  634. }
  635. public function testDeleteFromRegexFailEmpty()
  636. {
  637. $this->expectException(InvalidArgumentException::class);
  638. $this->expectExceptionMessage('The regex pattern is not specified');
  639. $zipFile = new ZipFile();
  640. $zipFile->deleteFromRegex('');
  641. }
  642. /**
  643. * Delete all entries.
  644. *
  645. * @throws ZipException
  646. */
  647. public function testDeleteAll()
  648. {
  649. $zipFile = new ZipFile();
  650. $zipFile->addDirRecursive(\dirname(__DIR__) . \DIRECTORY_SEPARATOR . 'src');
  651. static::assertTrue($zipFile->count() > 0);
  652. $zipFile->saveAsFile($this->outputFilename);
  653. $zipFile->close();
  654. static::assertCorrectZipArchive($this->outputFilename);
  655. $zipFile->openFile($this->outputFilename);
  656. static::assertTrue($zipFile->count() > 0);
  657. $zipFile->deleteAll();
  658. $zipFile->saveAsFile($this->outputFilename);
  659. $zipFile->close();
  660. static::assertCorrectEmptyZip($this->outputFilename);
  661. $zipFile->openFile($this->outputFilename);
  662. static::assertSame($zipFile->count(), 0);
  663. $zipFile->close();
  664. }
  665. /**
  666. * Test zip archive comment.
  667. *
  668. * @throws ZipException
  669. */
  670. public function testArchiveComment()
  671. {
  672. $comment = 'This zip file comment' . \PHP_EOL
  673. . 'Αυτό το σχόλιο αρχείο zip' . \PHP_EOL
  674. . 'Это комментарий zip архива' . \PHP_EOL
  675. . '這個ZIP文件註釋' . \PHP_EOL
  676. . 'ეს zip ფაილის კომენტარი' . \PHP_EOL
  677. . 'このzipファイルにコメント' . \PHP_EOL
  678. . 'ความคิดเห็นนี้ไฟล์ซิป';
  679. $zipFile = new ZipFile();
  680. $zipFile->setArchiveComment($comment);
  681. $zipFile->addFile(__FILE__);
  682. $zipFile->saveAsFile($this->outputFilename);
  683. $zipFile->close();
  684. static::assertCorrectZipArchive($this->outputFilename);
  685. $zipFile->openFile($this->outputFilename);
  686. static::assertSame($zipFile->getArchiveComment(), $comment);
  687. $zipFile->setArchiveComment(null); // remove archive comment
  688. $zipFile->saveAsFile($this->outputFilename);
  689. $zipFile->close();
  690. static::assertCorrectZipArchive($this->outputFilename);
  691. // check empty comment
  692. $zipFile->openFile($this->outputFilename);
  693. static::assertNull($zipFile->getArchiveComment());
  694. $zipFile->close();
  695. }
  696. /**
  697. * Test very long archive comment.
  698. */
  699. public function testVeryLongArchiveComment()
  700. {
  701. $this->expectException(InvalidArgumentException::class);
  702. $comment = 'Very long comment' . \PHP_EOL
  703. . 'Очень длинный комментарий' . \PHP_EOL;
  704. $comment = str_repeat($comment, ceil(0xffff / \strlen($comment)) + \strlen($comment) + 1);
  705. $zipFile = new ZipFile();
  706. $zipFile->setArchiveComment($comment);
  707. }
  708. /**
  709. * Test zip entry comment.
  710. *
  711. * @throws ZipException
  712. * @throws \Exception
  713. */
  714. public function testEntryComment()
  715. {
  716. $entries = [
  717. '文件1.txt' => [
  718. 'data' => random_bytes(255),
  719. 'comment' => '這是註釋的條目。',
  720. ],
  721. 'file2.txt' => [
  722. 'data' => random_bytes(255),
  723. 'comment' => null,
  724. ],
  725. 'file3.txt' => [
  726. 'data' => random_bytes(255),
  727. 'comment' => random_bytes(255),
  728. ],
  729. 'file4.txt' => [
  730. 'data' => random_bytes(255),
  731. 'comment' => 'Комментарий файла',
  732. ],
  733. 'file5.txt' => [
  734. 'data' => random_bytes(255),
  735. 'comment' => 'ไฟล์แสดงความคิดเห็น',
  736. ],
  737. 'file6 emoji 🙍🏼.txt' => [
  738. 'data' => random_bytes(255),
  739. 'comment' => 'Emoji comment file - 😀 ⛈ ❤️ 🤴🏽',
  740. ],
  741. ];
  742. // create archive with entry comments
  743. $zipFile = new ZipFile();
  744. foreach ($entries as $entryName => $item) {
  745. $zipFile->addFromString($entryName, $item['data']);
  746. $zipFile->setEntryComment($entryName, $item['comment']);
  747. }
  748. $zipFile->saveAsFile($this->outputFilename);
  749. $zipFile->close();
  750. static::assertCorrectZipArchive($this->outputFilename);
  751. // check and modify comments
  752. $zipFile->openFile($this->outputFilename);
  753. foreach ($zipFile->getListFiles() as $entryName) {
  754. $entriesItem = $entries[$entryName];
  755. static::assertNotEmpty($entriesItem);
  756. static::assertSame($zipFile[$entryName], $entriesItem['data']);
  757. static::assertSame($zipFile->getEntryComment($entryName), (string) $entriesItem['comment']);
  758. }
  759. // modify comment
  760. $entries['file5.txt']['comment'] = random_int(1, 100000000);
  761. $zipFile->setEntryComment('file5.txt', $entries['file5.txt']['comment']);
  762. $zipFile->saveAsFile($this->outputFilename);
  763. $zipFile->close();
  764. static::assertCorrectZipArchive($this->outputFilename);
  765. // check modify comments
  766. $zipFile->openFile($this->outputFilename);
  767. foreach ($entries as $entryName => $entriesItem) {
  768. static::assertTrue(isset($zipFile[$entryName]));
  769. static::assertSame($zipFile->getEntryComment($entryName), (string) $entriesItem['comment']);
  770. static::assertSame($zipFile[$entryName], $entriesItem['data']);
  771. }
  772. $zipFile->close();
  773. }
  774. /**
  775. * Test zip entry very long comment.
  776. *
  777. * @throws ZipException
  778. */
  779. public function testVeryLongEntryComment()
  780. {
  781. $this->expectException(InvalidArgumentException::class);
  782. $this->expectExceptionMessage('Comment too long');
  783. $comment = 'Very long comment' . \PHP_EOL
  784. . 'Очень длинный комментарий' . \PHP_EOL;
  785. $comment = str_repeat($comment, ceil(0xffff / \strlen($comment)) + \strlen($comment) + 1);
  786. $zipFile = new ZipFile();
  787. $zipFile->addFile(__FILE__, 'test');
  788. $zipFile->setEntryComment('test', $comment);
  789. }
  790. /**
  791. * @throws ZipException
  792. */
  793. public function testSetEntryCommentNotFoundEntry()
  794. {
  795. $this->expectException(ZipEntryNotFoundException::class);
  796. $zipFile = new ZipFile();
  797. $zipFile->setEntryComment('test', 'comment');
  798. }
  799. /**
  800. * Test all available support compression methods.
  801. *
  802. * @throws ZipException
  803. * @throws \Exception
  804. */
  805. public function testCompressionMethod()
  806. {
  807. $entries = [
  808. '1' => [
  809. 'data' => random_bytes(255),
  810. 'method' => ZipCompressionMethod::STORED,
  811. 'expected' => 'Stored',
  812. ],
  813. '2' => [
  814. 'data' => random_bytes(255),
  815. 'method' => ZipCompressionMethod::DEFLATED,
  816. 'expected' => 'Deflated',
  817. ],
  818. ];
  819. if (\extension_loaded('bz2')) {
  820. $entries['3'] = [
  821. 'data' => random_bytes(255),
  822. 'method' => ZipCompressionMethod::BZIP2,
  823. 'expected' => 'BZIP2',
  824. ];
  825. }
  826. $zipFile = new ZipFile();
  827. foreach ($entries as $entryName => $item) {
  828. $zipFile->addFromString($entryName, $item['data'], $item['method']);
  829. }
  830. $zipFile->saveAsFile($this->outputFilename);
  831. $zipFile->close();
  832. static::assertCorrectZipArchive($this->outputFilename);
  833. $zipFile->openFile($this->outputFilename);
  834. $zipFile->setCompressionLevel(ZipCompressionLevel::MAXIMUM);
  835. $zipAllInfo = $zipFile->getAllInfo();
  836. foreach ($zipAllInfo as $entryName => $info) {
  837. static::assertSame($zipFile[$entryName], $entries[$entryName]['data']);
  838. static::assertSame($info->getMethodName(), $entries[$entryName]['expected']);
  839. $entryInfo = $zipFile->getEntryInfo($entryName);
  840. static::assertEquals($entryInfo, $info);
  841. }
  842. $zipFile->close();
  843. }
  844. /**
  845. * @dataProvider provideInvalidCompressionLevels
  846. *
  847. * @param int $compressionLevel
  848. */
  849. public function testSetInvalidCompressionLevel($compressionLevel)
  850. {
  851. $this->expectException(
  852. InvalidArgumentException::class
  853. );
  854. $this->expectExceptionMessage(
  855. 'Invalid compression level. Minimum level 1. Maximum level 9'
  856. );
  857. $zipFile = new ZipFile();
  858. $zipFile['file 1'] = 'contents';
  859. $zipFile->setCompressionLevel($compressionLevel);
  860. }
  861. /**
  862. * @return array
  863. */
  864. public function provideInvalidCompressionLevels()
  865. {
  866. return [
  867. [-10],
  868. [-2],
  869. [10],
  870. [0xffff],
  871. ];
  872. }
  873. /**
  874. * Test extract all files.
  875. *
  876. * @throws ZipException
  877. * @throws \Exception
  878. */
  879. public function testExtract()
  880. {
  881. $entries = [
  882. 'test1.txt' => random_bytes(255),
  883. 'test2.txt' => random_bytes(255),
  884. 'test/test 2/test3.txt' => random_bytes(255),
  885. 'test empty/dir/' => null,
  886. ];
  887. $zipFile = new ZipFile();
  888. foreach ($entries as $entryName => $contents) {
  889. if ($contents === null) {
  890. $zipFile->addEmptyDir($entryName);
  891. } else {
  892. $zipFile->addFromString($entryName, $contents);
  893. }
  894. }
  895. $zipFile->saveAsFile($this->outputFilename);
  896. $zipFile->close();
  897. static::assertTrue(mkdir($this->outputDirname, 0755, true));
  898. $zipFile->openFile($this->outputFilename);
  899. $zipFile->extractTo($this->outputDirname, null, [], $extractedEntries);
  900. foreach ($entries as $entryName => $contents) {
  901. $name = $entryName;
  902. if (\DIRECTORY_SEPARATOR === '\\') {
  903. $name = str_replace('/', '\\', $name);
  904. }
  905. $fullExtractedFilename = $this->outputDirname . \DIRECTORY_SEPARATOR . $name;
  906. static::assertTrue(
  907. isset($extractedEntries[$fullExtractedFilename]),
  908. 'No extract info for ' . $fullExtractedFilename
  909. );
  910. if ($contents === null) {
  911. static::assertDirectoryExists($fullExtractedFilename);
  912. static::assertTrue(FilesUtil::isEmptyDir($fullExtractedFilename));
  913. } else {
  914. static::assertTrue(is_file($fullExtractedFilename));
  915. $contents = file_get_contents($fullExtractedFilename);
  916. static::assertSame($contents, $contents);
  917. }
  918. /** @var ZipEntry $entry */
  919. $entry = $extractedEntries[$fullExtractedFilename];
  920. static::assertSame($entry->getName(), $entryName);
  921. }
  922. $zipFile->close();
  923. }
  924. /**
  925. * Test extract some files.
  926. *
  927. * @throws ZipException
  928. * @throws \Exception
  929. */
  930. public function testExtractSomeFiles()
  931. {
  932. $entries = [
  933. 'test1.txt' => random_bytes(255),
  934. 'test2.txt' => random_bytes(255),
  935. 'test3.txt' => random_bytes(255),
  936. 'test4.txt' => random_bytes(255),
  937. 'test5.txt' => random_bytes(255),
  938. 'test/test/test.txt' => random_bytes(255),
  939. 'test/test/test 2.txt' => random_bytes(255),
  940. 'test empty/dir/' => null,
  941. 'test empty/dir2/' => null,
  942. ];
  943. $extractEntries = [
  944. 'test1.txt',
  945. 'test3.txt',
  946. 'test5.txt',
  947. 'test/test/test 2.txt',
  948. 'test empty/dir2/',
  949. ];
  950. static::assertTrue(mkdir($this->outputDirname, 0755, true));
  951. $zipFile = new ZipFile();
  952. $zipFile->addAll($entries);
  953. $zipFile->saveAsFile($this->outputFilename);
  954. $zipFile->close();
  955. $zipFile->openFile($this->outputFilename);
  956. $zipFile->extractTo($this->outputDirname, $extractEntries);
  957. foreach ($entries as $entryName => $value) {
  958. $fullExtractFilename = $this->outputDirname . \DIRECTORY_SEPARATOR . $entryName;
  959. if (\in_array($entryName, $extractEntries, true)) {
  960. if ($value === null) {
  961. static::assertDirectoryExists($fullExtractFilename);
  962. static::assertTrue(FilesUtil::isEmptyDir($fullExtractFilename));
  963. } else {
  964. static::assertTrue(is_file($fullExtractFilename));
  965. $contents = file_get_contents($fullExtractFilename);
  966. static::assertEquals($contents, $value);
  967. }
  968. } elseif ($value === null) {
  969. static::assertDirectoryDoesNotExist($fullExtractFilename);
  970. } else {
  971. static::assertFalse(is_file($fullExtractFilename));
  972. }
  973. }
  974. static::assertFalse(is_file($this->outputDirname . \DIRECTORY_SEPARATOR . 'test/test/test.txt'));
  975. $zipFile->extractTo($this->outputDirname, 'test/test/test.txt');
  976. static::assertTrue(is_file($this->outputDirname . \DIRECTORY_SEPARATOR . 'test/test/test.txt'));
  977. $zipFile->close();
  978. }
  979. /**
  980. * @throws ZipException
  981. */
  982. public function testExtractFail()
  983. {
  984. $this->expectException(ZipException::class);
  985. $this->expectExceptionMessage('not found');
  986. $zipFile = new ZipFile();
  987. $zipFile['file'] = 'content';
  988. $zipFile->saveAsFile($this->outputFilename);
  989. $zipFile->close();
  990. $zipFile->openFile($this->outputFilename);
  991. $zipFile->extractTo('path/to/path');
  992. }
  993. /**
  994. * @throws ZipException
  995. */
  996. public function testExtractFail2()
  997. {
  998. $this->expectException(ZipException::class);
  999. $this->expectExceptionMessage('Destination is not directory');
  1000. $zipFile = new ZipFile();
  1001. $zipFile['file'] = 'content';
  1002. $zipFile->saveAsFile($this->outputFilename);
  1003. $zipFile->close();
  1004. $zipFile->openFile($this->outputFilename);
  1005. $zipFile->extractTo($this->outputFilename);
  1006. }
  1007. /**
  1008. * @throws ZipException
  1009. */
  1010. public function testExtractFail3()
  1011. {
  1012. static::skipTestForWindows();
  1013. static::skipTestForRootUser();
  1014. $this->expectException(ZipException::class);
  1015. $this->expectExceptionMessage('Destination is not writable directory');
  1016. $zipFile = new ZipFile();
  1017. $zipFile['file'] = 'content';
  1018. $zipFile->saveAsFile($this->outputFilename);
  1019. $zipFile->close();
  1020. static::assertTrue(mkdir($this->outputDirname, 0444, true));
  1021. static::assertTrue(chmod($this->outputDirname, 0444));
  1022. $zipFile->openFile($this->outputFilename);
  1023. $zipFile->extractTo($this->outputDirname);
  1024. }
  1025. /**
  1026. * @noinspection OnlyWritesOnParameterInspection
  1027. */
  1028. public function testAddFromArrayAccessNullName()
  1029. {
  1030. $this->expectException(
  1031. InvalidArgumentException::class
  1032. );
  1033. $this->expectExceptionMessage(
  1034. 'Key must not be null, but must contain the name of the zip entry.'
  1035. );
  1036. $zipFile = new ZipFile();
  1037. $zipFile[null] = 'content';
  1038. }
  1039. /**
  1040. * @noinspection OnlyWritesOnParameterInspection
  1041. */
  1042. public function testAddFromArrayAccessEmptyName()
  1043. {
  1044. $this->expectException(
  1045. InvalidArgumentException::class
  1046. );
  1047. $this->expectExceptionMessage(
  1048. 'Key is empty, but must contain the name of the zip entry.'
  1049. );
  1050. $zipFile = new ZipFile();
  1051. $zipFile[''] = 'content';
  1052. }
  1053. /**
  1054. * @throws ZipException
  1055. */
  1056. public function testAddFromStringNullContents()
  1057. {
  1058. $this->expectException(InvalidArgumentException::class);
  1059. $this->expectExceptionMessage('Contents is null');
  1060. $zipFile = new ZipFile();
  1061. $zipFile->addFromString('file', null);
  1062. }
  1063. /**
  1064. * @throws ZipException
  1065. */
  1066. public function testAddFromStringNullEntryName()
  1067. {
  1068. $this->expectException(InvalidArgumentException::class);
  1069. $this->expectExceptionMessage('Entry name is null');
  1070. $zipFile = new ZipFile();
  1071. $zipFile->addFromString(null, 'contents');
  1072. }
  1073. /**
  1074. * @throws ZipException
  1075. */
  1076. public function testAddFromStringUnsupportedMethod()
  1077. {
  1078. $this->expectException(
  1079. ZipUnsupportMethodException::class
  1080. );
  1081. $this->expectExceptionMessage(
  1082. 'Compression method 99 (AES Encryption) is not supported.'
  1083. );
  1084. $zipFile = new ZipFile();
  1085. $zipFile->addFromString('file', 'contents', ZipCompressionMethod::WINZIP_AES);
  1086. $zipFile->outputAsString();
  1087. }
  1088. /**
  1089. * @throws ZipException
  1090. */
  1091. public function testAddFromStringEmptyEntryName()
  1092. {
  1093. $this->expectException(InvalidArgumentException::class);
  1094. $this->expectExceptionMessage('Empty entry name');
  1095. $zipFile = new ZipFile();
  1096. $zipFile->addFromString('', 'contents');
  1097. }
  1098. /**
  1099. * Test compression method from add string.
  1100. *
  1101. * @throws ZipException
  1102. */
  1103. public function testAddFromStringCompressionMethod()
  1104. {
  1105. $fileStored = sys_get_temp_dir() . '/zip-stored.txt';
  1106. $fileDeflated = sys_get_temp_dir() . '/zip-deflated.txt';
  1107. static::assertNotFalse(file_put_contents($fileStored, 'content'));
  1108. static::assertNotFalse(file_put_contents($fileDeflated, str_repeat('content', 200)));
  1109. $zipFile = new ZipFile();
  1110. $zipFile->addFromString(basename($fileStored), file_get_contents($fileStored));
  1111. $zipFile->addFromString(basename($fileDeflated), file_get_contents($fileDeflated));
  1112. $zipFile->saveAsFile($this->outputFilename);
  1113. $zipFile->close();
  1114. unlink($fileStored);
  1115. unlink($fileDeflated);
  1116. $zipFile->openFile($this->outputFilename);
  1117. $infoStored = $zipFile->getEntryInfo(basename($fileStored));
  1118. $infoDeflated = $zipFile->getEntryInfo(basename($fileDeflated));
  1119. static::assertSame($infoStored->getMethodName(), 'Stored');
  1120. static::assertSame($infoDeflated->getMethodName(), 'Deflated');
  1121. $zipFile->close();
  1122. }
  1123. /**
  1124. * @throws ZipException
  1125. */
  1126. public function testAddFromStreamInvalidResource()
  1127. {
  1128. $this->expectException(InvalidArgumentException::class);
  1129. $this->expectExceptionMessage('Stream is not resource');
  1130. $zipFile = new ZipFile();
  1131. /** @noinspection PhpParamsInspection */
  1132. $zipFile->addFromStream('invalid resource', 'name');
  1133. }
  1134. /**
  1135. * @throws ZipException
  1136. */
  1137. public function testAddFromStreamEmptyEntryName()
  1138. {
  1139. $this->expectException(InvalidArgumentException::class);
  1140. $this->expectExceptionMessage('Empty entry name');
  1141. $handle = fopen(__FILE__, 'rb');
  1142. $zipFile = new ZipFile();
  1143. $zipFile->addFromStream($handle, '');
  1144. }
  1145. /**
  1146. * @throws ZipException
  1147. */
  1148. public function testAddFromStreamUnsupportedMethod()
  1149. {
  1150. $this->expectException(
  1151. ZipUnsupportMethodException::class
  1152. );
  1153. $this->expectExceptionMessage(
  1154. 'Compression method 99 (AES Encryption) is not supported.'
  1155. );
  1156. $handle = fopen(__FILE__, 'rb');
  1157. $zipFile = new ZipFile();
  1158. $zipFile->addFromStream($handle, basename(__FILE__), ZipCompressionMethod::WINZIP_AES);
  1159. $zipFile->outputAsString();
  1160. }
  1161. /**
  1162. * Test compression method from add stream.
  1163. *
  1164. * @throws ZipException
  1165. */
  1166. public function testAddFromStreamCompressionMethod()
  1167. {
  1168. $fileStored = sys_get_temp_dir() . '/zip-stored.txt';
  1169. $fileDeflated = sys_get_temp_dir() . '/zip-deflated.txt';
  1170. static::assertNotFalse(file_put_contents($fileStored, 'content'));
  1171. static::assertNotFalse(file_put_contents($fileDeflated, str_repeat('content', 200)));
  1172. $fpStored = fopen($fileStored, 'rb');
  1173. $fpDeflated = fopen($fileDeflated, 'rb');
  1174. $zipFile = new ZipFile();
  1175. $zipFile->addFromStream($fpStored, basename($fileStored));
  1176. $zipFile->addFromStream($fpDeflated, basename($fileDeflated));
  1177. $zipFile->saveAsFile($this->outputFilename);
  1178. $zipFile->close();
  1179. unlink($fileStored);
  1180. unlink($fileDeflated);
  1181. $zipFile->openFile($this->outputFilename);
  1182. $infoStored = $zipFile->getEntryInfo(basename($fileStored));
  1183. $infoDeflated = $zipFile->getEntryInfo(basename($fileDeflated));
  1184. static::assertSame($infoStored->getMethodName(), 'Stored');
  1185. static::assertSame($infoDeflated->getMethodName(), 'Deflated');
  1186. $zipFile->close();
  1187. }
  1188. /**
  1189. * @throws ZipException
  1190. */
  1191. public function testAddFileNullFileName()
  1192. {
  1193. $this->expectException(InvalidArgumentException::class);
  1194. $this->expectExceptionMessage('Filename is null');
  1195. $zipFile = new ZipFile();
  1196. $zipFile->addFile(null);
  1197. }
  1198. /**
  1199. * @throws ZipException
  1200. */
  1201. public function testAddFileCantExists()
  1202. {
  1203. $this->expectException(InvalidArgumentException::class);
  1204. $this->expectExceptionMessage('File path/to/file is not readable');
  1205. $zipFile = new ZipFile();
  1206. $zipFile->addFile('path/to/file');
  1207. }
  1208. /**
  1209. * @throws ZipException
  1210. */
  1211. public function testAddFileUnsupportedMethod()
  1212. {
  1213. $this->expectException(
  1214. ZipUnsupportMethodException::class
  1215. );
  1216. $this->expectExceptionMessage(
  1217. 'Compression method 99 (AES Encryption) is not supported.'
  1218. );
  1219. $zipFile = new ZipFile();
  1220. $zipFile->addFile(__FILE__, null, ZipCompressionMethod::WINZIP_AES);
  1221. $zipFile->outputAsString();
  1222. }
  1223. /**
  1224. * @throws ZipException
  1225. */
  1226. public function testAddFileCannotOpen()
  1227. {
  1228. static::skipTestForWindows();
  1229. static::skipTestForRootUser();
  1230. $this->expectException(InvalidArgumentException::class);
  1231. $this->expectExceptionMessage('is not readable');
  1232. static::assertNotFalse(file_put_contents($this->outputFilename, ''));
  1233. static::assertTrue(chmod($this->outputFilename, 0244));
  1234. $zipFile = new ZipFile();
  1235. $zipFile->addFile($this->outputFilename);
  1236. }
  1237. /**
  1238. * @throws ZipException
  1239. */
  1240. public function testAddDirNullDirname()
  1241. {
  1242. $this->expectException(InvalidArgumentException::class);
  1243. $this->expectExceptionMessage('Input dir is null');
  1244. $zipFile = new ZipFile();
  1245. $zipFile->addDir(null);
  1246. }
  1247. /**
  1248. * @throws ZipException
  1249. */
  1250. public function testAddDirEmptyDirname()
  1251. {
  1252. $this->expectException(InvalidArgumentException::class);
  1253. $this->expectExceptionMessage('The input directory is not specified');
  1254. $zipFile = new ZipFile();
  1255. $zipFile->addDir('');
  1256. }
  1257. /**
  1258. * @throws ZipException
  1259. */
  1260. public function testAddDirCantExists()
  1261. {
  1262. $this->expectException(InvalidArgumentException::class);
  1263. $this->expectExceptionMessage('does not exist');
  1264. $zipFile = new ZipFile();
  1265. $zipFile->addDir(uniqid('', false));
  1266. }
  1267. /**
  1268. * @throws ZipException
  1269. */
  1270. public function testAddDirRecursiveNullDirname()
  1271. {
  1272. $this->expectException(InvalidArgumentException::class);
  1273. $this->expectExceptionMessage('Input dir is null');
  1274. $zipFile = new ZipFile();
  1275. $zipFile->addDirRecursive(null);
  1276. }
  1277. /**
  1278. * @throws ZipException
  1279. */
  1280. public function testAddDirRecursiveEmptyDirname()
  1281. {
  1282. $this->expectException(InvalidArgumentException::class);
  1283. $this->expectExceptionMessage('The input directory is not specified');
  1284. $zipFile = new ZipFile();
  1285. $zipFile->addDirRecursive('');
  1286. }
  1287. /**
  1288. * @throws ZipException
  1289. */
  1290. public function testAddDirRecursiveCantExists()
  1291. {
  1292. $this->expectException(InvalidArgumentException::class);
  1293. $this->expectExceptionMessage('does not exist');
  1294. $zipFile = new ZipFile();
  1295. $zipFile->addDirRecursive(uniqid('', false));
  1296. }
  1297. /**
  1298. * @throws ZipException
  1299. */
  1300. public function testAddFilesFromGlobNull()
  1301. {
  1302. $this->expectException(InvalidArgumentException::class);
  1303. $this->expectExceptionMessage('Input dir is null');
  1304. $zipFile = new ZipFile();
  1305. $zipFile->addFilesFromGlob(null, '*.png');
  1306. }
  1307. /**
  1308. * @throws ZipException
  1309. */
  1310. public function testAddFilesFromGlobEmpty()
  1311. {
  1312. $this->expectException(InvalidArgumentException::class);
  1313. $this->expectExceptionMessage('The input directory is not specified');
  1314. $zipFile = new ZipFile();
  1315. $zipFile->addFilesFromGlob('', '*.png');
  1316. }
  1317. /**
  1318. * @throws ZipException
  1319. */
  1320. public function testAddFilesFromGlobCantExists()
  1321. {
  1322. $this->expectException(InvalidArgumentException::class);
  1323. $this->expectExceptionMessage('does not exist');
  1324. $zipFile = new ZipFile();
  1325. $zipFile->addFilesFromGlob('path/to/path', '*.png');
  1326. }
  1327. /**
  1328. * @throws ZipException
  1329. */
  1330. public function testAddFilesFromGlobNullPattern()
  1331. {
  1332. $this->expectException(InvalidArgumentException::class);
  1333. $this->expectExceptionMessage('The glob pattern is not specified');
  1334. $zipFile = new ZipFile();
  1335. $zipFile->addFilesFromGlob(__DIR__, null);
  1336. }
  1337. /**
  1338. * @throws ZipException
  1339. */
  1340. public function testAddFilesFromGlobEmptyPattern()
  1341. {
  1342. $this->expectException(InvalidArgumentException::class);
  1343. $this->expectExceptionMessage('The glob pattern is not specified');
  1344. $zipFile = new ZipFile();
  1345. $zipFile->addFilesFromGlob(__DIR__, '');
  1346. }
  1347. /**
  1348. * @throws ZipException
  1349. */
  1350. public function testAddFilesFromGlobRecursiveNull()
  1351. {
  1352. $this->expectException(InvalidArgumentException::class);
  1353. $this->expectExceptionMessage('Input dir is null');
  1354. $zipFile = new ZipFile();
  1355. $zipFile->addFilesFromGlobRecursive(null, '*.png');
  1356. }
  1357. /**
  1358. * @throws ZipException
  1359. */
  1360. public function testAddFilesFromGlobRecursiveEmpty()
  1361. {
  1362. $this->expectException(InvalidArgumentException::class);
  1363. $this->expectExceptionMessage('The input directory is not specified');
  1364. $zipFile = new ZipFile();
  1365. $zipFile->addFilesFromGlobRecursive('', '*.png');
  1366. }
  1367. /**
  1368. * @throws ZipException
  1369. */
  1370. public function testAddFilesFromGlobRecursiveCantExists()
  1371. {
  1372. $this->expectException(InvalidArgumentException::class);
  1373. $this->expectExceptionMessage('does not exist');
  1374. $zipFile = new ZipFile();
  1375. $zipFile->addFilesFromGlobRecursive('path/to/path', '*.png');
  1376. }
  1377. /**
  1378. * @throws ZipException
  1379. */
  1380. public function testAddFilesFromGlobRecursiveNullPattern()
  1381. {
  1382. $this->expectException(InvalidArgumentException::class);
  1383. $this->expectExceptionMessage('The glob pattern is not specified');
  1384. $zipFile = new ZipFile();
  1385. $zipFile->addFilesFromGlobRecursive(__DIR__, null);
  1386. }
  1387. /**
  1388. * @throws ZipException
  1389. */
  1390. public function testAddFilesFromGlobRecursiveEmptyPattern()
  1391. {
  1392. $this->expectException(InvalidArgumentException::class);
  1393. $this->expectExceptionMessage('The glob pattern is not specified');
  1394. $zipFile = new ZipFile();
  1395. $zipFile->addFilesFromGlobRecursive(__DIR__, '');
  1396. }
  1397. /**
  1398. * @throws ZipException
  1399. */
  1400. public function testAddFilesFromRegexDirectoryNull()
  1401. {
  1402. $this->expectException(InvalidArgumentException::class);
  1403. $this->expectExceptionMessage('The input directory is not specified');
  1404. $zipFile = new ZipFile();
  1405. $zipFile->addFilesFromRegex(null, '~\.png$~i');
  1406. }
  1407. /**
  1408. * @throws ZipException
  1409. */
  1410. public function testAddFilesFromRegexDirectoryEmpty()
  1411. {
  1412. $this->expectException(InvalidArgumentException::class);
  1413. $this->expectExceptionMessage('The input directory is not specified');
  1414. $zipFile = new ZipFile();
  1415. $zipFile->addFilesFromRegex('', '~\.png$~i');
  1416. }
  1417. /**
  1418. * @throws ZipException
  1419. */
  1420. public function testAddFilesFromRegexCantExists()
  1421. {
  1422. $this->expectException(InvalidArgumentException::class);
  1423. $this->expectExceptionMessage('does not exist');
  1424. $zipFile = new ZipFile();
  1425. $zipFile->addFilesFromRegex('path/to/path', '~\.png$~i');
  1426. }
  1427. /**
  1428. * @throws ZipException
  1429. */
  1430. public function testAddFilesFromRegexNullPattern()
  1431. {
  1432. $this->expectException(InvalidArgumentException::class);
  1433. $this->expectExceptionMessage('The regex pattern is not specified');
  1434. $zipFile = new ZipFile();
  1435. $zipFile->addFilesFromRegex(__DIR__, null);
  1436. }
  1437. /**
  1438. * @throws ZipException
  1439. */
  1440. public function testAddFilesFromRegexEmptyPattern()
  1441. {
  1442. $this->expectException(InvalidArgumentException::class);
  1443. $this->expectExceptionMessage('The regex pattern is not specified');
  1444. $zipFile = new ZipFile();
  1445. $zipFile->addFilesFromRegex(__DIR__, '');
  1446. }
  1447. /**
  1448. * @throws ZipException
  1449. */
  1450. public function testAddFilesFromRegexRecursiveDirectoryNull()
  1451. {
  1452. $this->expectException(InvalidArgumentException::class);
  1453. $this->expectExceptionMessage('The input directory is not specified');
  1454. $zipFile = new ZipFile();
  1455. $zipFile->addFilesFromRegexRecursive(null, '~\.png$~i');
  1456. }
  1457. /**
  1458. * @throws ZipException
  1459. */
  1460. public function testAddFilesFromRegexRecursiveEmpty()
  1461. {
  1462. $this->expectException(InvalidArgumentException::class);
  1463. $this->expectExceptionMessage('The input directory is not specified');
  1464. $zipFile = new ZipFile();
  1465. $zipFile->addFilesFromRegexRecursive('', '~\.png$~i');
  1466. }
  1467. /**
  1468. * @throws ZipException
  1469. */
  1470. public function testAddFilesFromRegexRecursiveCantExists()
  1471. {
  1472. $this->expectException(InvalidArgumentException::class);
  1473. $this->expectExceptionMessage('does not exist');
  1474. $zipFile = new ZipFile();
  1475. $zipFile->addFilesFromGlobRecursive('path/to/path', '~\.png$~i');
  1476. }
  1477. /**
  1478. * @throws ZipException
  1479. */
  1480. public function testAddFilesFromRegexRecursiveNullPattern()
  1481. {
  1482. $this->expectException(InvalidArgumentException::class);
  1483. $this->expectExceptionMessage('The regex pattern is not specified');
  1484. $zipFile = new ZipFile();
  1485. $zipFile->addFilesFromRegexRecursive(__DIR__, null);
  1486. }
  1487. /**
  1488. * @throws ZipException
  1489. */
  1490. public function testAddFilesFromRegexRecursiveEmptyPattern()
  1491. {
  1492. $this->expectException(InvalidArgumentException::class);
  1493. $this->expectExceptionMessage('The regex pattern is not specified');
  1494. $zipFile = new ZipFile();
  1495. $zipFile->addFilesFromRegexRecursive(__DIR__, '');
  1496. }
  1497. /**
  1498. * @throws ZipException
  1499. */
  1500. public function testSaveAsStreamBadStream()
  1501. {
  1502. $this->expectException(InvalidArgumentException::class);
  1503. $this->expectExceptionMessage('handle is not resource');
  1504. $zipFile = new ZipFile();
  1505. /** @noinspection PhpParamsInspection */
  1506. $zipFile->saveAsStream('bad stream');
  1507. }
  1508. /**
  1509. * @throws ZipException
  1510. */
  1511. public function testSaveAsFileNotWritable()
  1512. {
  1513. static::skipTestForWindows();
  1514. static::skipTestForRootUser();
  1515. static::assertTrue(mkdir($this->outputDirname, 0444, true));
  1516. static::assertTrue(chmod($this->outputDirname, 0444));
  1517. $this->outputFilename = $this->outputDirname . \DIRECTORY_SEPARATOR . basename($this->outputFilename);
  1518. $this->expectException(InvalidArgumentException::class);
  1519. $this->expectExceptionMessageMatches('~Cannot open ".*?" for writing.~');
  1520. $zipFile = new ZipFile();
  1521. $zipFile->saveAsFile($this->outputFilename);
  1522. }
  1523. /**
  1524. * Test `ZipFile` implemented \ArrayAccess, \Countable and |iterator.
  1525. *
  1526. * @throws ZipException
  1527. * @throws \Exception
  1528. */
  1529. public function testZipFileArrayAccessAndCountableAndIterator()
  1530. {
  1531. $files = [];
  1532. $numFiles = random_int(20, 100);
  1533. for ($i = 0; $i < $numFiles; $i++) {
  1534. $files['file' . $i . '.txt'] = random_bytes(255);
  1535. }
  1536. $compressionMethods = [ZipCompressionMethod::STORED, ZipCompressionMethod::DEFLATED];
  1537. if (\extension_loaded('bz2')) {
  1538. $compressionMethods[] = ZipCompressionMethod::BZIP2;
  1539. }
  1540. $zipFile = new ZipFile();
  1541. $zipFile->setCompressionLevel(ZipCompressionLevel::SUPER_FAST);
  1542. $i = 0;
  1543. $countMethods = \count($compressionMethods);
  1544. foreach ($files as $entryName => $content) {
  1545. $compressionMethod = $compressionMethods[$i++ % $countMethods];
  1546. $zipFile->addFromString($entryName, $content, $compressionMethod);
  1547. }
  1548. $zipFile->saveAsFile($this->outputFilename);
  1549. $zipFile->close();
  1550. static::assertCorrectZipArchive($this->outputFilename);
  1551. $zipFile->openFile($this->outputFilename);
  1552. // Test \Countable
  1553. static::assertSame($zipFile->count(), $numFiles);
  1554. static::assertSame(\count($zipFile), $numFiles);
  1555. // Test \ArrayAccess
  1556. reset($files);
  1557. foreach ($zipFile as $entryName => $content) {
  1558. static::assertSame($entryName, key($files));
  1559. static::assertSame($content, current($files));
  1560. next($files);
  1561. }
  1562. // Test \Iterator
  1563. reset($files);
  1564. $iterator = new \ArrayIterator($zipFile);
  1565. $iterator->rewind();
  1566. while ($iterator->valid()) {
  1567. $key = $iterator->key();
  1568. $value = $iterator->current();
  1569. static::assertSame($key, key($files));
  1570. static::assertSame($value, current($files));
  1571. next($files);
  1572. $iterator->next();
  1573. }
  1574. $zipFile->close();
  1575. $zipFile = new ZipFile();
  1576. $zipFile['file1.txt'] = 'content 1';
  1577. $zipFile['dir/file2.txt'] = 'content 1';
  1578. $zipFile['dir/empty dir/'] = null;
  1579. $zipFile->saveAsFile($this->outputFilename);
  1580. $zipFile->close();
  1581. static::assertCorrectZipArchive($this->outputFilename);
  1582. $zipFile->openFile($this->outputFilename);
  1583. static::assertTrue(isset($zipFile['file1.txt']));
  1584. static::assertTrue(isset($zipFile['dir/file2.txt']));
  1585. static::assertTrue(isset($zipFile['dir/empty dir/']));
  1586. static::assertFalse(isset($zipFile['dir/empty dir/2/']));
  1587. $zipFile['dir/empty dir/2/'] = null;
  1588. unset($zipFile['dir/file2.txt'], $zipFile['dir/empty dir/']);
  1589. $zipFile->saveAsFile($this->outputFilename);
  1590. $zipFile->close();
  1591. static::assertCorrectZipArchive($this->outputFilename);
  1592. $zipFile->openFile($this->outputFilename);
  1593. static::assertTrue(isset($zipFile['file1.txt']));
  1594. static::assertFalse(isset($zipFile['dir/file2.txt']));
  1595. static::assertFalse(isset($zipFile['dir/empty dir/']));
  1596. static::assertTrue(isset($zipFile['dir/empty dir/2/']));
  1597. $zipFile->close();
  1598. }
  1599. /**
  1600. * @throws ZipException
  1601. */
  1602. public function testArrayAccessAddFile()
  1603. {
  1604. $entryName = 'path/to/file.dat';
  1605. $entryNameStream = 'path/to/' . basename(__FILE__);
  1606. $zipFile = new ZipFile();
  1607. $zipFile[$entryName] = new \SplFileInfo(__FILE__);
  1608. $zipFile[$entryNameStream] = fopen(__FILE__, 'rb');
  1609. $zipFile->saveAsFile($this->outputFilename);
  1610. $zipFile->close();
  1611. static::assertCorrectZipArchive($this->outputFilename);
  1612. $zipFile->openFile($this->outputFilename);
  1613. static::assertSame(\count($zipFile), 2);
  1614. static::assertTrue(isset($zipFile[$entryName]));
  1615. static::assertTrue(isset($zipFile[$entryNameStream]));
  1616. static::assertSame($zipFile[$entryName], file_get_contents(__FILE__));
  1617. static::assertSame($zipFile[$entryNameStream], file_get_contents(__FILE__));
  1618. $zipFile->close();
  1619. }
  1620. /**
  1621. * @throws ZipEntryNotFoundException
  1622. * @throws ZipException
  1623. * @throws \Exception
  1624. */
  1625. public function testUnknownCompressionMethod()
  1626. {
  1627. $zipFile = new ZipFile();
  1628. $zipFile->addFromString('file', 'content', ZipEntry::UNKNOWN);
  1629. $zipFile->addFromString('file2', base64_encode(random_bytes(512)), ZipEntry::UNKNOWN);
  1630. static::assertSame($zipFile->getEntryInfo('file')->getMethodName(), 'Stored');
  1631. static::assertSame($zipFile->getEntryInfo('file2')->getMethodName(), 'Deflated');
  1632. $zipFile->saveAsFile($this->outputFilename);
  1633. $zipFile->close();
  1634. $zipFile->openFile($this->outputFilename);
  1635. static::assertSame($zipFile->getEntryInfo('file')->getMethodName(), 'Stored');
  1636. static::assertSame($zipFile->getEntryInfo('file2')->getMethodName(), 'Deflated');
  1637. $zipFile->close();
  1638. }
  1639. /**
  1640. * @throws ZipException
  1641. */
  1642. public function testAddEmptyDirNullName()
  1643. {
  1644. $this->expectException(InvalidArgumentException::class);
  1645. $this->expectExceptionMessage('Entry name is null');
  1646. $zipFile = new ZipFile();
  1647. $zipFile->addEmptyDir(null);
  1648. }
  1649. /**
  1650. * @throws ZipException
  1651. */
  1652. public function testAddEmptyDirEmptyName()
  1653. {
  1654. $this->expectException(InvalidArgumentException::class);
  1655. $this->expectExceptionMessage('Empty entry name');
  1656. $zipFile = new ZipFile();
  1657. $zipFile->addEmptyDir('');
  1658. }
  1659. public function testNotFoundEntry()
  1660. {
  1661. $this->expectException(ZipEntryNotFoundException::class);
  1662. $this->expectExceptionMessage('"bad entry name"');
  1663. $zipFile = new ZipFile();
  1664. $zipFile['bad entry name'];
  1665. }
  1666. /**
  1667. * Test rewrite input file.
  1668. *
  1669. * @throws ZipException
  1670. */
  1671. public function testRewriteFile()
  1672. {
  1673. $zipFile = new ZipFile();
  1674. $zipFile['file'] = 'content';
  1675. $zipFile['file2'] = 'content2';
  1676. static::assertSame(\count($zipFile), 2);
  1677. $zipFile
  1678. ->saveAsFile($this->outputFilename)
  1679. ->close()
  1680. ;
  1681. $md5file = md5_file($this->outputFilename);
  1682. $zipFile->openFile($this->outputFilename);
  1683. static::assertSame(\count($zipFile), 2);
  1684. static::assertTrue(isset($zipFile['file']));
  1685. static::assertTrue(isset($zipFile['file2']));
  1686. $zipFile['file3'] = 'content3';
  1687. static::assertSame(\count($zipFile), 3);
  1688. $zipFile = $zipFile->rewrite();
  1689. static::assertSame(\count($zipFile), 3);
  1690. static::assertTrue(isset($zipFile['file']));
  1691. static::assertTrue(isset($zipFile['file2']));
  1692. static::assertTrue(isset($zipFile['file3']));
  1693. $zipFile->close();
  1694. static::assertNotSame(md5_file($this->outputFilename), $md5file);
  1695. }
  1696. /**
  1697. * Test rewrite for string.
  1698. *
  1699. * @throws ZipException
  1700. */
  1701. public function testRewriteString()
  1702. {
  1703. $this->expectException(ZipException::class);
  1704. $this->expectExceptionMessage('Overwrite is only supported for open local files');
  1705. $zipFile = new ZipFile();
  1706. $zipFile['file'] = 'content';
  1707. $zipFile['file2'] = 'content2';
  1708. $zipFile->saveAsFile($this->outputFilename);
  1709. $zipFile->close();
  1710. $zipFile->openFromString(file_get_contents($this->outputFilename));
  1711. static::assertSame(\count($zipFile), 2);
  1712. static::assertTrue(isset($zipFile['file']));
  1713. static::assertTrue(isset($zipFile['file2']));
  1714. $zipFile['file3'] = 'content3';
  1715. $zipFile = $zipFile->rewrite();
  1716. static::assertSame(\count($zipFile), 3);
  1717. static::assertTrue(isset($zipFile['file']));
  1718. static::assertTrue(isset($zipFile['file2']));
  1719. static::assertTrue(isset($zipFile['file3']));
  1720. $zipFile->close();
  1721. }
  1722. /**
  1723. * @throws ZipException
  1724. */
  1725. public function testRewriteNullStream()
  1726. {
  1727. $this->expectException(ZipException::class);
  1728. $this->expectExceptionMessage('input stream is null');
  1729. $zipFile = new ZipFile();
  1730. $zipFile->rewrite();
  1731. }
  1732. /**
  1733. * Checks the ability to overwrite an open zip file with a relative path.
  1734. *
  1735. * @throws ZipException
  1736. */
  1737. public function testRewriteRelativeFile()
  1738. {
  1739. $zipFile = new ZipFile();
  1740. $zipFile['entry.txt'] = 'test';
  1741. $zipFile->saveAsFile($this->outputFilename);
  1742. $zipFile->close();
  1743. $outputDirname = \dirname($this->outputFilename);
  1744. static::assertTrue(chdir($outputDirname));
  1745. $relativeFilename = basename($this->outputFilename);
  1746. $zipFile->openFile($this->outputFilename);
  1747. $zipFile['entry2.txt'] = 'test';
  1748. $zipFile->saveAsFile($relativeFilename);
  1749. $zipFile->close();
  1750. self::assertCorrectZipArchive($this->outputFilename);
  1751. }
  1752. /**
  1753. * Checks the ability to overwrite an open zip file with a relative path.
  1754. *
  1755. * @throws ZipException
  1756. */
  1757. public function testRewriteDifferentWinDirectorySeparator()
  1758. {
  1759. if (\DIRECTORY_SEPARATOR !== '\\') {
  1760. static::markTestSkipped('Windows test only');
  1761. return;
  1762. }
  1763. $zipFile = new ZipFile();
  1764. $zipFile['entry.txt'] = 'test';
  1765. $zipFile->saveAsFile($this->outputFilename);
  1766. $zipFile->close();
  1767. $alternativeOutputFilename = str_replace('\\', '/', $this->outputFilename);
  1768. self::assertCorrectZipArchive($alternativeOutputFilename);
  1769. $zipFile->openFile($this->outputFilename);
  1770. $zipFile['entry2.txt'] = 'test';
  1771. $zipFile->saveAsFile($alternativeOutputFilename);
  1772. $zipFile->close();
  1773. self::assertCorrectZipArchive($alternativeOutputFilename);
  1774. $zipFile->openFile($this->outputFilename);
  1775. static::assertCount(2, $zipFile);
  1776. $zipFile->close();
  1777. }
  1778. /**
  1779. * @throws ZipException
  1780. */
  1781. public function testRewriteRelativeFile2()
  1782. {
  1783. $this->outputFilename = basename($this->outputFilename);
  1784. $zipFile = new ZipFile();
  1785. $zipFile['entry.txt'] = 'test';
  1786. $zipFile->saveAsFile($this->outputFilename);
  1787. $zipFile->close();
  1788. $absoluteOutputFilename = getcwd() . \DIRECTORY_SEPARATOR . $this->outputFilename;
  1789. self::assertCorrectZipArchive($absoluteOutputFilename);
  1790. $zipFile->openFile($this->outputFilename);
  1791. $zipFile['entry2.txt'] = 'test';
  1792. $zipFile->saveAsFile($absoluteOutputFilename);
  1793. $zipFile->close();
  1794. self::assertCorrectZipArchive($absoluteOutputFilename);
  1795. }
  1796. /**
  1797. * @throws ZipException
  1798. */
  1799. public function testFilename0()
  1800. {
  1801. $zipFile = new ZipFile();
  1802. $zipFile[0] = 0;
  1803. static::assertTrue(isset($zipFile[0]));
  1804. static::assertTrue(isset($zipFile['0']));
  1805. static::assertCount(1, $zipFile);
  1806. $zipFile
  1807. ->saveAsFile($this->outputFilename)
  1808. ->close()
  1809. ;
  1810. static::assertCorrectZipArchive($this->outputFilename);
  1811. $zipFile->openFile($this->outputFilename);
  1812. static::assertTrue(isset($zipFile[0]));
  1813. static::assertTrue(isset($zipFile['0']));
  1814. static::assertSame($zipFile['0'], '0');
  1815. static::assertCount(1, $zipFile);
  1816. $zipFile->close();
  1817. static::assertTrue(unlink($this->outputFilename));
  1818. $zipFile = new ZipFile();
  1819. $zipFile->addFromString(0, 0);
  1820. static::assertTrue(isset($zipFile[0]));
  1821. static::assertTrue(isset($zipFile['0']));
  1822. static::assertCount(1, $zipFile);
  1823. $zipFile
  1824. ->saveAsFile($this->outputFilename)
  1825. ->close()
  1826. ;
  1827. static::assertCorrectZipArchive($this->outputFilename);
  1828. }
  1829. /**
  1830. * @throws ZipException
  1831. */
  1832. public function testPsrResponse()
  1833. {
  1834. $zipFile = new ZipFile();
  1835. for ($i = 0; $i < 10; $i++) {
  1836. $zipFile[$i] = $i;
  1837. }
  1838. $filename = 'file.jar';
  1839. $response = $zipFile->outputAsResponse(new Response(), $filename);
  1840. static::assertInstanceOf(ResponseInterface::class, $response);
  1841. static::assertSame('application/java-archive', $response->getHeaderLine('content-type'));
  1842. static::assertSame('attachment; filename="file.jar"', $response->getHeaderLine('content-disposition'));
  1843. }
  1844. /**
  1845. * @dataProvider provideCompressionLevels
  1846. *
  1847. * @param int $compressionLevel
  1848. *
  1849. * @throws ZipEntryNotFoundException
  1850. * @throws ZipException
  1851. * @throws \Exception
  1852. */
  1853. public function testCompressionLevel($compressionLevel)
  1854. {
  1855. $fileContent = random_bytes(512);
  1856. $entryName = 'file.txt';
  1857. $zipFile = new ZipFile();
  1858. $zipFile
  1859. ->addFromString($entryName, $fileContent, ZipCompressionMethod::DEFLATED)
  1860. ->setCompressionLevelEntry($entryName, $compressionLevel)
  1861. ->saveAsFile($this->outputFilename)
  1862. ->close()
  1863. ;
  1864. static::assertCorrectZipArchive($this->outputFilename);
  1865. $zipFile->openFile($this->outputFilename);
  1866. static::assertSame($zipFile->getEntryContents($entryName), $fileContent);
  1867. static::assertSame($zipFile->getEntry($entryName)->getCompressionLevel(), $compressionLevel);
  1868. static::assertSame($zipFile->getEntryInfo($entryName)->getCompressionLevel(), $compressionLevel);
  1869. $zipFile->close();
  1870. }
  1871. /**
  1872. * @return array
  1873. */
  1874. public function provideCompressionLevels()
  1875. {
  1876. return [
  1877. [ZipCompressionLevel::MAXIMUM],
  1878. [ZipCompressionLevel::NORMAL],
  1879. [ZipCompressionLevel::FAST],
  1880. [ZipCompressionLevel::SUPER_FAST],
  1881. ];
  1882. }
  1883. /**
  1884. * @throws ZipException
  1885. */
  1886. public function testInvalidCompressionLevel()
  1887. {
  1888. $this->expectException(InvalidArgumentException::class);
  1889. $this->expectExceptionMessage('Invalid compression level');
  1890. $zipFile = new ZipFile();
  1891. $zipFile->addFromString('file', 'content');
  1892. $zipFile->setCompressionLevel(15);
  1893. }
  1894. /**
  1895. * @throws ZipException
  1896. */
  1897. public function testInvalidCompressionLevelEntry()
  1898. {
  1899. $this->expectException(InvalidArgumentException::class);
  1900. $this->expectExceptionMessage('Invalid compression level');
  1901. $zipFile = new ZipFile();
  1902. $zipFile->addFromString('file', 'content');
  1903. $zipFile->setCompressionLevelEntry('file', 15);
  1904. }
  1905. /**
  1906. * @throws ZipException
  1907. */
  1908. public function testCompressionGlobal()
  1909. {
  1910. $zipFile = new ZipFile();
  1911. for ($i = 0; $i < 10; $i++) {
  1912. $zipFile->addFromString('file' . $i, 'content', ZipCompressionMethod::DEFLATED);
  1913. }
  1914. $zipFile
  1915. ->setCompressionLevel(ZipCompressionLevel::SUPER_FAST)
  1916. ->saveAsFile($this->outputFilename)
  1917. ->close()
  1918. ;
  1919. static::assertCorrectZipArchive($this->outputFilename);
  1920. $zipFile->openFile($this->outputFilename);
  1921. $infoList = $zipFile->getAllInfo();
  1922. array_walk(
  1923. $infoList,
  1924. function (ZipInfo $zipInfo) {
  1925. $this->assertSame($zipInfo->getCompressionLevel(), ZipCompressionLevel::SUPER_FAST);
  1926. }
  1927. );
  1928. $zipFile->close();
  1929. }
  1930. /**
  1931. * @throws ZipEntryNotFoundException
  1932. * @throws ZipException
  1933. */
  1934. public function testCompressionMethodEntry()
  1935. {
  1936. $zipFile = new ZipFile();
  1937. $zipFile->addFromString('file', 'content', ZipCompressionMethod::STORED);
  1938. $zipFile->saveAsFile($this->outputFilename);
  1939. $zipFile->close();
  1940. $zipFile->openFile($this->outputFilename);
  1941. static::assertSame($zipFile->getEntryInfo('file')->getMethodName(), 'Stored');
  1942. $zipFile->setCompressionMethodEntry('file', ZipCompressionMethod::DEFLATED);
  1943. static::assertSame($zipFile->getEntryInfo('file')->getMethodName(), 'Deflated');
  1944. $zipFile->rewrite();
  1945. static::assertSame($zipFile->getEntryInfo('file')->getMethodName(), 'Deflated');
  1946. }
  1947. /**
  1948. * @throws ZipException
  1949. */
  1950. public function testInvalidCompressionMethodEntry()
  1951. {
  1952. $this->expectException(
  1953. ZipUnsupportMethodException::class
  1954. );
  1955. $this->expectExceptionMessage(
  1956. 'Compression method 99 (AES Encryption) is not supported.'
  1957. );
  1958. $zipFile = new ZipFile();
  1959. $zipFile->addFromString('file', 'content', ZipCompressionMethod::STORED);
  1960. $zipFile->setCompressionMethodEntry('file', 99);
  1961. $zipFile->outputAsString();
  1962. }
  1963. /**
  1964. * @throws ZipException
  1965. */
  1966. public function testUnchangeAll()
  1967. {
  1968. $zipFile = new ZipFile();
  1969. for ($i = 0; $i < 10; $i++) {
  1970. $zipFile[$i] = $i;
  1971. }
  1972. $zipFile->setArchiveComment('comment');
  1973. static::assertCount(10, $zipFile);
  1974. static::assertSame($zipFile->getArchiveComment(), 'comment');
  1975. $zipFile->saveAsFile($this->outputFilename);
  1976. $zipFile->unchangeAll();
  1977. static::assertCount(0, $zipFile);
  1978. static::assertNull($zipFile->getArchiveComment());
  1979. $zipFile->close();
  1980. $zipFile->openFile($this->outputFilename);
  1981. static::assertCount(10, $zipFile);
  1982. static::assertSame($zipFile->getArchiveComment(), 'comment');
  1983. for ($i = 10; $i < 100; $i++) {
  1984. $zipFile[$i] = $i;
  1985. }
  1986. $zipFile->setArchiveComment('comment 2');
  1987. static::assertCount(100, $zipFile);
  1988. static::assertSame($zipFile->getArchiveComment(), 'comment 2');
  1989. $zipFile->unchangeAll();
  1990. static::assertCount(10, $zipFile);
  1991. static::assertSame($zipFile->getArchiveComment(), 'comment');
  1992. $zipFile->close();
  1993. }
  1994. /**
  1995. * @throws ZipException
  1996. */
  1997. public function testUnchangeArchiveComment()
  1998. {
  1999. $zipFile = new ZipFile();
  2000. for ($i = 0; $i < 10; $i++) {
  2001. $zipFile[$i] = $i;
  2002. }
  2003. $zipFile->setArchiveComment('comment');
  2004. static::assertSame($zipFile->getArchiveComment(), 'comment');
  2005. $zipFile->saveAsFile($this->outputFilename);
  2006. $zipFile->unchangeArchiveComment();
  2007. static::assertNull($zipFile->getArchiveComment());
  2008. $zipFile->close();
  2009. $zipFile->openFile($this->outputFilename);
  2010. static::assertSame($zipFile->getArchiveComment(), 'comment');
  2011. $zipFile->setArchiveComment('comment 2');
  2012. static::assertSame($zipFile->getArchiveComment(), 'comment 2');
  2013. $zipFile->unchangeArchiveComment();
  2014. static::assertSame($zipFile->getArchiveComment(), 'comment');
  2015. $zipFile->close();
  2016. }
  2017. /**
  2018. * @throws ZipEntryNotFoundException
  2019. * @throws ZipException
  2020. */
  2021. public function testUnchangeEntry()
  2022. {
  2023. $zipFile = new ZipFile();
  2024. $zipFile['file 1'] = 'content 1';
  2025. $zipFile['file 2'] = 'content 2';
  2026. $zipFile
  2027. ->saveAsFile($this->outputFilename)
  2028. ->close()
  2029. ;
  2030. $zipFile->openFile($this->outputFilename);
  2031. $zipFile['file 1'] = 'modify content 1';
  2032. $zipFile->setPasswordEntry('file 1', 'password');
  2033. static::assertSame($zipFile['file 1'], 'modify content 1');
  2034. static::assertTrue($zipFile->getEntryInfo('file 1')->isEncrypted());
  2035. static::assertSame($zipFile['file 2'], 'content 2');
  2036. static::assertFalse($zipFile->getEntryInfo('file 2')->isEncrypted());
  2037. $zipFile->unchangeEntry('file 1');
  2038. static::assertSame($zipFile['file 1'], 'content 1');
  2039. static::assertFalse($zipFile->getEntryInfo('file 1')->isEncrypted());
  2040. static::assertSame($zipFile['file 2'], 'content 2');
  2041. static::assertFalse($zipFile->getEntryInfo('file 2')->isEncrypted());
  2042. $zipFile->close();
  2043. }
  2044. /**
  2045. * @runInSeparateProcess
  2046. *
  2047. * @dataProvider provideOutputAsAttachment
  2048. *
  2049. * @param string $zipFilename
  2050. * @param string|null $mimeType
  2051. * @param string $expectedMimeType
  2052. * @param bool $attachment
  2053. * @param string $expectedAttachment
  2054. *
  2055. * @throws ZipException
  2056. */
  2057. public function testOutputAsAttachment($zipFilename, $mimeType, $expectedMimeType, $attachment, $expectedAttachment)
  2058. {
  2059. $zipFile = new ZipFile();
  2060. $file1Contents = 'content 1';
  2061. $zipFile['file 1'] = $file1Contents;
  2062. ob_start();
  2063. $zipFile->outputAsAttachment($zipFilename, $mimeType, $attachment);
  2064. $zipContents = ob_get_clean();
  2065. $zipFile->close();
  2066. $length = \strlen($zipContents);
  2067. static::assertTrue($length > 0);
  2068. $zipFile->openFromString($zipContents);
  2069. static::assertSame($zipFile['file 1'], $file1Contents);
  2070. $zipFile->close();
  2071. if (\function_exists('xdebug_get_headers')) {
  2072. $expectedHeaders = [
  2073. 'Content-Disposition: ' . $expectedAttachment . '; filename="' . $zipFilename . '"',
  2074. 'Content-Type: ' . $expectedMimeType,
  2075. 'Content-Length: ' . $length,
  2076. ];
  2077. /** @noinspection ForgottenDebugOutputInspection */
  2078. /** @noinspection PhpComposerExtensionStubsInspection */
  2079. static::assertSame($expectedHeaders, xdebug_get_headers());
  2080. }
  2081. }
  2082. /**
  2083. * @return array
  2084. */
  2085. public function provideOutputAsAttachment()
  2086. {
  2087. return [
  2088. ['file.zip', null, 'application/zip', true, 'attachment'],
  2089. ['file.zip', 'application/x-zip', 'application/x-zip', false, 'inline'],
  2090. ['file.apk', null, 'application/vnd.android.package-archive', true, 'attachment'],
  2091. ];
  2092. }
  2093. /**
  2094. * @dataProvider provideGetEntryStream
  2095. *
  2096. * @param ZipFile $zipFile
  2097. * @param string $entryName
  2098. * @param string $contents
  2099. *
  2100. * @throws ZipEntryNotFoundException
  2101. * @throws ZipException
  2102. */
  2103. public function testReopenEntryStream(ZipFile $zipFile, $entryName, $contents)
  2104. {
  2105. for ($i = 0; $i < 2; $i++) {
  2106. $fp = $zipFile->getEntryStream($entryName);
  2107. static::assertIsResource($fp);
  2108. static::assertSame(stream_get_contents($fp), $contents);
  2109. fclose($fp);
  2110. }
  2111. $zipFile->close();
  2112. }
  2113. /**
  2114. * @throws \Exception
  2115. *
  2116. * @return array
  2117. */
  2118. public function provideGetEntryStream()
  2119. {
  2120. $entryName = 'entry';
  2121. $contents = random_bytes(1024);
  2122. $zipFileSpl = new ZipFile();
  2123. $zipFileSpl->addSplFile(new \SplFileInfo(__FILE__), $entryName);
  2124. return [
  2125. [(new ZipFile())->addFromString($entryName, $contents), $entryName, $contents],
  2126. [(new ZipFile())->addFile(__FILE__, $entryName), $entryName, file_get_contents(__FILE__)],
  2127. [
  2128. (new ZipFile())->addFromStream(fopen(__FILE__, 'rb'), $entryName),
  2129. $entryName,
  2130. file_get_contents(__FILE__),
  2131. ],
  2132. [$zipFileSpl, $entryName, file_get_contents(__FILE__)],
  2133. ];
  2134. }
  2135. /**
  2136. * @throws ZipException
  2137. */
  2138. public function testGetEntries()
  2139. {
  2140. $zipFile = new ZipFile();
  2141. for ($i = 0; $i < 100; $i++) {
  2142. $zipFile->addFromString($i . '.txt', 'contents ' . $i);
  2143. }
  2144. $zipFile->saveAsFile($this->outputFilename);
  2145. $zipFile->close();
  2146. $zipFile->openFile($this->outputFilename);
  2147. $zipEntries = $zipFile->getEntries();
  2148. static::assertCount(100, $zipEntries);
  2149. foreach ($zipEntries as $zipEntry) {
  2150. static::assertInstanceOf(ZipEntry::class, $zipEntry);
  2151. static::assertNotSame($zipEntry->getDosTime(), 0);
  2152. $zipEntry->setDosTime(0);
  2153. $zipEntry->setCreatedOS(ZipPlatform::OS_DOS);
  2154. $zipEntry->setExtractedOS(ZipPlatform::OS_DOS);
  2155. $zipEntry->setInternalAttributes(1);
  2156. $zipEntry->setExternalAttributes(0);
  2157. }
  2158. $zipFile->rewrite();
  2159. self::assertCorrectZipArchive($this->outputFilename);
  2160. foreach ($zipFile->getEntries() as $zipEntry) {
  2161. static::assertSame($zipEntry->getDosTime(), 0);
  2162. static::assertSame($zipEntry->getExtractedOS(), ZipPlatform::OS_DOS);
  2163. static::assertSame($zipEntry->getCreatedOS(), ZipPlatform::OS_DOS);
  2164. static::assertSame($zipEntry->getInternalAttributes(), 1);
  2165. static::assertSame($zipEntry->getExternalAttributes(), 0);
  2166. }
  2167. $zipFile->close();
  2168. }
  2169. /**
  2170. * @throws ZipException
  2171. */
  2172. public function testRenameWithRecompressData()
  2173. {
  2174. $entryName = 'file.txt';
  2175. $newEntryName = 'rename_file.txt';
  2176. $contents = str_repeat('Test' . \PHP_EOL, 1024);
  2177. $zipFile = new ZipFile();
  2178. $zipFile->addFromString($entryName, $contents, ZipCompressionMethod::DEFLATED);
  2179. $zipFile->saveAsFile($this->outputFilename);
  2180. $zipFile->close();
  2181. self::assertCorrectZipArchive($this->outputFilename);
  2182. $zipFile->openFile($this->outputFilename);
  2183. $zipFile->rename($entryName, $newEntryName);
  2184. $zipFile->setCompressionMethodEntry($newEntryName, ZipCompressionMethod::STORED);
  2185. $zipFile->saveAsFile($this->outputFilename);
  2186. $zipFile->close();
  2187. self::assertCorrectZipArchive($this->outputFilename);
  2188. $zipFile->openFile($this->outputFilename);
  2189. static::assertSame($zipFile->getEntry($newEntryName)->getCompressionMethod(), ZipCompressionMethod::STORED);
  2190. $zipFile->close();
  2191. }
  2192. /**
  2193. * @throws ZipEntryNotFoundException
  2194. * @throws ZipException
  2195. */
  2196. public function testCloneZipContainerInZipWriter()
  2197. {
  2198. $zipFile = new ZipFile();
  2199. $zipFile['file 1'] = 'contents';
  2200. $zipEntryBeforeWrite = $zipFile->getEntry('file 1');
  2201. $zipFile->saveAsFile($this->outputFilename);
  2202. $zipAfterBeforeWrite = $zipFile->getEntry('file 1');
  2203. static::assertSame($zipAfterBeforeWrite, $zipEntryBeforeWrite);
  2204. $zipFile->close();
  2205. }
  2206. /**
  2207. * @throws ZipException
  2208. */
  2209. public function testMultiSave()
  2210. {
  2211. $zipFile = new ZipFile();
  2212. $zipFile['file 1'] = 'contents';
  2213. for ($i = 0; $i < 10; $i++) {
  2214. $zipFile->saveAsFile($this->outputFilename);
  2215. self::assertCorrectZipArchive($this->outputFilename);
  2216. }
  2217. $zipFile->close();
  2218. }
  2219. /**
  2220. * @throws ZipEntryNotFoundException
  2221. * @throws ZipException
  2222. */
  2223. public function testNoData()
  2224. {
  2225. $this->expectException(ZipException::class);
  2226. $this->expectExceptionMessage('No data for zip entry file');
  2227. $entryName = 'file';
  2228. $zipFile = new ZipFile();
  2229. try {
  2230. $zipFile[$entryName] = '';
  2231. $zipEntry = $zipFile->getEntry($entryName);
  2232. $zipEntry->setData(null);
  2233. $zipFile->getEntryContents($entryName);
  2234. } finally {
  2235. $zipFile->close();
  2236. }
  2237. }
  2238. /**
  2239. * @throws ZipEntryNotFoundException
  2240. * @throws ZipException
  2241. */
  2242. public function testReplaceEntryContentsByFile()
  2243. {
  2244. $entryName = basename(__FILE__);
  2245. $zipFile = new ZipFile();
  2246. $zipFile[$entryName] = 'contents';
  2247. $zipFile->saveAsFile($this->outputFilename);
  2248. $zipFile->close();
  2249. $zipFile->openFile($this->outputFilename);
  2250. $entry = $zipFile->getEntry($entryName);
  2251. $data = new ZipFileData($entry, new \SplFileInfo(__FILE__));
  2252. $entry->setData($data);
  2253. $zipFile->saveAsFile($this->outputFilename);
  2254. $zipFile->close();
  2255. self::assertCorrectZipArchive($this->outputFilename);
  2256. $zipFile->openFile($this->outputFilename);
  2257. static::assertSame(
  2258. $zipFile->getEntryContents($entryName),
  2259. file_get_contents(__FILE__)
  2260. );
  2261. $zipFile->close();
  2262. }
  2263. }