ZipFileAddDirTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the nelexa/zip package.
  5. * (c) Ne-Lexa <https://github.com/Ne-Lexa/php-zip>
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace PhpZip\Tests;
  10. use PhpZip\Exception\ZipException;
  11. use PhpZip\Util\Iterator\IgnoreFilesFilterIterator;
  12. use PhpZip\Util\Iterator\IgnoreFilesRecursiveFilterIterator;
  13. use PhpZip\ZipFile;
  14. /**
  15. * Test add directory to zip archive.
  16. *
  17. * @internal
  18. *
  19. * @small
  20. */
  21. class ZipFileAddDirTest extends ZipFileSetTestCase
  22. {
  23. /**
  24. * @throws ZipException
  25. */
  26. public function testAddDirWithLocalPath(): void
  27. {
  28. $localPath = 'to/path';
  29. $zipFile = new ZipFile();
  30. $zipFile->addDir($this->outputDirname, $localPath);
  31. $zipFile->saveAsFile($this->outputFilename);
  32. $zipFile->close();
  33. static::assertCorrectZipArchive($this->outputFilename);
  34. $zipFile->openFile($this->outputFilename);
  35. static::assertFilesResult(
  36. $zipFile,
  37. [
  38. '.hidden',
  39. 'text file.txt',
  40. 'Текстовый документ.txt',
  41. 'empty dir/',
  42. 'LoremIpsum.txt',
  43. ],
  44. $localPath
  45. );
  46. $zipFile->close();
  47. }
  48. /**
  49. * @throws ZipException
  50. */
  51. public function testAddDirWithoutLocalPath(): void
  52. {
  53. $zipFile = new ZipFile();
  54. $zipFile->addDir($this->outputDirname);
  55. $zipFile->saveAsFile($this->outputFilename);
  56. $zipFile->close();
  57. static::assertCorrectZipArchive($this->outputFilename);
  58. $zipFile->openFile($this->outputFilename);
  59. static::assertFilesResult(
  60. $zipFile,
  61. [
  62. '.hidden',
  63. 'text file.txt',
  64. 'Текстовый документ.txt',
  65. 'empty dir/',
  66. 'LoremIpsum.txt',
  67. ]
  68. );
  69. $zipFile->close();
  70. }
  71. /**
  72. * @throws ZipException
  73. */
  74. public function testAddFilesFromIterator(): void
  75. {
  76. $localPath = 'to/project';
  77. $directoryIterator = new \DirectoryIterator($this->outputDirname);
  78. $zipFile = new ZipFile();
  79. $zipFile->addFilesFromIterator($directoryIterator, $localPath);
  80. $zipFile->saveAsFile($this->outputFilename);
  81. $zipFile->close();
  82. static::assertCorrectZipArchive($this->outputFilename);
  83. $zipFile->openFile($this->outputFilename);
  84. static::assertFilesResult(
  85. $zipFile,
  86. [
  87. '.hidden',
  88. 'text file.txt',
  89. 'Текстовый документ.txt',
  90. 'empty dir/',
  91. 'LoremIpsum.txt',
  92. ],
  93. $localPath
  94. );
  95. $zipFile->close();
  96. }
  97. /**
  98. * @throws ZipException
  99. */
  100. public function testAddFilesFromIteratorEmptyLocalPath(): void
  101. {
  102. $localPath = '';
  103. $directoryIterator = new \DirectoryIterator($this->outputDirname);
  104. $zipFile = new ZipFile();
  105. $zipFile->addFilesFromIterator($directoryIterator, $localPath);
  106. $zipFile->saveAsFile($this->outputFilename);
  107. $zipFile->close();
  108. static::assertCorrectZipArchive($this->outputFilename);
  109. $zipFile->openFile($this->outputFilename);
  110. static::assertFilesResult(
  111. $zipFile,
  112. [
  113. '.hidden',
  114. 'text file.txt',
  115. 'Текстовый документ.txt',
  116. 'empty dir/',
  117. 'LoremIpsum.txt',
  118. ]
  119. );
  120. $zipFile->close();
  121. }
  122. /**
  123. * @throws ZipException
  124. */
  125. public function testAddFilesFromRecursiveIterator(): void
  126. {
  127. $localPath = 'to/project';
  128. $directoryIterator = new \RecursiveDirectoryIterator($this->outputDirname);
  129. $zipFile = new ZipFile();
  130. $zipFile->addFilesFromIterator($directoryIterator, $localPath);
  131. $zipFile->saveAsFile($this->outputFilename);
  132. $zipFile->close();
  133. static::assertCorrectZipArchive($this->outputFilename);
  134. $zipFile->openFile($this->outputFilename);
  135. static::assertFilesResult($zipFile, array_keys(self::$files), $localPath);
  136. $zipFile->close();
  137. }
  138. /**
  139. * @throws ZipException
  140. */
  141. public function testAddRecursiveDirWithLocalPath(): void
  142. {
  143. $localPath = 'to/path';
  144. $zipFile = new ZipFile();
  145. $zipFile->addDirRecursive($this->outputDirname, $localPath);
  146. $zipFile->saveAsFile($this->outputFilename);
  147. $zipFile->close();
  148. static::assertCorrectZipArchive($this->outputFilename);
  149. $zipFile->openFile($this->outputFilename);
  150. static::assertFilesResult($zipFile, array_keys(self::$files), $localPath);
  151. $zipFile->close();
  152. }
  153. /**
  154. * @throws ZipException
  155. */
  156. public function testAddRecursiveDirWithoutLocalPath(): void
  157. {
  158. $zipFile = new ZipFile();
  159. $zipFile->addDirRecursive($this->outputDirname);
  160. $zipFile->saveAsFile($this->outputFilename);
  161. $zipFile->close();
  162. static::assertCorrectZipArchive($this->outputFilename);
  163. $zipFile->openFile($this->outputFilename);
  164. static::assertFilesResult($zipFile, array_keys(self::$files));
  165. $zipFile->close();
  166. }
  167. /**
  168. * @throws ZipException
  169. */
  170. public function testAddFilesFromIteratorWithIgnoreFiles(): void
  171. {
  172. $localPath = 'to/project';
  173. $ignoreFiles = [
  174. 'Текстовый документ.txt',
  175. 'empty dir/',
  176. 'LoremIpsum.txt',
  177. ];
  178. $directoryIterator = new \DirectoryIterator($this->outputDirname);
  179. $ignoreIterator = new IgnoreFilesFilterIterator($directoryIterator, $ignoreFiles);
  180. $zipFile = new ZipFile();
  181. $zipFile->addFilesFromIterator($ignoreIterator, $localPath);
  182. $zipFile->saveAsFile($this->outputFilename);
  183. $zipFile->close();
  184. static::assertCorrectZipArchive($this->outputFilename);
  185. $zipFile->openFile($this->outputFilename);
  186. static::assertFilesResult(
  187. $zipFile,
  188. [
  189. '.hidden',
  190. 'text file.txt',
  191. ],
  192. $localPath
  193. );
  194. $zipFile->close();
  195. }
  196. /**
  197. * @throws ZipException
  198. */
  199. public function testAddFilesFromRecursiveIteratorWithIgnoreFiles(): void
  200. {
  201. $localPath = 'to/project';
  202. $ignoreFiles = [
  203. '.hidden',
  204. 'empty dir2/ещё пустой каталог/',
  205. 'list.txt',
  206. 'category/Pictures/240x320',
  207. 'LoremIpsum.txt',
  208. ];
  209. $directoryIterator = new \RecursiveDirectoryIterator($this->outputDirname);
  210. $ignoreIterator = new IgnoreFilesRecursiveFilterIterator($directoryIterator, $ignoreFiles);
  211. $zipFile = new ZipFile();
  212. $zipFile->addFilesFromIterator($ignoreIterator, $localPath);
  213. $zipFile->saveAsFile($this->outputFilename);
  214. $zipFile->close();
  215. static::assertCorrectZipArchive($this->outputFilename);
  216. $zipFile->openFile($this->outputFilename);
  217. static::assertFilesResult(
  218. $zipFile,
  219. [
  220. 'text file.txt',
  221. 'Текстовый документ.txt',
  222. 'empty dir/',
  223. 'catalog/New File',
  224. 'catalog/New File 2',
  225. 'catalog/Empty Dir/',
  226. 'category/Pictures/128x160/Car/01.jpg',
  227. 'category/Pictures/128x160/Car/02.jpg',
  228. ],
  229. $localPath
  230. );
  231. $zipFile->close();
  232. }
  233. /**
  234. * Create archive and add files from glob pattern.
  235. *
  236. * @throws ZipException
  237. */
  238. public function testAddFilesFromGlob(): void
  239. {
  240. $localPath = '/';
  241. $zipFile = new ZipFile();
  242. $zipFile->addFilesFromGlob($this->outputDirname, '**.{txt,jpg}', $localPath);
  243. $zipFile->saveAsFile($this->outputFilename);
  244. $zipFile->close();
  245. static::assertCorrectZipArchive($this->outputFilename);
  246. $zipFile->openFile($this->outputFilename);
  247. static::assertFilesResult(
  248. $zipFile,
  249. [
  250. 'text file.txt',
  251. 'Текстовый документ.txt',
  252. 'LoremIpsum.txt',
  253. ],
  254. $localPath
  255. );
  256. $zipFile->close();
  257. }
  258. /**
  259. * Create archive and add recursively files from glob pattern.
  260. *
  261. * @throws ZipException
  262. */
  263. public function testAddFilesFromGlobRecursive(): void
  264. {
  265. $localPath = '/';
  266. $zipFile = new ZipFile();
  267. $zipFile->addFilesFromGlobRecursive($this->outputDirname, '**.{txt,jpg}', $localPath);
  268. $zipFile->saveAsFile($this->outputFilename);
  269. $zipFile->close();
  270. static::assertCorrectZipArchive($this->outputFilename);
  271. $zipFile->openFile($this->outputFilename);
  272. static::assertFilesResult(
  273. $zipFile,
  274. [
  275. 'text file.txt',
  276. 'Текстовый документ.txt',
  277. 'category/list.txt',
  278. 'category/Pictures/128x160/Car/01.jpg',
  279. 'category/Pictures/128x160/Car/02.jpg',
  280. 'category/Pictures/240x320/Car/01.jpg',
  281. 'category/Pictures/240x320/Car/02.jpg',
  282. 'LoremIpsum.txt',
  283. ],
  284. $localPath
  285. );
  286. $zipFile->close();
  287. }
  288. /**
  289. * Create archive and add files from regex pattern.
  290. *
  291. * @throws ZipException
  292. */
  293. public function testAddFilesFromRegex(): void
  294. {
  295. $localPath = 'path';
  296. $zipFile = new ZipFile();
  297. $zipFile->addFilesFromRegex($this->outputDirname, '~\.(txt|jpe?g)$~i', $localPath);
  298. $zipFile->saveAsFile($this->outputFilename);
  299. $zipFile->close();
  300. static::assertCorrectZipArchive($this->outputFilename);
  301. $zipFile->openFile($this->outputFilename);
  302. static::assertFilesResult(
  303. $zipFile,
  304. [
  305. 'text file.txt',
  306. 'Текстовый документ.txt',
  307. 'LoremIpsum.txt',
  308. ],
  309. $localPath
  310. );
  311. $zipFile->close();
  312. }
  313. /**
  314. * Create archive and add files recursively from regex pattern.
  315. *
  316. * @throws ZipException
  317. */
  318. public function testAddFilesFromRegexRecursive(): void
  319. {
  320. $localPath = '/';
  321. $zipFile = new ZipFile();
  322. $zipFile->addFilesFromRegexRecursive($this->outputDirname, '~\.(txt|jpe?g)$~i', $localPath);
  323. $zipFile->saveAsFile($this->outputFilename);
  324. $zipFile->close();
  325. static::assertCorrectZipArchive($this->outputFilename);
  326. $zipFile->openFile($this->outputFilename);
  327. static::assertFilesResult(
  328. $zipFile,
  329. [
  330. 'text file.txt',
  331. 'Текстовый документ.txt',
  332. 'category/list.txt',
  333. 'LoremIpsum.txt',
  334. 'category/Pictures/128x160/Car/01.jpg',
  335. 'category/Pictures/128x160/Car/02.jpg',
  336. 'category/Pictures/240x320/Car/01.jpg',
  337. 'category/Pictures/240x320/Car/02.jpg',
  338. ],
  339. $localPath
  340. );
  341. $zipFile->close();
  342. }
  343. /**
  344. * @throws ZipException
  345. */
  346. public function testArrayAccessAddDir(): void
  347. {
  348. $localPath = 'path/to';
  349. $iterator = new \RecursiveDirectoryIterator($this->outputDirname);
  350. $zipFile = new ZipFile();
  351. $zipFile[$localPath] = $iterator;
  352. $zipFile->saveAsFile($this->outputFilename);
  353. $zipFile->close();
  354. static::assertCorrectZipArchive($this->outputFilename);
  355. $zipFile->openFile($this->outputFilename);
  356. static::assertFilesResult($zipFile, array_keys(self::$files), $localPath);
  357. $zipFile->close();
  358. }
  359. }