ZipFileTest.php 82 KB

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