ZipContainer.php 8.9 KB

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