ZipFileTest.php 80 KB

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