12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384 |
- <?php
- declare(strict_types=1);
- /*
- * This file is part of the nelexa/zip package.
- * (c) Ne-Lexa <https://github.com/Ne-Lexa/php-zip>
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace PhpZip\Tests;
- use GuzzleHttp\Psr7\Response;
- use PhpZip\Constants\ZipCompressionLevel;
- use PhpZip\Constants\ZipCompressionMethod;
- use PhpZip\Constants\ZipPlatform;
- use PhpZip\Exception\InvalidArgumentException;
- use PhpZip\Exception\ZipEntryNotFoundException;
- use PhpZip\Exception\ZipException;
- use PhpZip\Exception\ZipUnsupportMethodException;
- use PhpZip\Model\Data\ZipFileData;
- use PhpZip\Model\ZipEntry;
- use PhpZip\Util\FilesUtil;
- use PhpZip\ZipFile;
- /**
- * ZipFile test.
- *
- * @internal
- *
- * @small
- */
- class ZipFileTest extends ZipTestCase
- {
- /**
- * @throws ZipException
- */
- public function testOpenFileCantExists(): void
- {
- $this->expectException(ZipException::class);
- $this->expectExceptionMessage('does not exist');
- $zipFile = new ZipFile();
- $zipFile->openFile(uniqid('', false));
- }
- /**
- * @throws ZipException
- */
- public function testOpenFileCantOpen(): void
- {
- static::skipTestForWindows();
- static::skipTestForRootUser();
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Permission denied');
- static::assertNotFalse(file_put_contents($this->outputFilename, 'content'));
- static::assertTrue(chmod($this->outputFilename, 0222));
- $zipFile = new ZipFile();
- $zipFile->openFile($this->outputFilename);
- }
- /**
- * @throws ZipException
- */
- public function testOpenFileEmptyFile(): void
- {
- $this->expectException(ZipException::class);
- $this->expectExceptionMessage('Corrupt zip file');
- static::assertNotFalse(touch($this->outputFilename));
- $zipFile = new ZipFile();
- $zipFile->openFile($this->outputFilename);
- }
- /**
- * @throws ZipException
- * @throws \Exception
- */
- public function testOpenFileInvalidZip(): void
- {
- $this->expectException(ZipException::class);
- $this->expectExceptionMessage('Invalid zip file. The end of the central directory could not be found.');
- static::assertNotFalse(file_put_contents($this->outputFilename, random_bytes(255)));
- $zipFile = new ZipFile();
- $zipFile->openFile($this->outputFilename);
- }
- /**
- * @throws ZipException
- */
- public function testOpenFromStringEmptyString(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Empty string passed');
- $zipFile = new ZipFile();
- $zipFile->openFromString('');
- }
- /**
- * @throws ZipException
- * @throws \Exception
- */
- public function testOpenFromStringInvalidZip(): void
- {
- $this->expectException(ZipException::class);
- $this->expectExceptionMessage('Invalid zip file. The end of the central directory could not be found.');
- $zipFile = new ZipFile();
- $zipFile->openFromString(random_bytes(255));
- }
- /**
- * @throws ZipException
- */
- public function testOpenFromString(): void
- {
- $zipFile = new ZipFile();
- $zipFile->addFromString('file', 'content');
- $zipFile['file2'] = 'content 2';
- $zipContents = $zipFile->outputAsString();
- $zipFile->close();
- $zipFile->openFromString($zipContents);
- static::assertSame($zipFile->count(), 2);
- static::assertTrue(isset($zipFile['file']));
- static::assertTrue(isset($zipFile['file2']));
- static::assertSame($zipFile['file'], 'content');
- static::assertSame($zipFile['file2'], 'content 2');
- $zipFile->close();
- }
- /**
- * @throws ZipException
- */
- public function testOpenFromStreamNullStream(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Stream must be a resource');
- $zipFile = new ZipFile();
- $zipFile->openFromStream(null);
- }
- /**
- * @throws ZipException
- */
- public function testOpenFromStreamInvalidResourceType(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Stream must be a resource');
- $zipFile = new ZipFile();
- /** @noinspection PhpParamsInspection */
- $zipFile->openFromStream('stream resource');
- }
- /**
- * @throws ZipException
- */
- public function testOpenFromStreamInvalidResourceType3(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Directory stream not supported');
- $zipFile = new ZipFile();
- $zipFile->openFromStream(opendir(__DIR__));
- }
- /**
- * @throws ZipException
- * @noinspection PhpUsageOfSilenceOperatorInspection
- * @noinspection NestedPositiveIfStatementsInspection
- */
- public function testOpenFromStreamNoSeekable(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('The stream wrapper type "http" is not supported');
- if (!$fp = @fopen('http://localhost', 'rb')) {
- if (!$fp = @fopen('http://example.org', 'rb')) {
- static::markTestSkipped('not connected to localhost or remote host');
- }
- }
- $zipFile = new ZipFile();
- $zipFile->openFromStream($fp);
- }
- /**
- * @throws ZipException
- */
- public function testOpenFromStreamEmptyContents(): void
- {
- $this->expectException(ZipException::class);
- $this->expectExceptionMessage('Corrupt zip file');
- $fp = fopen($this->outputFilename, 'w+b');
- $zipFile = new ZipFile();
- $zipFile->openFromStream($fp);
- }
- /**
- * @throws ZipException
- * @throws \Exception
- */
- public function testOpenFromStreamInvalidZip(): void
- {
- $this->expectException(ZipException::class);
- $this->expectExceptionMessage('Invalid zip file. The end of the central directory could not be found.');
- $fp = fopen($this->outputFilename, 'w+b');
- fwrite($fp, random_bytes(255));
- $zipFile = new ZipFile();
- $zipFile->openFromStream($fp);
- }
- /**
- * @throws ZipException
- */
- public function testOpenFromStream(): void
- {
- $zipFile = new ZipFile();
- $zipFile
- ->addFromString('file', 'content')
- ->saveAsFile($this->outputFilename)
- ->close()
- ;
- $handle = fopen($this->outputFilename, 'rb');
- $zipFile->openFromStream($handle);
- static::assertSame($zipFile->count(), 1);
- static::assertTrue(isset($zipFile['file']));
- static::assertSame($zipFile['file'], 'content');
- $zipFile->close();
- }
- /**
- * Test create, open and extract empty archive.
- *
- * @throws ZipException
- */
- public function testEmptyArchive(): void
- {
- $zipFile = new ZipFile();
- $zipFile
- ->saveAsFile($this->outputFilename)
- ->close()
- ;
- static::assertCorrectEmptyZip($this->outputFilename);
- static::assertTrue(mkdir($this->outputDirname, 0755, true));
- $zipFile->openFile($this->outputFilename);
- static::assertSame($zipFile->count(), 0);
- $zipFile
- ->extractTo($this->outputDirname)
- ->close()
- ;
- static::assertTrue(FilesUtil::isEmptyDir($this->outputDirname));
- }
- /**
- * No modified archive.
- *
- * @throws ZipException
- *
- * @see ZipOutputFile::create()
- */
- public function testNoModifiedArchive(): void
- {
- static::assertTrue(mkdir($this->outputDirname, 0755, true));
- $fileActual = $this->outputDirname . \DIRECTORY_SEPARATOR . 'file_actual.zip';
- $fileExpected = $this->outputDirname . \DIRECTORY_SEPARATOR . 'file_expected.zip';
- $zipFile = new ZipFile();
- $zipFile->addDirRecursive(__DIR__ . '/../src');
- $sourceCount = $zipFile->count();
- static::assertTrue($sourceCount > 0);
- $zipFile
- ->saveAsFile($fileActual)
- ->close()
- ;
- static::assertCorrectZipArchive($fileActual);
- $zipFile
- ->openFile($fileActual)
- ->saveAsFile($fileExpected)
- ;
- static::assertCorrectZipArchive($fileExpected);
- $zipFileExpected = new ZipFile();
- $zipFileExpected->openFile($fileExpected);
- static::assertSame($zipFile->count(), $sourceCount);
- static::assertSame($zipFileExpected->count(), $zipFile->count());
- static::assertSame($zipFileExpected->getListFiles(), $zipFile->getListFiles());
- foreach ($zipFile as $entryName => $content) {
- static::assertSame($zipFileExpected[$entryName], $content);
- }
- $zipFileExpected->close();
- $zipFile->close();
- }
- /**
- * Create archive and add files.
- *
- * @throws ZipException
- *
- * @see ZipOutputFile::addFromFile()
- * @see ZipOutputFile::addFromStream()
- * @see ZipFile::getEntryContents()
- * @see ZipOutputFile::addFromString()
- */
- public function testCreateArchiveAndAddFiles(): void
- {
- $outputFromString = file_get_contents(__FILE__);
- $outputFromString2 = file_get_contents(\dirname(__DIR__) . \DIRECTORY_SEPARATOR . 'README.md');
- $outputFromFile = file_get_contents(\dirname(__DIR__) . \DIRECTORY_SEPARATOR . 'phpunit.xml');
- $outputFromStream = file_get_contents(\dirname(__DIR__) . \DIRECTORY_SEPARATOR . 'composer.json');
- $filenameFromString = basename(__FILE__);
- $filenameFromString2 = 'test_file.txt';
- $filenameFromFile = 'data/test file.txt';
- $filenameFromStream = 'data/ডিরেক্টরি/αρχείο.json';
- $emptyDirName = 'empty dir/пустой каталог/空目錄/ไดเรกทอรีที่ว่างเปล่า/';
- $emptyDirName2 = 'empty dir/пустой каталог/';
- $emptyDirName3 = 'empty dir/пустой каталог/ещё один пустой каталог/';
- $tempFile = tempnam(sys_get_temp_dir(), 'txt');
- file_put_contents($tempFile, $outputFromFile);
- $tempStream = tmpfile();
- fwrite($tempStream, $outputFromStream);
- $zipFile = new ZipFile();
- $zipFile
- ->addFromString($filenameFromString, $outputFromString)
- ->addFile($tempFile, $filenameFromFile)
- ->addFromStream($tempStream, $filenameFromStream)
- ->addEmptyDir($emptyDirName)
- ;
- $zipFile[$filenameFromString2] = $outputFromString2;
- $zipFile[$emptyDirName2] = null;
- $zipFile[$emptyDirName3] = 'this content ignoring';
- static::assertSame(\count($zipFile), 7);
- $zipFile
- ->saveAsFile($this->outputFilename)
- ->close()
- ;
- unlink($tempFile);
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertSame(\count($zipFile), 7);
- static::assertSame($zipFile[$filenameFromString], $outputFromString);
- static::assertSame($zipFile[$filenameFromFile], $outputFromFile);
- static::assertSame($zipFile[$filenameFromStream], $outputFromStream);
- static::assertSame($zipFile[$filenameFromString2], $outputFromString2);
- static::assertTrue(isset($zipFile[$emptyDirName]));
- static::assertTrue(isset($zipFile[$emptyDirName2]));
- static::assertTrue(isset($zipFile[$emptyDirName3]));
- static::assertTrue($zipFile->isDirectory($emptyDirName));
- static::assertTrue($zipFile->isDirectory($emptyDirName2));
- static::assertTrue($zipFile->isDirectory($emptyDirName3));
- $listFiles = $zipFile->getListFiles();
- static::assertSame($listFiles[0], $filenameFromString);
- static::assertSame($listFiles[1], $filenameFromFile);
- static::assertSame($listFiles[2], $filenameFromStream);
- static::assertSame($listFiles[3], $emptyDirName);
- static::assertSame($listFiles[4], $filenameFromString2);
- static::assertSame($listFiles[5], $emptyDirName2);
- static::assertSame($listFiles[6], $emptyDirName3);
- $zipFile->close();
- }
- /**
- * @throws ZipException
- */
- public function testEmptyContent(): void
- {
- $zipFile = new ZipFile();
- $zipFile['file'] = '';
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- $zipFile->openFile($this->outputFilename);
- static::assertSame($zipFile['file'], '');
- $zipFile->close();
- }
- /**
- * Test compression method from image file.
- *
- * @throws ZipException
- */
- public function testCompressionMethodFromImageMimeType(): void
- {
- if (!\function_exists('mime_content_type')) {
- static::markTestSkipped('Function mime_content_type not exists');
- }
- $outputFilename = $this->outputFilename;
- $this->outputFilename .= '.gif';
- static::assertNotFalse(
- file_put_contents(
- $this->outputFilename,
- base64_decode('R0lGODlhAQABAJAAAP8AAAAAACH5BAUQAAAALAAAAAABAAEAAAICBAEAOw==', true)
- )
- );
- $basename = basename($this->outputFilename);
- $zipFile = new ZipFile();
- $zipFile->addFile($this->outputFilename, $basename);
- $zipFile->saveAsFile($outputFilename);
- unlink($this->outputFilename);
- $this->outputFilename = $outputFilename;
- $zipFile->openFile($this->outputFilename);
- $zipEntry = $zipFile->getEntry($basename);
- static::assertSame($zipEntry->getCompressionMethod(), ZipCompressionMethod::STORED);
- $zipFile->close();
- }
- /**
- * Rename zip entry name.
- *
- * @throws ZipException
- */
- public function testRename(): void
- {
- $oldName = basename(__FILE__);
- $newName = 'tests/' . $oldName;
- $zipFile = new ZipFile();
- $zipFile->addDir(__DIR__);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- $zipFile->rename($oldName, $newName);
- $zipFile->addFromString('file1.txt', 'content');
- $zipFile->addFromString('file2.txt', 'content');
- $zipFile->addFromString('file3.txt', 'content');
- $zipFile->rename('file1.txt', 'file_long_name.txt');
- $zipFile->rename('file2.txt', 'file4.txt');
- $zipFile->rename('file3.txt', 'fi.txt');
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertFalse(isset($zipFile[$oldName]));
- static::assertTrue(isset($zipFile[$newName]));
- static::assertFalse(isset($zipFile['file1.txt']));
- static::assertFalse(isset($zipFile['file2.txt']));
- static::assertFalse(isset($zipFile['file3.txt']));
- static::assertTrue(isset($zipFile['file_long_name.txt']));
- static::assertTrue(isset($zipFile['file4.txt']));
- static::assertTrue(isset($zipFile['fi.txt']));
- $zipFile->close();
- }
- /**
- * @throws ZipException
- */
- public function testRenameEntryToExistsNewEntry(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('is exists');
- $zipFile = new ZipFile();
- $zipFile['file'] = 'content';
- $zipFile['file2'] = 'content 2';
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- $zipFile = new ZipFile();
- $zipFile->openFile($this->outputFilename);
- $zipFile->rename('file2', 'file');
- }
- /**
- * @throws ZipException
- */
- public function testRenameEntryNotFound(): void
- {
- $this->expectException(ZipEntryNotFoundException::class);
- $zipFile = new ZipFile();
- $zipFile['file'] = 'content';
- $zipFile['file2'] = 'content 2';
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- $zipFile = new ZipFile();
- $zipFile->openFile($this->outputFilename);
- $zipFile->rename('file2.bak', 'file3');
- }
- /**
- * Delete entry from name.
- *
- * @throws ZipException
- */
- public function testDeleteFromName(): void
- {
- $inputDir = \dirname(__DIR__) . \DIRECTORY_SEPARATOR;
- $deleteEntryName = 'composer.json';
- $zipFile = new ZipFile();
- $zipFile->addDir($inputDir);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- $zipFile->deleteFromName($deleteEntryName);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertFalse(isset($zipFile[$deleteEntryName]));
- $zipFile->close();
- }
- /**
- * @throws ZipEntryNotFoundException
- * @throws ZipException
- */
- public function testDeleteNewEntry(): void
- {
- $zipFile = new ZipFile();
- $zipFile['entry1'] = '';
- $zipFile['entry2'] = '';
- $zipFile->deleteFromName('entry2');
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- $zipFile->openFile($this->outputFilename);
- static::assertSame(\count($zipFile), 1);
- static::assertTrue(isset($zipFile['entry1']));
- static::assertFalse(isset($zipFile['entry2']));
- $zipFile->close();
- }
- /**
- * @throws ZipEntryNotFoundException
- */
- public function testDeleteFromNameNotFoundEntry(): void
- {
- $this->expectException(ZipEntryNotFoundException::class);
- $zipFile = new ZipFile();
- $zipFile->deleteFromName('entry');
- }
- public function testCatchNotFoundEntry(): void
- {
- $entryName = 'entry';
- $zipFile = new ZipFile();
- try {
- $zipFile->getEntry($entryName);
- } catch (ZipEntryNotFoundException $e) {
- static::assertSame($e->getEntryName(), $entryName);
- }
- }
- /**
- * Delete zip entries from glob pattern.
- *
- * @throws ZipException
- */
- public function testDeleteFromGlob(): void
- {
- $inputDir = \dirname(__DIR__);
- $zipFile = new ZipFile();
- $zipFile->addFilesFromGlobRecursive($inputDir, '**.{xml,json,md}');
- static::assertTrue(isset($zipFile['composer.json']));
- static::assertTrue(isset($zipFile['phpunit.xml']));
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertTrue(isset($zipFile['composer.json']));
- static::assertTrue(isset($zipFile['phpunit.xml']));
- $zipFile->deleteFromGlob('**.{xml,json}');
- static::assertFalse(isset($zipFile['composer.json']));
- static::assertFalse(isset($zipFile['phpunit.xml']));
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertTrue($zipFile->count() > 0);
- foreach ($zipFile->getListFiles() as $name) {
- static::assertStringEndsWith('.md', $name);
- }
- $zipFile->close();
- }
- public function testDeleteFromGlobFailEmpty(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('The glob pattern is not specified');
- $zipFile = new ZipFile();
- $zipFile->deleteFromGlob('');
- }
- /**
- * Delete entries from regex pattern.
- *
- * @throws ZipException
- */
- public function testDeleteFromRegex(): void
- {
- $inputDir = \dirname(__DIR__);
- $zipFile = new ZipFile();
- $zipFile->addFilesFromRegexRecursive($inputDir, '~\.(xml|json)$~i', 'Path');
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- $zipFile->deleteFromRegex('~\.(json)$~i');
- $zipFile->addFromString('test.txt', 'content');
- $zipFile->deleteFromRegex('~\.txt$~');
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertFalse(isset($zipFile['Path/composer.json']));
- static::assertFalse(isset($zipFile['Path/test.txt']));
- static::assertTrue(isset($zipFile['Path/phpunit.xml']));
- $zipFile->close();
- }
- public function testDeleteFromRegexFailEmpty(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('The regex pattern is not specified');
- $zipFile = new ZipFile();
- $zipFile->deleteFromRegex('');
- }
- /**
- * Delete all entries.
- *
- * @throws ZipException
- */
- public function testDeleteAll(): void
- {
- $zipFile = new ZipFile();
- $zipFile->addDirRecursive(\dirname(__DIR__) . \DIRECTORY_SEPARATOR . 'src');
- static::assertTrue($zipFile->count() > 0);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertTrue($zipFile->count() > 0);
- $zipFile->deleteAll();
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectEmptyZip($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertSame($zipFile->count(), 0);
- $zipFile->close();
- }
- /**
- * Test zip archive comment.
- *
- * @throws ZipException
- */
- public function testArchiveComment(): void
- {
- $comment = 'This zip file comment' . \PHP_EOL
- . 'Αυτό το σχόλιο αρχείο zip' . \PHP_EOL
- . 'Это комментарий zip архива' . \PHP_EOL
- . '這個ZIP文件註釋' . \PHP_EOL
- . 'ეს zip ფაილის კომენტარი' . \PHP_EOL
- . 'このzipファイルにコメント' . \PHP_EOL
- . 'ความคิดเห็นนี้ไฟล์ซิป';
- $zipFile = new ZipFile();
- $zipFile->setArchiveComment($comment);
- $zipFile->addFile(__FILE__);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertSame($zipFile->getArchiveComment(), $comment);
- $zipFile->setArchiveComment(/* null */); // remove archive comment
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- // check empty comment
- $zipFile->openFile($this->outputFilename);
- static::assertNull($zipFile->getArchiveComment());
- $zipFile->close();
- }
- /**
- * Test very long archive comment.
- */
- public function testVeryLongArchiveComment(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $comment = 'Very long comment' . \PHP_EOL
- . 'Очень длинный комментарий' . \PHP_EOL;
- $comment = str_repeat($comment, (int) ceil(0xFFFF / \strlen($comment)) + \strlen($comment) + 1);
- $zipFile = new ZipFile();
- $zipFile->setArchiveComment($comment);
- }
- /**
- * Test zip entry comment.
- *
- * @throws ZipException
- * @throws \Exception
- */
- public function testEntryComment(): void
- {
- $entries = [
- '文件1.txt' => [
- 'data' => random_bytes(255),
- 'comment' => '這是註釋的條目。',
- ],
- 'file2.txt' => [
- 'data' => random_bytes(255),
- 'comment' => null,
- ],
- 'file3.txt' => [
- 'data' => random_bytes(255),
- 'comment' => random_bytes(255),
- ],
- 'file4.txt' => [
- 'data' => random_bytes(255),
- 'comment' => 'Комментарий файла',
- ],
- 'file5.txt' => [
- 'data' => random_bytes(255),
- 'comment' => 'ไฟล์แสดงความคิดเห็น',
- ],
- 'file6 emoji 🙍🏼.txt' => [
- 'data' => random_bytes(255),
- 'comment' => 'Emoji comment file - 😀 ⛈ ❤️ 🤴🏽',
- ],
- ];
- // create archive with entry comments
- $zipFile = new ZipFile();
- foreach ($entries as $entryName => $item) {
- $zipFile->addFromString($entryName, $item['data']);
- $zipFile->setEntryComment($entryName, $item['comment']);
- }
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- // check and modify comments
- $zipFile->openFile($this->outputFilename);
- foreach ($zipFile->getListFiles() as $entryName) {
- $entriesItem = $entries[$entryName];
- static::assertNotEmpty($entriesItem);
- static::assertSame($zipFile[$entryName], $entriesItem['data']);
- static::assertSame($zipFile->getEntryComment($entryName), (string) $entriesItem['comment']);
- }
- // modify comment
- $entries['file5.txt']['comment'] = random_bytes(256);
- $zipFile->setEntryComment('file5.txt', $entries['file5.txt']['comment']);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- // check modify comments
- $zipFile->openFile($this->outputFilename);
- foreach ($entries as $entryName => $entriesItem) {
- static::assertTrue(isset($zipFile[$entryName]));
- static::assertSame($zipFile->getEntryComment($entryName), (string) $entriesItem['comment']);
- static::assertSame($zipFile[$entryName], $entriesItem['data']);
- }
- $zipFile->close();
- }
- /**
- * Test zip entry very long comment.
- *
- * @throws ZipException
- */
- public function testVeryLongEntryComment(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Comment too long');
- $comment = 'Very long comment' . \PHP_EOL
- . 'Очень длинный комментарий' . \PHP_EOL;
- $comment = str_repeat($comment, (int) ceil(0xFFFF / \strlen($comment)) + \strlen($comment) + 1);
- $zipFile = new ZipFile();
- $zipFile->addFile(__FILE__, 'test');
- $zipFile->setEntryComment('test', $comment);
- }
- /**
- * @throws ZipException
- */
- public function testSetEntryCommentNotFoundEntry(): void
- {
- $this->expectException(ZipEntryNotFoundException::class);
- $zipFile = new ZipFile();
- $zipFile->setEntryComment('test', 'comment');
- }
- /**
- * Test all available support compression methods.
- *
- * @throws ZipException
- * @throws \Exception
- */
- public function testCompressionMethod(): void
- {
- $entries = [
- 1 => [
- 'data' => random_bytes(255),
- 'method' => ZipCompressionMethod::STORED,
- ],
- 2 => [
- 'data' => random_bytes(255),
- 'method' => ZipCompressionMethod::DEFLATED,
- ],
- ];
- if (\extension_loaded('bz2')) {
- $entries[3] = [
- 'data' => random_bytes(255),
- 'method' => ZipCompressionMethod::BZIP2,
- ];
- }
- $zipFile = new ZipFile();
- foreach ($entries as $entryName => $item) {
- $entryName = (string) $entryName;
- $zipFile->addFromString($entryName, $item['data'], $item['method']);
- }
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- $zipFile->setCompressionLevel(ZipCompressionLevel::MAXIMUM);
- foreach ($zipFile->getEntries() as $entryName => $zipEntry) {
- $entryName = (string) $entryName;
- static::assertSame($zipFile[$entryName], $entries[$entryName]['data']);
- static::assertSame($zipEntry->getCompressionMethod(), $entries[$entryName]['method']);
- }
- $zipFile->close();
- }
- /**
- * @dataProvider provideInvalidCompressionLevels
- */
- public function testSetInvalidCompressionLevel(int $compressionLevel): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Invalid compression level. Minimum level 1. Maximum level 9');
- $zipFile = new ZipFile();
- $zipFile['file 1'] = 'contents';
- $zipFile->setCompressionLevel($compressionLevel);
- }
- public function provideInvalidCompressionLevels(): array
- {
- return [
- [-10],
- [-2],
- [10],
- [0xFFFF],
- ];
- }
- /**
- * Test extract all files.
- *
- * @throws ZipException
- * @throws \Exception
- */
- public function testExtract(): void
- {
- $entries = [
- 'test1.txt' => random_bytes(255),
- 'test2.txt' => random_bytes(255),
- 'test/test 2/test3.txt' => random_bytes(255),
- 'test empty/dir/' => null,
- ];
- $zipFile = new ZipFile();
- foreach ($entries as $entryName => $contents) {
- if ($contents === null) {
- $zipFile->addEmptyDir($entryName);
- } else {
- $zipFile->addFromString($entryName, $contents);
- }
- }
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertTrue(mkdir($this->outputDirname, 0755, true));
- $zipFile->openFile($this->outputFilename);
- $zipFile->extractTo($this->outputDirname, null, [], $extractedEntries);
- foreach ($entries as $entryName => $contents) {
- $name = $entryName;
- if (\DIRECTORY_SEPARATOR === '\\') {
- $name = str_replace('/', '\\', $name);
- }
- $fullExtractedFilename = $this->outputDirname . \DIRECTORY_SEPARATOR . $name;
- static::assertTrue(
- isset($extractedEntries[$fullExtractedFilename]),
- 'No extract info for ' . $fullExtractedFilename
- );
- if ($contents === null) {
- static::assertDirectoryExists($fullExtractedFilename);
- static::assertTrue(FilesUtil::isEmptyDir($fullExtractedFilename));
- } else {
- static::assertTrue(is_file($fullExtractedFilename));
- $contents = file_get_contents($fullExtractedFilename);
- static::assertSame($contents, $contents);
- }
- /** @var ZipEntry $entry */
- $entry = $extractedEntries[$fullExtractedFilename];
- static::assertSame($entry->getName(), $entryName);
- }
- $zipFile->close();
- }
- /**
- * Test extract some files.
- *
- * @throws ZipException
- * @throws \Exception
- */
- public function testExtractSomeFiles(): void
- {
- $entries = [
- 'test1.txt' => random_bytes(255),
- 'test2.txt' => random_bytes(255),
- 'test3.txt' => random_bytes(255),
- 'test4.txt' => random_bytes(255),
- 'test5.txt' => random_bytes(255),
- 'test/test/test.txt' => random_bytes(255),
- 'test/test/test 2.txt' => random_bytes(255),
- 'test empty/dir/' => null,
- 'test empty/dir2/' => null,
- ];
- $extractEntries = [
- 'test1.txt',
- 'test3.txt',
- 'test5.txt',
- 'test/test/test 2.txt',
- 'test empty/dir2/',
- ];
- static::assertTrue(mkdir($this->outputDirname, 0755, true));
- $zipFile = new ZipFile();
- $zipFile->addAll($entries);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- $zipFile->openFile($this->outputFilename);
- $zipFile->extractTo($this->outputDirname, $extractEntries);
- foreach ($entries as $entryName => $value) {
- $fullExtractFilename = $this->outputDirname . \DIRECTORY_SEPARATOR . $entryName;
- if (\in_array($entryName, $extractEntries, true)) {
- if ($value === null) {
- static::assertDirectoryExists($fullExtractFilename);
- static::assertTrue(FilesUtil::isEmptyDir($fullExtractFilename));
- } else {
- static::assertTrue(is_file($fullExtractFilename));
- $contents = file_get_contents($fullExtractFilename);
- static::assertEquals($contents, $value);
- }
- } elseif ($value === null) {
- static::assertDirectoryDoesNotExist($fullExtractFilename);
- } else {
- static::assertFalse(is_file($fullExtractFilename));
- }
- }
- static::assertFalse(is_file($this->outputDirname . \DIRECTORY_SEPARATOR . 'test/test/test.txt'));
- $zipFile->extractTo($this->outputDirname, 'test/test/test.txt');
- static::assertTrue(is_file($this->outputDirname . \DIRECTORY_SEPARATOR . 'test/test/test.txt'));
- $zipFile->close();
- }
- /**
- * @throws ZipException
- */
- public function testExtractFail(): void
- {
- $this->expectException(ZipException::class);
- $this->expectExceptionMessage('not found');
- $zipFile = new ZipFile();
- $zipFile['file'] = 'content';
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- $zipFile->openFile($this->outputFilename);
- $zipFile->extractTo('path/to/path');
- }
- /**
- * @throws ZipException
- */
- public function testExtractFail2(): void
- {
- $this->expectException(ZipException::class);
- $this->expectExceptionMessage('Destination is not directory');
- $zipFile = new ZipFile();
- $zipFile['file'] = 'content';
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- $zipFile->openFile($this->outputFilename);
- $zipFile->extractTo($this->outputFilename);
- }
- /**
- * @throws ZipException
- */
- public function testExtractFail3(): void
- {
- $this->expectException(ZipException::class);
- $this->expectExceptionMessage('Destination is not writable directory');
- static::skipTestForRootUser();
- $zipFile = new ZipFile();
- $zipFile['file'] = 'content';
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertTrue(mkdir($this->outputDirname, 0444, true));
- static::assertTrue(chmod($this->outputDirname, 0444));
- $zipFile->openFile($this->outputFilename);
- $zipFile->extractTo($this->outputDirname);
- }
- /**
- * @noinspection OnlyWritesOnParameterInspection
- */
- public function testAddFromArrayAccessNullName(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Key must not be null, but must contain the name of the zip entry.');
- $zipFile = new ZipFile();
- $zipFile[null] = 'content';
- }
- /**
- * @noinspection OnlyWritesOnParameterInspection
- */
- public function testAddFromArrayAccessEmptyName(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Key is empty, but must contain the name of the zip entry.');
- $zipFile = new ZipFile();
- $zipFile[''] = 'content';
- }
- /**
- * @throws ZipException
- */
- public function testAddFromStringUnsupportedMethod(): void
- {
- $this->expectException(ZipUnsupportMethodException::class);
- $this->expectExceptionMessage('Compression method 99 (AES Encryption) is not supported.');
- $zipFile = new ZipFile();
- $zipFile->addFromString('file', 'contents', ZipCompressionMethod::WINZIP_AES);
- $zipFile->outputAsString();
- }
- /**
- * @throws ZipException
- */
- public function testAddFromStringEmptyEntryName(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Empty entry name');
- $zipFile = new ZipFile();
- $zipFile->addFromString('', 'contents');
- }
- /**
- * Test compression method from add string.
- *
- * @throws ZipException
- */
- public function testAddFromStringCompressionMethod(): void
- {
- $fileStored = sys_get_temp_dir() . '/zip-stored.txt';
- $fileDeflated = sys_get_temp_dir() . '/zip-deflated.txt';
- static::assertNotFalse(file_put_contents($fileStored, 'content'));
- static::assertNotFalse(file_put_contents($fileDeflated, str_repeat('content', 200)));
- $zipFile = new ZipFile();
- $zipFile->addFromString(basename($fileStored), file_get_contents($fileStored));
- $zipFile->addFromString(basename($fileDeflated), file_get_contents($fileDeflated));
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- unlink($fileStored);
- unlink($fileDeflated);
- $zipFile->openFile($this->outputFilename);
- $methodStored = $zipFile->getEntry(basename($fileStored))->getCompressionMethod();
- $methodDeflated = $zipFile->getEntry(basename($fileDeflated))->getCompressionMethod();
- static::assertSame($methodStored, ZipCompressionMethod::STORED);
- static::assertSame($methodDeflated, ZipCompressionMethod::DEFLATED);
- $zipFile->close();
- }
- /**
- * @throws ZipException
- */
- public function testAddFromStreamInvalidResource(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Stream is not resource');
- $zipFile = new ZipFile();
- /** @noinspection PhpParamsInspection */
- $zipFile->addFromStream('invalid resource', 'name');
- }
- /**
- * @throws ZipException
- */
- public function testAddFromStreamEmptyEntryName(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Empty entry name');
- $handle = fopen(__FILE__, 'rb');
- $zipFile = new ZipFile();
- $zipFile->addFromStream($handle, '');
- }
- /**
- * @throws ZipException
- */
- public function testAddFromStreamUnsupportedMethod(): void
- {
- $this->expectException(ZipUnsupportMethodException::class);
- $this->expectExceptionMessage('Compression method 99 (AES Encryption) is not supported.');
- $handle = fopen(__FILE__, 'rb');
- $zipFile = new ZipFile();
- $zipFile->addFromStream($handle, basename(__FILE__), ZipCompressionMethod::WINZIP_AES);
- $zipFile->outputAsString();
- }
- /**
- * Test compression method from add stream.
- *
- * @throws ZipException
- */
- public function testAddFromStreamCompressionMethod(): void
- {
- $fileStored = sys_get_temp_dir() . '/zip-stored.txt';
- $fileDeflated = sys_get_temp_dir() . '/zip-deflated.txt';
- static::assertNotFalse(file_put_contents($fileStored, 'content'));
- static::assertNotFalse(file_put_contents($fileDeflated, str_repeat('content', 200)));
- $fpStored = fopen($fileStored, 'rb');
- $fpDeflated = fopen($fileDeflated, 'rb');
- $zipFile = new ZipFile();
- $zipFile->addFromStream($fpStored, basename($fileStored));
- $zipFile->addFromStream($fpDeflated, basename($fileDeflated));
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- unlink($fileStored);
- unlink($fileDeflated);
- $zipFile->openFile($this->outputFilename);
- $methodStored = $zipFile->getEntry(basename($fileStored))->getCompressionMethod();
- $methodDeflated = $zipFile->getEntry(basename($fileDeflated))->getCompressionMethod();
- static::assertSame($methodStored, ZipCompressionMethod::STORED);
- static::assertSame($methodDeflated, ZipCompressionMethod::DEFLATED);
- $zipFile->close();
- }
- /**
- * @throws ZipException
- */
- public function testAddFileCantExists(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('File path/to/file is not readable');
- $zipFile = new ZipFile();
- $zipFile->addFile('path/to/file');
- }
- /**
- * @throws ZipException
- */
- public function testAddFileUnsupportedMethod(): void
- {
- $this->expectException(ZipUnsupportMethodException::class);
- $this->expectExceptionMessage('Compression method 99 (AES Encryption) is not supported.');
- $zipFile = new ZipFile();
- $zipFile->addFile(__FILE__, null, ZipCompressionMethod::WINZIP_AES);
- $zipFile->outputAsString();
- }
- /**
- * @throws ZipException
- */
- public function testAddFileCannotOpen(): void
- {
- static::skipTestForWindows();
- static::skipTestForRootUser();
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('is not readable');
- static::assertNotFalse(file_put_contents($this->outputFilename, ''));
- static::assertTrue(chmod($this->outputFilename, 0244));
- $zipFile = new ZipFile();
- $zipFile->addFile($this->outputFilename);
- }
- /**
- * @throws ZipException
- */
- public function testAddDirEmptyDirname(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('The input directory is not specified');
- $zipFile = new ZipFile();
- $zipFile->addDir('');
- }
- /**
- * @throws ZipException
- */
- public function testAddDirCantExists(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('does not exist');
- $zipFile = new ZipFile();
- $zipFile->addDir(uniqid('', false));
- }
- /**
- * @throws ZipException
- */
- public function testAddDirRecursiveEmptyDirname(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('The input directory is not specified');
- $zipFile = new ZipFile();
- $zipFile->addDirRecursive('');
- }
- /**
- * @throws ZipException
- */
- public function testAddDirRecursiveCantExists(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('does not exist');
- $zipFile = new ZipFile();
- $zipFile->addDirRecursive(uniqid('', false));
- }
- /**
- * @throws ZipException
- */
- public function testAddFilesFromGlobEmpty(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('The input directory is not specified');
- $zipFile = new ZipFile();
- $zipFile->addFilesFromGlob('', '*.png');
- }
- /**
- * @throws ZipException
- */
- public function testAddFilesFromGlobCantExists(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('does not exist');
- $zipFile = new ZipFile();
- $zipFile->addFilesFromGlob('path/to/path', '*.png');
- }
- /**
- * @throws ZipException
- */
- public function testAddFilesFromGlobEmptyPattern(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('The glob pattern is not specified');
- $zipFile = new ZipFile();
- $zipFile->addFilesFromGlob(__DIR__, '');
- }
- /**
- * @throws ZipException
- */
- public function testAddFilesFromGlobRecursiveEmpty(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('The input directory is not specified');
- $zipFile = new ZipFile();
- $zipFile->addFilesFromGlobRecursive('', '*.png');
- }
- /**
- * @throws ZipException
- */
- public function testAddFilesFromGlobRecursiveCantExists(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('does not exist');
- $zipFile = new ZipFile();
- $zipFile->addFilesFromGlobRecursive('path/to/path', '*.png');
- }
- /**
- * @throws ZipException
- */
- public function testAddFilesFromGlobRecursiveEmptyPattern(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('The glob pattern is not specified');
- $zipFile = new ZipFile();
- $zipFile->addFilesFromGlobRecursive(__DIR__, '');
- }
- /**
- * @throws ZipException
- */
- public function testAddFilesFromRegexDirectoryEmpty(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('The input directory is not specified');
- $zipFile = new ZipFile();
- $zipFile->addFilesFromRegex('', '~\.png$~i');
- }
- /**
- * @throws ZipException
- */
- public function testAddFilesFromRegexCantExists(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('does not exist');
- $zipFile = new ZipFile();
- $zipFile->addFilesFromRegex('path/to/path', '~\.png$~i');
- }
- /**
- * @throws ZipException
- */
- public function testAddFilesFromRegexEmptyPattern(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('The regex pattern is not specified');
- $zipFile = new ZipFile();
- $zipFile->addFilesFromRegex(__DIR__, '');
- }
- /**
- * @throws ZipException
- */
- public function testAddFilesFromRegexRecursiveEmpty(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('The input directory is not specified');
- $zipFile = new ZipFile();
- $zipFile->addFilesFromRegexRecursive('', '~\.png$~i');
- }
- /**
- * @throws ZipException
- */
- public function testAddFilesFromRegexRecursiveCantExists(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('does not exist');
- $zipFile = new ZipFile();
- $zipFile->addFilesFromGlobRecursive('path/to/path', '~\.png$~i');
- }
- /**
- * @throws ZipException
- */
- public function testAddFilesFromRegexRecursiveEmptyPattern(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('The regex pattern is not specified');
- $zipFile = new ZipFile();
- $zipFile->addFilesFromRegexRecursive(__DIR__, '');
- }
- /**
- * @throws ZipException
- */
- public function testSaveAsStreamBadStream(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('handle is not resource');
- $zipFile = new ZipFile();
- /** @noinspection PhpParamsInspection */
- $zipFile->saveAsStream('bad stream');
- }
- /**
- * @throws ZipException
- */
- public function testSaveAsFileNotWritable(): void
- {
- static::skipTestForWindows();
- static::skipTestForRootUser();
- static::assertTrue(mkdir($this->outputDirname, 0444, true));
- static::assertTrue(chmod($this->outputDirname, 0444));
- $this->outputFilename = $this->outputDirname . \DIRECTORY_SEPARATOR . basename($this->outputFilename);
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Permission denied');
- $zipFile = new ZipFile();
- $zipFile->saveAsFile($this->outputFilename);
- }
- /**
- * Test `ZipFile` implemented \ArrayAccess, \Countable and |iterator.
- *
- * @throws ZipException
- * @throws \Exception
- */
- public function testZipFileArrayAccessAndCountableAndIterator(): void
- {
- $files = [];
- $numFiles = random_int(20, 100);
- for ($i = 0; $i < $numFiles; $i++) {
- $files['file' . $i . '.txt'] = random_bytes(255);
- }
- $compressionMethods = [ZipCompressionMethod::STORED, ZipCompressionMethod::DEFLATED];
- if (\extension_loaded('bz2')) {
- $compressionMethods[] = ZipCompressionMethod::BZIP2;
- }
- $zipFile = new ZipFile();
- $zipFile->setCompressionLevel(ZipCompressionLevel::SUPER_FAST);
- $i = 0;
- $countMethods = \count($compressionMethods);
- foreach ($files as $entryName => $content) {
- $compressionMethod = $compressionMethods[$i++ % $countMethods];
- $zipFile->addFromString($entryName, $content, $compressionMethod);
- }
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- // Test \Countable
- static::assertSame($zipFile->count(), $numFiles);
- static::assertSame(\count($zipFile), $numFiles);
- // Test \ArrayAccess
- reset($files);
- foreach ($zipFile as $entryName => $content) {
- static::assertSame($entryName, key($files));
- static::assertSame($content, current($files));
- next($files);
- }
- // Test \Iterator
- reset($files);
- $iterator = new \ArrayIterator($zipFile);
- $iterator->rewind();
- while ($iterator->valid()) {
- $key = $iterator->key();
- $value = $iterator->current();
- static::assertSame($key, key($files));
- static::assertSame($value, current($files));
- next($files);
- $iterator->next();
- }
- $zipFile->close();
- $zipFile = new ZipFile();
- $zipFile['file1.txt'] = 'content 1';
- $zipFile['dir/file2.txt'] = 'content 1';
- $zipFile['dir/empty dir/'] = null;
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertTrue(isset($zipFile['file1.txt']));
- static::assertTrue(isset($zipFile['dir/file2.txt']));
- static::assertTrue(isset($zipFile['dir/empty dir/']));
- static::assertFalse(isset($zipFile['dir/empty dir/2/']));
- $zipFile['dir/empty dir/2/'] = null;
- unset($zipFile['dir/file2.txt'], $zipFile['dir/empty dir/']);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertTrue(isset($zipFile['file1.txt']));
- static::assertFalse(isset($zipFile['dir/file2.txt']));
- static::assertFalse(isset($zipFile['dir/empty dir/']));
- static::assertTrue(isset($zipFile['dir/empty dir/2/']));
- $zipFile->close();
- }
- /**
- * @throws ZipException
- */
- public function testArrayAccessAddFile(): void
- {
- $entryName = 'path/to/file.dat';
- $entryNameStream = 'path/to/' . basename(__FILE__);
- $zipFile = new ZipFile();
- $zipFile[$entryName] = new \SplFileInfo(__FILE__);
- $zipFile[$entryNameStream] = fopen(__FILE__, 'rb');
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertSame(\count($zipFile), 2);
- static::assertTrue(isset($zipFile[$entryName]));
- static::assertTrue(isset($zipFile[$entryNameStream]));
- static::assertSame($zipFile[$entryName], file_get_contents(__FILE__));
- static::assertSame($zipFile[$entryNameStream], file_get_contents(__FILE__));
- $zipFile->close();
- }
- /**
- * @throws ZipException
- * @throws \Exception
- */
- public function testUnknownCompressionMethod(): void
- {
- $zipFile = new ZipFile();
- $zipFile->addFromString('file', 'content', ZipEntry::UNKNOWN);
- $zipFile->addFromString('file2', base64_encode(random_bytes(512)), ZipEntry::UNKNOWN);
- static::assertSame($zipFile->getEntry('file')->getCompressionMethod(), ZipCompressionMethod::STORED);
- static::assertSame($zipFile->getEntry('file2')->getCompressionMethod(), ZipCompressionMethod::DEFLATED);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- $zipFile->openFile($this->outputFilename);
- static::assertSame($zipFile->getEntry('file')->getCompressionMethod(), ZipCompressionMethod::STORED);
- static::assertSame($zipFile->getEntry('file2')->getCompressionMethod(), ZipCompressionMethod::DEFLATED);
- $zipFile->close();
- }
- /**
- * @throws ZipException
- */
- public function testAddEmptyDirEmptyName(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Empty entry name');
- $zipFile = new ZipFile();
- $zipFile->addEmptyDir('');
- }
- public function testNotFoundEntry(): void
- {
- $this->expectException(ZipEntryNotFoundException::class);
- $this->expectExceptionMessage('"bad entry name"');
- $zipFile = new ZipFile();
- $zipFile['bad entry name'];
- }
- /**
- * Test rewrite input file.
- *
- * @throws ZipException
- */
- public function testRewriteFile(): void
- {
- $zipFile = new ZipFile();
- $zipFile['file'] = 'content';
- $zipFile['file2'] = 'content2';
- static::assertSame(\count($zipFile), 2);
- $zipFile
- ->saveAsFile($this->outputFilename)
- ->close()
- ;
- $md5file = md5_file($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertSame(\count($zipFile), 2);
- static::assertTrue(isset($zipFile['file']));
- static::assertTrue(isset($zipFile['file2']));
- $zipFile['file3'] = 'content3';
- static::assertSame(\count($zipFile), 3);
- $zipFile = $zipFile->rewrite();
- static::assertSame(\count($zipFile), 3);
- static::assertTrue(isset($zipFile['file']));
- static::assertTrue(isset($zipFile['file2']));
- static::assertTrue(isset($zipFile['file3']));
- $zipFile->close();
- static::assertNotSame(md5_file($this->outputFilename), $md5file);
- }
- /**
- * Test rewrite for string.
- *
- * @throws ZipException
- */
- public function testRewriteString(): void
- {
- $this->expectException(ZipException::class);
- $this->expectExceptionMessage('Overwrite is only supported for open local files');
- $zipFile = new ZipFile();
- $zipFile['file'] = 'content';
- $zipFile['file2'] = 'content2';
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- $zipFile->openFromString(file_get_contents($this->outputFilename));
- static::assertSame(\count($zipFile), 2);
- static::assertTrue(isset($zipFile['file']));
- static::assertTrue(isset($zipFile['file2']));
- $zipFile['file3'] = 'content3';
- $zipFile = $zipFile->rewrite();
- static::assertSame(\count($zipFile), 3);
- static::assertTrue(isset($zipFile['file']));
- static::assertTrue(isset($zipFile['file2']));
- static::assertTrue(isset($zipFile['file3']));
- $zipFile->close();
- }
- /**
- * @throws ZipException
- */
- public function testRewriteNullStream(): void
- {
- $this->expectException(ZipException::class);
- $this->expectExceptionMessage('input stream is null');
- $zipFile = new ZipFile();
- $zipFile->rewrite();
- }
- /**
- * Checks the ability to overwrite an open zip file with a relative path.
- *
- * @throws ZipException
- */
- public function testRewriteRelativeFile(): void
- {
- $zipFile = new ZipFile();
- $zipFile['entry.txt'] = 'test';
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- $outputDirname = \dirname($this->outputFilename);
- static::assertTrue(chdir($outputDirname));
- $relativeFilename = basename($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- $zipFile['entry2.txt'] = 'test';
- $zipFile->saveAsFile($relativeFilename);
- $zipFile->close();
- self::assertCorrectZipArchive($this->outputFilename);
- }
- /**
- * Checks the ability to overwrite an open zip file with a relative path.
- *
- * @throws ZipException
- */
- public function testRewriteDifferentWinDirectorySeparator(): void
- {
- if (\DIRECTORY_SEPARATOR !== '\\') {
- static::markTestSkipped('Windows test only');
- }
- $zipFile = new ZipFile();
- $zipFile['entry.txt'] = 'test';
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- $alternativeOutputFilename = str_replace('\\', '/', $this->outputFilename);
- self::assertCorrectZipArchive($alternativeOutputFilename);
- $zipFile->openFile($this->outputFilename);
- $zipFile['entry2.txt'] = 'test';
- $zipFile->saveAsFile($alternativeOutputFilename);
- $zipFile->close();
- self::assertCorrectZipArchive($alternativeOutputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertCount(2, $zipFile);
- $zipFile->close();
- }
- /**
- * @throws ZipException
- */
- public function testRewriteRelativeFile2(): void
- {
- $this->outputFilename = basename($this->outputFilename);
- $zipFile = new ZipFile();
- $zipFile['entry.txt'] = 'test';
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- $absoluteOutputFilename = getcwd() . \DIRECTORY_SEPARATOR . $this->outputFilename;
- self::assertCorrectZipArchive($absoluteOutputFilename);
- $zipFile->openFile($this->outputFilename);
- $zipFile['entry2.txt'] = 'test';
- $zipFile->saveAsFile($absoluteOutputFilename);
- $zipFile->close();
- self::assertCorrectZipArchive($absoluteOutputFilename);
- }
- /**
- * @throws ZipException
- */
- public function testFilename0(): void
- {
- $zipFile = new ZipFile();
- $zipFile[0] = '0';
- static::assertTrue(isset($zipFile['0']));
- static::assertCount(1, $zipFile);
- $zipFile
- ->saveAsFile($this->outputFilename)
- ->close()
- ;
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertTrue(isset($zipFile[0]));
- static::assertTrue(isset($zipFile['0']));
- static::assertSame($zipFile['0'], '0');
- static::assertCount(1, $zipFile);
- $zipFile->close();
- static::assertTrue(unlink($this->outputFilename));
- $zipFile = new ZipFile();
- $zipFile->addFromString('0', '0');
- static::assertTrue(isset($zipFile[0]));
- static::assertTrue(isset($zipFile['0']));
- static::assertCount(1, $zipFile);
- $zipFile
- ->saveAsFile($this->outputFilename)
- ->close()
- ;
- static::assertCorrectZipArchive($this->outputFilename);
- }
- /**
- * @throws ZipException
- */
- public function testOutputAsPsr7Response(): void
- {
- $zipFile = new ZipFile();
- for ($i = 0; $i < 10; $i++) {
- $zipFile[$i] = (string) $i;
- }
- $filename = 'file.jar';
- $response = $zipFile->outputAsPsr7Response(new Response(), $filename);
- static::assertSame('application/java-archive', $response->getHeaderLine('content-type'));
- static::assertSame('attachment; filename="file.jar"', $response->getHeaderLine('content-disposition'));
- }
- /**
- * @throws ZipException
- */
- public function testOutputAsSymfonyResponse(): void
- {
- $zipFile = new ZipFile();
- for ($i = 0; $i < 10; $i++) {
- $zipFile[$i] = (string) $i;
- }
- $filename = 'file.jar';
- $response = $zipFile->outputAsSymfonyResponse($filename);
- static::assertSame('application/java-archive', $response->headers->get('content-type'));
- static::assertSame('attachment; filename="file.jar"', $response->headers->get('content-disposition'));
- }
- /**
- * @dataProvider provideCompressionLevels
- *
- * @throws ZipEntryNotFoundException
- * @throws ZipException
- * @throws \Exception
- */
- public function testCompressionLevel(int $compressionLevel): void
- {
- $fileContent = random_bytes(512);
- $entryName = 'file.txt';
- $zipFile = new ZipFile();
- $zipFile
- ->addFromString($entryName, $fileContent, ZipCompressionMethod::DEFLATED)
- ->setCompressionLevelEntry($entryName, $compressionLevel)
- ->saveAsFile($this->outputFilename)
- ->close()
- ;
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertSame($zipFile->getEntryContents($entryName), $fileContent);
- static::assertSame($zipFile->getEntry($entryName)->getCompressionLevel(), $compressionLevel);
- $zipFile->close();
- }
- public function provideCompressionLevels(): array
- {
- return [
- [ZipCompressionLevel::MAXIMUM],
- [ZipCompressionLevel::NORMAL],
- [ZipCompressionLevel::FAST],
- [ZipCompressionLevel::SUPER_FAST],
- ];
- }
- /**
- * @throws ZipException
- */
- public function testInvalidCompressionLevel(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Invalid compression level');
- $zipFile = new ZipFile();
- $zipFile->addFromString('file', 'content');
- $zipFile->setCompressionLevel(15);
- }
- /**
- * @throws ZipException
- */
- public function testInvalidCompressionLevelEntry(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Invalid compression level');
- $zipFile = new ZipFile();
- $zipFile->addFromString('file', 'content');
- $zipFile->setCompressionLevelEntry('file', 15);
- }
- /**
- * @throws ZipException
- */
- public function testCompressionGlobal(): void
- {
- $zipFile = new ZipFile();
- for ($i = 0; $i < 10; $i++) {
- $zipFile->addFromString('file' . $i, 'content', ZipCompressionMethod::DEFLATED);
- }
- $zipFile
- ->setCompressionLevel(ZipCompressionLevel::SUPER_FAST)
- ->saveAsFile($this->outputFilename)
- ->close()
- ;
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- $zipEntries = $zipFile->getEntries();
- array_walk(
- $zipEntries,
- function (ZipEntry $zipEntry): void {
- $this->assertSame($zipEntry->getCompressionLevel(), ZipCompressionLevel::SUPER_FAST);
- }
- );
- $zipFile->close();
- }
- /**
- * @throws ZipException
- */
- public function testCompressionMethodEntry(): void
- {
- $zipFile = new ZipFile();
- $zipFile->addFromString('file', 'content', ZipCompressionMethod::STORED);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- $zipFile->openFile($this->outputFilename);
- static::assertSame($zipFile->getEntry('file')->getCompressionMethod(), ZipCompressionMethod::STORED);
- $zipFile->setCompressionMethodEntry('file', ZipCompressionMethod::DEFLATED);
- static::assertSame($zipFile->getEntry('file')->getCompressionMethod(), ZipCompressionMethod::DEFLATED);
- $zipFile->rewrite();
- static::assertSame($zipFile->getEntry('file')->getCompressionMethod(), ZipCompressionMethod::DEFLATED);
- }
- /**
- * @throws ZipException
- */
- public function testInvalidCompressionMethodEntry(): void
- {
- $this->expectException(ZipUnsupportMethodException::class);
- $this->expectExceptionMessage('Compression method 99 (AES Encryption) is not supported.');
- $zipFile = new ZipFile();
- $zipFile->addFromString('file', 'content', ZipCompressionMethod::STORED);
- $zipFile->setCompressionMethodEntry('file', 99);
- $zipFile->outputAsString();
- }
- /**
- * @throws ZipException
- */
- public function testUnchangeAll(): void
- {
- $zipFile = new ZipFile();
- for ($i = 0; $i < 10; $i++) {
- $zipFile[$i] = $i;
- }
- $zipFile->setArchiveComment('comment');
- static::assertCount(10, $zipFile);
- static::assertSame($zipFile->getArchiveComment(), 'comment');
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->unchangeAll();
- static::assertCount(0, $zipFile);
- static::assertNull($zipFile->getArchiveComment());
- $zipFile->close();
- $zipFile->openFile($this->outputFilename);
- static::assertCount(10, $zipFile);
- static::assertSame($zipFile->getArchiveComment(), 'comment');
- for ($i = 10; $i < 100; $i++) {
- $zipFile[$i] = $i;
- }
- $zipFile->setArchiveComment('comment 2');
- static::assertCount(100, $zipFile);
- static::assertSame($zipFile->getArchiveComment(), 'comment 2');
- $zipFile->unchangeAll();
- static::assertCount(10, $zipFile);
- static::assertSame($zipFile->getArchiveComment(), 'comment');
- $zipFile->close();
- }
- /**
- * @throws ZipException
- */
- public function testUnchangeArchiveComment(): void
- {
- $zipFile = new ZipFile();
- for ($i = 0; $i < 10; $i++) {
- $zipFile[$i] = $i;
- }
- $zipFile->setArchiveComment('comment');
- static::assertSame($zipFile->getArchiveComment(), 'comment');
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->unchangeArchiveComment();
- static::assertNull($zipFile->getArchiveComment());
- $zipFile->close();
- $zipFile->openFile($this->outputFilename);
- static::assertSame($zipFile->getArchiveComment(), 'comment');
- $zipFile->setArchiveComment('comment 2');
- static::assertSame($zipFile->getArchiveComment(), 'comment 2');
- $zipFile->unchangeArchiveComment();
- static::assertSame($zipFile->getArchiveComment(), 'comment');
- $zipFile->close();
- }
- /**
- * @throws ZipException
- */
- public function testUnchangeEntry(): void
- {
- $zipFile = new ZipFile();
- $zipFile['file 1'] = 'content 1';
- $zipFile['file 2'] = 'content 2';
- $zipFile
- ->saveAsFile($this->outputFilename)
- ->close()
- ;
- $zipFile->openFile($this->outputFilename);
- $zipFile['file 1'] = 'modify content 1';
- $zipFile->setPasswordEntry('file 1', 'password');
- static::assertSame($zipFile['file 1'], 'modify content 1');
- static::assertTrue($zipFile->getEntry('file 1')->isEncrypted());
- static::assertSame($zipFile['file 2'], 'content 2');
- static::assertFalse($zipFile->getEntry('file 2')->isEncrypted());
- $zipFile->unchangeEntry('file 1');
- static::assertSame($zipFile['file 1'], 'content 1');
- static::assertFalse($zipFile->getEntry('file 1')->isEncrypted());
- static::assertSame($zipFile['file 2'], 'content 2');
- static::assertFalse($zipFile->getEntry('file 2')->isEncrypted());
- $zipFile->close();
- }
- /**
- * @runInSeparateProcess
- *
- * @dataProvider provideOutputAsAttachment
- *
- * @param ?string $mimeType
- *
- * @throws ZipException
- */
- public function testOutputAsAttachment(string $zipFilename, ?string $mimeType, string $expectedMimeType, bool $attachment, string $expectedAttachment): void
- {
- $zipFile = new ZipFile();
- $file1Contents = 'content 1';
- $zipFile['file 1'] = $file1Contents;
- ob_start();
- $zipFile->outputAsAttachment($zipFilename, $mimeType, $attachment);
- $zipContents = ob_get_clean();
- $zipFile->close();
- $length = \strlen($zipContents);
- static::assertTrue($length > 0);
- $zipFile->openFromString($zipContents);
- static::assertSame($zipFile['file 1'], $file1Contents);
- $zipFile->close();
- if (\function_exists('xdebug_get_headers')) {
- $expectedHeaders = [
- 'Content-Disposition: ' . $expectedAttachment . '; filename="' . $zipFilename . '"',
- 'Content-Type: ' . $expectedMimeType,
- 'Content-Length: ' . $length,
- ];
- /** @noinspection ForgottenDebugOutputInspection */
- /** @noinspection PhpComposerExtensionStubsInspection */
- static::assertSame($expectedHeaders, xdebug_get_headers());
- }
- }
- public function provideOutputAsAttachment(): array
- {
- return [
- ['file.zip', null, 'application/zip', true, 'attachment'],
- ['file.zip', 'application/x-zip', 'application/x-zip', false, 'inline'],
- ['file.apk', null, 'application/vnd.android.package-archive', true, 'attachment'],
- ];
- }
- /**
- * @dataProvider provideGetEntryStream
- *
- * @throws ZipEntryNotFoundException
- * @throws ZipException
- */
- public function testReopenEntryStream(ZipFile $zipFile, string $entryName, string $contents): void
- {
- for ($i = 0; $i < 2; $i++) {
- $fp = $zipFile->getEntryStream($entryName);
- static::assertIsResource($fp);
- static::assertSame(stream_get_contents($fp), $contents);
- fclose($fp);
- }
- $zipFile->close();
- }
- /**
- * @throws \Exception
- */
- public function provideGetEntryStream(): array
- {
- $entryName = 'entry';
- $contents = random_bytes(1024);
- $zipFileSpl = new ZipFile();
- $zipFileSpl->addSplFile(new \SplFileInfo(__FILE__), $entryName);
- return [
- [(new ZipFile())->addFromString($entryName, $contents), $entryName, $contents],
- [(new ZipFile())->addFile(__FILE__, $entryName), $entryName, file_get_contents(__FILE__)],
- [
- (new ZipFile())->addFromStream(fopen(__FILE__, 'rb'), $entryName),
- $entryName,
- file_get_contents(__FILE__),
- ],
- [$zipFileSpl, $entryName, file_get_contents(__FILE__)],
- ];
- }
- /**
- * @throws ZipException
- */
- public function testGetEntries(): void
- {
- $zipFile = new ZipFile();
- for ($i = 0; $i < 100; $i++) {
- $zipFile->addFromString($i . '.txt', 'contents ' . $i);
- }
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- $zipFile->openFile($this->outputFilename);
- $zipEntries = $zipFile->getEntries();
- static::assertCount(100, $zipEntries);
- foreach ($zipEntries as $zipEntry) {
- static::assertInstanceOf(ZipEntry::class, $zipEntry);
- static::assertNotSame($zipEntry->getDosTime(), 0);
- $zipEntry->setDosTime(0);
- $zipEntry->setCreatedOS(ZipPlatform::OS_DOS);
- $zipEntry->setExtractedOS(ZipPlatform::OS_DOS);
- $zipEntry->setInternalAttributes(1);
- $zipEntry->setExternalAttributes(0);
- }
- $zipFile->rewrite();
- self::assertCorrectZipArchive($this->outputFilename);
- foreach ($zipFile->getEntries() as $zipEntry) {
- static::assertSame($zipEntry->getDosTime(), 0);
- static::assertSame($zipEntry->getExtractedOS(), ZipPlatform::OS_DOS);
- static::assertSame($zipEntry->getCreatedOS(), ZipPlatform::OS_DOS);
- static::assertSame($zipEntry->getInternalAttributes(), 1);
- static::assertSame($zipEntry->getExternalAttributes(), 0);
- }
- $zipFile->close();
- }
- /**
- * @throws ZipException
- */
- public function testRenameWithRecompressData(): void
- {
- $entryName = 'file.txt';
- $newEntryName = 'rename_file.txt';
- $contents = str_repeat('Test' . \PHP_EOL, 1024);
- $zipFile = new ZipFile();
- $zipFile->addFromString($entryName, $contents, ZipCompressionMethod::DEFLATED);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- self::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- $zipFile->rename($entryName, $newEntryName);
- $zipFile->setCompressionMethodEntry($newEntryName, ZipCompressionMethod::STORED);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- self::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertSame($zipFile->getEntry($newEntryName)->getCompressionMethod(), ZipCompressionMethod::STORED);
- $zipFile->close();
- }
- /**
- * @throws ZipEntryNotFoundException
- * @throws ZipException
- */
- public function testCloneZipContainerInZipWriter(): void
- {
- $zipFile = new ZipFile();
- $zipFile['file 1'] = 'contents';
- $zipEntryBeforeWrite = $zipFile->getEntry('file 1');
- $zipFile->saveAsFile($this->outputFilename);
- $zipAfterBeforeWrite = $zipFile->getEntry('file 1');
- static::assertSame($zipAfterBeforeWrite, $zipEntryBeforeWrite);
- $zipFile->close();
- }
- /**
- * @throws ZipException
- */
- public function testMultiSave(): void
- {
- $zipFile = new ZipFile();
- $zipFile['file 1'] = 'contents';
- for ($i = 0; $i < 10; $i++) {
- $zipFile->saveAsFile($this->outputFilename);
- self::assertCorrectZipArchive($this->outputFilename);
- }
- $zipFile->close();
- }
- /**
- * @throws ZipEntryNotFoundException
- * @throws ZipException
- */
- public function testNoData(): void
- {
- $this->expectException(ZipException::class);
- $this->expectExceptionMessage('No data for zip entry file');
- $entryName = 'file';
- $zipFile = new ZipFile();
- try {
- $zipFile[$entryName] = '';
- $zipEntry = $zipFile->getEntry($entryName);
- $zipEntry->setData(null);
- $zipFile->getEntryContents($entryName);
- } finally {
- $zipFile->close();
- }
- }
- /**
- * @throws ZipEntryNotFoundException
- * @throws ZipException
- */
- public function testReplaceEntryContentsByFile(): void
- {
- $entryName = basename(__FILE__);
- $zipFile = new ZipFile();
- $zipFile[$entryName] = 'contents';
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- $zipFile->openFile($this->outputFilename);
- $entry = $zipFile->getEntry($entryName);
- $data = new ZipFileData($entry, new \SplFileInfo(__FILE__));
- $entry->setData($data);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- self::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertSame(
- $zipFile->getEntryContents($entryName),
- file_get_contents(__FILE__)
- );
- $zipFile->close();
- }
- }
|