ZipContainer.php 9.2 KB

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