123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- <?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 PhpZip\Exception\ZipException;
- use PhpZip\Util\Iterator\IgnoreFilesFilterIterator;
- use PhpZip\Util\Iterator\IgnoreFilesRecursiveFilterIterator;
- use PhpZip\ZipFile;
- /**
- * Test add directory to zip archive.
- *
- * @internal
- *
- * @small
- */
- class ZipFileAddDirTest extends ZipFileSetTestCase
- {
- /**
- * @throws ZipException
- */
- public function testAddDirWithLocalPath(): void
- {
- $localPath = 'to/path';
- $zipFile = new ZipFile();
- $zipFile->addDir($this->outputDirname, $localPath);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertFilesResult(
- $zipFile,
- [
- '.hidden',
- 'text file.txt',
- 'Текстовый документ.txt',
- 'empty dir/',
- 'LoremIpsum.txt',
- ],
- $localPath
- );
- $zipFile->close();
- }
- /**
- * @throws ZipException
- */
- public function testAddDirWithoutLocalPath(): void
- {
- $zipFile = new ZipFile();
- $zipFile->addDir($this->outputDirname);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertFilesResult(
- $zipFile,
- [
- '.hidden',
- 'text file.txt',
- 'Текстовый документ.txt',
- 'empty dir/',
- 'LoremIpsum.txt',
- ]
- );
- $zipFile->close();
- }
- /**
- * @throws ZipException
- */
- public function testAddFilesFromIterator(): void
- {
- $localPath = 'to/project';
- $directoryIterator = new \DirectoryIterator($this->outputDirname);
- $zipFile = new ZipFile();
- $zipFile->addFilesFromIterator($directoryIterator, $localPath);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertFilesResult(
- $zipFile,
- [
- '.hidden',
- 'text file.txt',
- 'Текстовый документ.txt',
- 'empty dir/',
- 'LoremIpsum.txt',
- ],
- $localPath
- );
- $zipFile->close();
- }
- /**
- * @throws ZipException
- */
- public function testAddFilesFromIteratorEmptyLocalPath(): void
- {
- $localPath = '';
- $directoryIterator = new \DirectoryIterator($this->outputDirname);
- $zipFile = new ZipFile();
- $zipFile->addFilesFromIterator($directoryIterator, $localPath);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertFilesResult(
- $zipFile,
- [
- '.hidden',
- 'text file.txt',
- 'Текстовый документ.txt',
- 'empty dir/',
- 'LoremIpsum.txt',
- ]
- );
- $zipFile->close();
- }
- /**
- * @throws ZipException
- */
- public function testAddFilesFromRecursiveIterator(): void
- {
- $localPath = 'to/project';
- $directoryIterator = new \RecursiveDirectoryIterator($this->outputDirname);
- $zipFile = new ZipFile();
- $zipFile->addFilesFromIterator($directoryIterator, $localPath);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertFilesResult($zipFile, array_keys(self::$files), $localPath);
- $zipFile->close();
- }
- /**
- * @throws ZipException
- */
- public function testAddRecursiveDirWithLocalPath(): void
- {
- $localPath = 'to/path';
- $zipFile = new ZipFile();
- $zipFile->addDirRecursive($this->outputDirname, $localPath);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertFilesResult($zipFile, array_keys(self::$files), $localPath);
- $zipFile->close();
- }
- /**
- * @throws ZipException
- */
- public function testAddRecursiveDirWithoutLocalPath(): void
- {
- $zipFile = new ZipFile();
- $zipFile->addDirRecursive($this->outputDirname);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertFilesResult($zipFile, array_keys(self::$files));
- $zipFile->close();
- }
- /**
- * @throws ZipException
- */
- public function testAddFilesFromIteratorWithIgnoreFiles(): void
- {
- $localPath = 'to/project';
- $ignoreFiles = [
- 'Текстовый документ.txt',
- 'empty dir/',
- 'LoremIpsum.txt',
- ];
- $directoryIterator = new \DirectoryIterator($this->outputDirname);
- $ignoreIterator = new IgnoreFilesFilterIterator($directoryIterator, $ignoreFiles);
- $zipFile = new ZipFile();
- $zipFile->addFilesFromIterator($ignoreIterator, $localPath);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertFilesResult(
- $zipFile,
- [
- '.hidden',
- 'text file.txt',
- ],
- $localPath
- );
- $zipFile->close();
- }
- /**
- * @throws ZipException
- */
- public function testAddFilesFromRecursiveIteratorWithIgnoreFiles(): void
- {
- $localPath = 'to/project';
- $ignoreFiles = [
- '.hidden',
- 'empty dir2/ещё пустой каталог/',
- 'list.txt',
- 'category/Pictures/240x320',
- 'LoremIpsum.txt',
- ];
- $directoryIterator = new \RecursiveDirectoryIterator($this->outputDirname);
- $ignoreIterator = new IgnoreFilesRecursiveFilterIterator($directoryIterator, $ignoreFiles);
- $zipFile = new ZipFile();
- $zipFile->addFilesFromIterator($ignoreIterator, $localPath);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertFilesResult(
- $zipFile,
- [
- 'text file.txt',
- 'Текстовый документ.txt',
- 'empty dir/',
- 'catalog/New File',
- 'catalog/New File 2',
- 'catalog/Empty Dir/',
- 'category/Pictures/128x160/Car/01.jpg',
- 'category/Pictures/128x160/Car/02.jpg',
- ],
- $localPath
- );
- $zipFile->close();
- }
- /**
- * Create archive and add files from glob pattern.
- *
- * @throws ZipException
- */
- public function testAddFilesFromGlob(): void
- {
- $localPath = '/';
- $zipFile = new ZipFile();
- $zipFile->addFilesFromGlob($this->outputDirname, '**.{txt,jpg}', $localPath);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertFilesResult(
- $zipFile,
- [
- 'text file.txt',
- 'Текстовый документ.txt',
- 'LoremIpsum.txt',
- ],
- $localPath
- );
- $zipFile->close();
- }
- /**
- * Create archive and add recursively files from glob pattern.
- *
- * @throws ZipException
- */
- public function testAddFilesFromGlobRecursive(): void
- {
- $localPath = '/';
- $zipFile = new ZipFile();
- $zipFile->addFilesFromGlobRecursive($this->outputDirname, '**.{txt,jpg}', $localPath);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertFilesResult(
- $zipFile,
- [
- 'text file.txt',
- 'Текстовый документ.txt',
- 'category/list.txt',
- 'category/Pictures/128x160/Car/01.jpg',
- 'category/Pictures/128x160/Car/02.jpg',
- 'category/Pictures/240x320/Car/01.jpg',
- 'category/Pictures/240x320/Car/02.jpg',
- 'LoremIpsum.txt',
- ],
- $localPath
- );
- $zipFile->close();
- }
- /**
- * Create archive and add files from regex pattern.
- *
- * @throws ZipException
- */
- public function testAddFilesFromRegex(): void
- {
- $localPath = 'path';
- $zipFile = new ZipFile();
- $zipFile->addFilesFromRegex($this->outputDirname, '~\.(txt|jpe?g)$~i', $localPath);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertFilesResult(
- $zipFile,
- [
- 'text file.txt',
- 'Текстовый документ.txt',
- 'LoremIpsum.txt',
- ],
- $localPath
- );
- $zipFile->close();
- }
- /**
- * Create archive and add files recursively from regex pattern.
- *
- * @throws ZipException
- */
- public function testAddFilesFromRegexRecursive(): void
- {
- $localPath = '/';
- $zipFile = new ZipFile();
- $zipFile->addFilesFromRegexRecursive($this->outputDirname, '~\.(txt|jpe?g)$~i', $localPath);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertFilesResult(
- $zipFile,
- [
- 'text file.txt',
- 'Текстовый документ.txt',
- 'category/list.txt',
- 'LoremIpsum.txt',
- 'category/Pictures/128x160/Car/01.jpg',
- 'category/Pictures/128x160/Car/02.jpg',
- 'category/Pictures/240x320/Car/01.jpg',
- 'category/Pictures/240x320/Car/02.jpg',
- ],
- $localPath
- );
- $zipFile->close();
- }
- /**
- * @throws ZipException
- */
- public function testArrayAccessAddDir(): void
- {
- $localPath = 'path/to';
- $iterator = new \RecursiveDirectoryIterator($this->outputDirname);
- $zipFile = new ZipFile();
- $zipFile[$localPath] = $iterator;
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- static::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- static::assertFilesResult($zipFile, array_keys(self::$files), $localPath);
- $zipFile->close();
- }
- }
|