ZipContainer.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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\Model;
  10. use PhpZip\Constants\ZipEncryptionMethod;
  11. use PhpZip\Exception\InvalidArgumentException;
  12. use PhpZip\Exception\ZipEntryNotFoundException;
  13. use PhpZip\Exception\ZipException;
  14. /**
  15. * Zip Container.
  16. */
  17. class ZipContainer extends ImmutableZipContainer
  18. {
  19. /**
  20. * @var ImmutableZipContainer|null The source container contains zip entries from
  21. * an open zip archive. The source container makes
  22. * it possible to undo changes in the archive.
  23. * When cloning, this container is not cloned.
  24. */
  25. private ?ImmutableZipContainer $sourceContainer;
  26. public function __construct(?ImmutableZipContainer $sourceContainer = null)
  27. {
  28. $entries = [];
  29. $archiveComment = null;
  30. if ($sourceContainer !== null) {
  31. foreach ($sourceContainer->getEntries() as $entryName => $entry) {
  32. $entries[$entryName] = clone $entry;
  33. }
  34. $archiveComment = $sourceContainer->getArchiveComment();
  35. }
  36. parent::__construct($entries, $archiveComment);
  37. $this->sourceContainer = $sourceContainer;
  38. }
  39. public function getSourceContainer(): ?ImmutableZipContainer
  40. {
  41. return $this->sourceContainer;
  42. }
  43. public function addEntry(ZipEntry $entry): void
  44. {
  45. $this->entries[$entry->getName()] = $entry;
  46. }
  47. /**
  48. * @param string|ZipEntry $entry
  49. */
  50. public function deleteEntry($entry): bool
  51. {
  52. $entry = $entry instanceof ZipEntry ? $entry->getName() : (string) $entry;
  53. if (isset($this->entries[$entry])) {
  54. unset($this->entries[$entry]);
  55. return true;
  56. }
  57. return false;
  58. }
  59. /**
  60. * @param string|ZipEntry $old
  61. * @param string|ZipEntry $new
  62. *
  63. * @throws ZipException
  64. *
  65. * @return ZipEntry New zip entry
  66. */
  67. public function renameEntry($old, $new): ZipEntry
  68. {
  69. $old = $old instanceof ZipEntry ? $old->getName() : (string) $old;
  70. $new = $new instanceof ZipEntry ? $new->getName() : (string) $new;
  71. if (isset($this->entries[$new])) {
  72. throw new InvalidArgumentException('New entry name ' . $new . ' is exists.');
  73. }
  74. $entry = $this->getEntry($old);
  75. $newEntry = $entry->rename($new);
  76. $this->deleteEntry($entry);
  77. $this->addEntry($newEntry);
  78. return $newEntry;
  79. }
  80. /**
  81. * @param string|ZipEntry $entryName
  82. *
  83. * @throws ZipEntryNotFoundException
  84. */
  85. public function getEntry($entryName): ZipEntry
  86. {
  87. $entry = $this->getEntryOrNull($entryName);
  88. if ($entry !== null) {
  89. return $entry;
  90. }
  91. throw new ZipEntryNotFoundException($entryName);
  92. }
  93. /**
  94. * @param string|ZipEntry $entryName
  95. */
  96. public function getEntryOrNull($entryName): ?ZipEntry
  97. {
  98. $entryName = $entryName instanceof ZipEntry ? $entryName->getName() : (string) $entryName;
  99. return $this->entries[$entryName] ?? null;
  100. }
  101. /**
  102. * @param string|ZipEntry $entryName
  103. */
  104. public function hasEntry($entryName): bool
  105. {
  106. $entryName = $entryName instanceof ZipEntry ? $entryName->getName() : (string) $entryName;
  107. return isset($this->entries[$entryName]);
  108. }
  109. /**
  110. * Delete all entries.
  111. */
  112. public function deleteAll(): void
  113. {
  114. $this->entries = [];
  115. }
  116. /**
  117. * Delete entries by regex pattern.
  118. *
  119. * @param string $regexPattern Regex pattern
  120. *
  121. * @return ZipEntry[] Deleted entries
  122. */
  123. public function deleteByRegex(string $regexPattern): array
  124. {
  125. if (empty($regexPattern)) {
  126. throw new InvalidArgumentException('The regex pattern is not specified');
  127. }
  128. /** @var ZipEntry[] $found */
  129. $found = [];
  130. foreach ($this->entries as $entryName => $entry) {
  131. if (preg_match($regexPattern, $entryName)) {
  132. $found[] = $entry;
  133. }
  134. }
  135. foreach ($found as $entry) {
  136. $this->deleteEntry($entry);
  137. }
  138. return $found;
  139. }
  140. /**
  141. * Undo all changes done in the archive.
  142. */
  143. public function unchangeAll(): void
  144. {
  145. $this->entries = [];
  146. if ($this->sourceContainer !== null) {
  147. foreach ($this->sourceContainer->getEntries() as $entry) {
  148. $this->entries[$entry->getName()] = clone $entry;
  149. }
  150. }
  151. $this->unchangeArchiveComment();
  152. }
  153. /**
  154. * Undo change archive comment.
  155. */
  156. public function unchangeArchiveComment(): void
  157. {
  158. $this->archiveComment = null;
  159. if ($this->sourceContainer !== null) {
  160. $this->archiveComment = $this->sourceContainer->archiveComment;
  161. }
  162. }
  163. /**
  164. * Revert all changes done to an entry with the given name.
  165. *
  166. * @param string|ZipEntry $entry Entry name or ZipEntry
  167. */
  168. public function unchangeEntry($entry): bool
  169. {
  170. $entry = $entry instanceof ZipEntry ? $entry->getName() : (string) $entry;
  171. if (
  172. $this->sourceContainer !== null
  173. && isset($this->entries[$entry], $this->sourceContainer->entries[$entry])
  174. ) {
  175. $this->entries[$entry] = clone $this->sourceContainer->entries[$entry];
  176. return true;
  177. }
  178. return false;
  179. }
  180. /**
  181. * Entries sort by name.
  182. *
  183. * Example:
  184. * ```php
  185. * $zipContainer->sortByName(static function (string $nameA, string $nameB): int {
  186. * return strcmp($nameA, $nameB);
  187. * });
  188. * ```
  189. */
  190. public function sortByName(callable $cmp): void
  191. {
  192. uksort($this->entries, $cmp);
  193. }
  194. /**
  195. * Entries sort by entry.
  196. *
  197. * Example:
  198. * ```php
  199. * $zipContainer->sortByEntry(static function (ZipEntry $a, ZipEntry $b): int {
  200. * return strcmp($a->getName(), $b->getName());
  201. * });
  202. * ```
  203. */
  204. public function sortByEntry(callable $cmp): void
  205. {
  206. uasort($this->entries, $cmp);
  207. }
  208. public function setArchiveComment(?string $archiveComment): void
  209. {
  210. if ($archiveComment !== null && $archiveComment !== '') {
  211. $length = \strlen($archiveComment);
  212. if ($length > 0xFFFF) {
  213. throw new InvalidArgumentException('Length comment out of range');
  214. }
  215. }
  216. $this->archiveComment = $archiveComment;
  217. }
  218. public function matcher(): ZipEntryMatcher
  219. {
  220. return new ZipEntryMatcher($this);
  221. }
  222. /**
  223. * Specify a password for extracting files.
  224. *
  225. * @param ?string $password
  226. */
  227. public function setReadPassword(?string $password): void
  228. {
  229. if ($this->sourceContainer !== null) {
  230. foreach ($this->sourceContainer->entries as $entry) {
  231. if ($entry->isEncrypted()) {
  232. $entry->setPassword($password);
  233. }
  234. }
  235. }
  236. }
  237. /**
  238. * @throws ZipEntryNotFoundException
  239. * @throws ZipException
  240. */
  241. public function setReadPasswordEntry(string $entryName, string $password): void
  242. {
  243. if (!isset($this->sourceContainer->entries[$entryName])) {
  244. throw new ZipEntryNotFoundException($entryName);
  245. }
  246. if ($this->sourceContainer->entries[$entryName]->isEncrypted()) {
  247. $this->sourceContainer->entries[$entryName]->setPassword($password);
  248. }
  249. }
  250. /**
  251. * @param ?string $writePassword
  252. *
  253. * @throws ZipEntryNotFoundException
  254. */
  255. public function setWritePassword(?string $writePassword): void
  256. {
  257. $this->matcher()->all()->setPassword($writePassword);
  258. }
  259. /**
  260. * Remove password.
  261. *
  262. * @throws ZipEntryNotFoundException
  263. */
  264. public function removePassword(): void
  265. {
  266. $this->matcher()->all()->setPassword(null);
  267. }
  268. /**
  269. * @param string|ZipEntry $entryName
  270. *
  271. * @throws ZipEntryNotFoundException
  272. */
  273. public function removePasswordEntry($entryName): void
  274. {
  275. $this->matcher()->add($entryName)->setPassword(null);
  276. }
  277. /**
  278. * @throws ZipEntryNotFoundException
  279. */
  280. public function setEncryptionMethod(int $encryptionMethod = ZipEncryptionMethod::WINZIP_AES_256): void
  281. {
  282. $this->matcher()->all()->setEncryptionMethod($encryptionMethod);
  283. }
  284. }