ZipAbstractEntry.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. <?php
  2. namespace PhpZip\Model\Entry;
  3. use PhpZip\Exception\InvalidArgumentException;
  4. use PhpZip\Exception\ZipException;
  5. use PhpZip\Extra\ExtraFieldsCollection;
  6. use PhpZip\Extra\ExtraFieldsFactory;
  7. use PhpZip\Extra\Fields\WinZipAesEntryExtraField;
  8. use PhpZip\Model\ZipEntry;
  9. use PhpZip\Util\DateTimeConverter;
  10. use PhpZip\Util\StringUtil;
  11. use PhpZip\ZipFile;
  12. /**
  13. * Abstract ZIP entry.
  14. *
  15. * @see https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT .ZIP File Format Specification
  16. *
  17. * @author Ne-Lexa alexey@nelexa.ru
  18. * @license MIT
  19. */
  20. abstract class ZipAbstractEntry implements ZipEntry
  21. {
  22. /** @var string Entry name (filename in archive) */
  23. private $name;
  24. /** @var int Made by platform */
  25. private $createdOS = self::UNKNOWN;
  26. /** @var int Extracted by platform */
  27. private $extractedOS = self::UNKNOWN;
  28. /** @var int */
  29. private $softwareVersion = self::UNKNOWN;
  30. /** @var int */
  31. private $versionNeededToExtract = self::UNKNOWN;
  32. /** @var int Compression method */
  33. private $method = self::UNKNOWN;
  34. /** @var int */
  35. private $generalPurposeBitFlags = 0;
  36. /** @var int Dos time */
  37. private $dosTime = self::UNKNOWN;
  38. /** @var int Crc32 */
  39. private $crc = self::UNKNOWN;
  40. /** @var int Compressed size */
  41. private $compressedSize = self::UNKNOWN;
  42. /** @var int Uncompressed size */
  43. private $size = self::UNKNOWN;
  44. /** @var int Internal attributes */
  45. private $internalAttributes = 0;
  46. /** @var int External attributes */
  47. private $externalAttributes = 0;
  48. /** @var int relative Offset Of Local File Header */
  49. private $offset = 0;
  50. /**
  51. * Collections of Extra Fields.
  52. * Keys from Header ID [int] and value Extra Field [ExtraField].
  53. * Should be null or may be empty if no Extra Fields are used.
  54. *
  55. * @var ExtraFieldsCollection
  56. */
  57. private $extraFieldsCollection;
  58. /** @var string|null comment field */
  59. private $comment;
  60. /** @var string entry password for read or write encryption data */
  61. private $password;
  62. /**
  63. * Encryption method.
  64. *
  65. * @see ZipFile::ENCRYPTION_METHOD_TRADITIONAL
  66. * @see ZipFile::ENCRYPTION_METHOD_WINZIP_AES_128
  67. * @see ZipFile::ENCRYPTION_METHOD_WINZIP_AES_192
  68. * @see ZipFile::ENCRYPTION_METHOD_WINZIP_AES_256
  69. *
  70. * @var int
  71. */
  72. private $encryptionMethod = ZipFile::ENCRYPTION_METHOD_TRADITIONAL;
  73. /** @var int */
  74. private $compressionLevel = ZipFile::LEVEL_DEFAULT_COMPRESSION;
  75. /**
  76. * ZipAbstractEntry constructor.
  77. */
  78. public function __construct()
  79. {
  80. $this->extraFieldsCollection = new ExtraFieldsCollection();
  81. }
  82. /**
  83. * @param ZipEntry $entry
  84. *
  85. * @throws ZipException
  86. */
  87. public function setEntry(ZipEntry $entry)
  88. {
  89. $this->setName($entry->getName());
  90. $this->setSoftwareVersion($entry->getSoftwareVersion());
  91. $this->setCreatedOS($entry->getCreatedOS());
  92. $this->setExtractedOS($entry->getExtractedOS());
  93. $this->setVersionNeededToExtract($entry->getVersionNeededToExtract());
  94. $this->setMethod($entry->getMethod());
  95. $this->setGeneralPurposeBitFlags($entry->getGeneralPurposeBitFlags());
  96. $this->setDosTime($entry->getDosTime());
  97. $this->setCrc($entry->getCrc());
  98. $this->setCompressedSize($entry->getCompressedSize());
  99. $this->setSize($entry->getSize());
  100. $this->setInternalAttributes($entry->getInternalAttributes());
  101. $this->setExternalAttributes($entry->getExternalAttributes());
  102. $this->setOffset($entry->getOffset());
  103. $this->setExtra($entry->getExtra());
  104. $this->setComment($entry->getComment());
  105. $this->setPassword($entry->getPassword());
  106. $this->setEncryptionMethod($entry->getEncryptionMethod());
  107. $this->setCompressionLevel($entry->getCompressionLevel());
  108. $this->setEncrypted($entry->isEncrypted());
  109. }
  110. /**
  111. * Returns the ZIP entry name.
  112. *
  113. * @return string
  114. */
  115. public function getName()
  116. {
  117. return $this->name;
  118. }
  119. /**
  120. * Set entry name.
  121. *
  122. * @param string $name New entry name
  123. *
  124. * @throws ZipException
  125. *
  126. * @return ZipEntry
  127. */
  128. public function setName($name)
  129. {
  130. $length = \strlen($name);
  131. if ($length < 0x0000 || $length > 0xffff) {
  132. throw new ZipException('Illegal zip entry name parameter');
  133. }
  134. $this->setGeneralPurposeBitFlag(self::GPBF_UTF8, true);
  135. $this->name = $name;
  136. $this->externalAttributes = $this->isDirectory() ? 0x10 : 0;
  137. return $this;
  138. }
  139. /**
  140. * Sets the indexed General Purpose Bit Flag.
  141. *
  142. * @param int $mask
  143. * @param bool $bit
  144. *
  145. * @return ZipEntry
  146. */
  147. public function setGeneralPurposeBitFlag($mask, $bit)
  148. {
  149. if ($bit) {
  150. $this->generalPurposeBitFlags |= $mask;
  151. } else {
  152. $this->generalPurposeBitFlags &= ~$mask;
  153. }
  154. return $this;
  155. }
  156. /**
  157. * @return int Get platform
  158. *
  159. * @deprecated Use {@see ZipEntry::getCreatedOS()}
  160. */
  161. public function getPlatform()
  162. {
  163. @trigger_error('ZipEntry::getPlatform() is deprecated. Use ZipEntry::getCreatedOS()', \E_USER_DEPRECATED);
  164. return $this->getCreatedOS();
  165. }
  166. /**
  167. * @param int $platform
  168. *
  169. * @throws ZipException
  170. *
  171. * @return ZipEntry
  172. *
  173. * @deprecated Use {@see ZipEntry::setCreatedOS()}
  174. */
  175. public function setPlatform($platform)
  176. {
  177. @trigger_error('ZipEntry::setPlatform() is deprecated. Use ZipEntry::setCreatedOS()', \E_USER_DEPRECATED);
  178. return $this->setCreatedOS($platform);
  179. }
  180. /**
  181. * @return int platform
  182. */
  183. public function getCreatedOS()
  184. {
  185. return $this->createdOS;
  186. }
  187. /**
  188. * Set platform.
  189. *
  190. * @param int $platform
  191. *
  192. * @throws ZipException
  193. *
  194. * @return ZipEntry
  195. */
  196. public function setCreatedOS($platform)
  197. {
  198. $platform = (int) $platform;
  199. if ($platform < 0x00 || $platform > 0xff) {
  200. throw new ZipException('Platform out of range');
  201. }
  202. $this->createdOS = $platform;
  203. return $this;
  204. }
  205. /**
  206. * @return int
  207. */
  208. public function getExtractedOS()
  209. {
  210. return $this->extractedOS;
  211. }
  212. /**
  213. * Set extracted OS.
  214. *
  215. * @param int $platform
  216. *
  217. * @throws ZipException
  218. *
  219. * @return ZipEntry
  220. */
  221. public function setExtractedOS($platform)
  222. {
  223. $platform = (int) $platform;
  224. if ($platform < 0x00 || $platform > 0xff) {
  225. throw new ZipException('Platform out of range');
  226. }
  227. $this->extractedOS = $platform;
  228. return $this;
  229. }
  230. /**
  231. * @return int
  232. */
  233. public function getSoftwareVersion()
  234. {
  235. return $this->softwareVersion;
  236. }
  237. /**
  238. * @param int $softwareVersion
  239. *
  240. * @return ZipEntry
  241. */
  242. public function setSoftwareVersion($softwareVersion)
  243. {
  244. $this->softwareVersion = (int) $softwareVersion;
  245. return $this;
  246. }
  247. /**
  248. * Version needed to extract.
  249. *
  250. * @return int
  251. */
  252. public function getVersionNeededToExtract()
  253. {
  254. if ($this->versionNeededToExtract === self::UNKNOWN) {
  255. $method = $this->getMethod();
  256. if ($method === self::METHOD_WINZIP_AES) {
  257. return 51;
  258. }
  259. if ($method === ZipFile::METHOD_BZIP2) {
  260. return 46;
  261. }
  262. if ($this->isZip64ExtensionsRequired()) {
  263. return 45;
  264. }
  265. return $method === ZipFile::METHOD_DEFLATED || $this->isDirectory() ? 20 : 10;
  266. }
  267. return $this->versionNeededToExtract;
  268. }
  269. /**
  270. * Set version needed to extract.
  271. *
  272. * @param int $version
  273. *
  274. * @return ZipEntry
  275. */
  276. public function setVersionNeededToExtract($version)
  277. {
  278. $this->versionNeededToExtract = $version;
  279. return $this;
  280. }
  281. /**
  282. * @return bool
  283. */
  284. public function isZip64ExtensionsRequired()
  285. {
  286. return $this->getCompressedSize() >= 0xffffffff
  287. || $this->getSize() >= 0xffffffff;
  288. }
  289. /**
  290. * Returns the compressed size of this entry.
  291. *
  292. * @see int
  293. */
  294. public function getCompressedSize()
  295. {
  296. return $this->compressedSize;
  297. }
  298. /**
  299. * Sets the compressed size of this entry.
  300. *
  301. * @param int $compressedSize the Compressed Size
  302. *
  303. * @return ZipEntry
  304. */
  305. public function setCompressedSize($compressedSize)
  306. {
  307. $this->compressedSize = $compressedSize;
  308. return $this;
  309. }
  310. /**
  311. * Returns the uncompressed size of this entry.
  312. *
  313. * @see ZipEntry::setCompressedSize
  314. */
  315. public function getSize()
  316. {
  317. return $this->size;
  318. }
  319. /**
  320. * Sets the uncompressed size of this entry.
  321. *
  322. * @param int $size the (Uncompressed) Size
  323. *
  324. * @return ZipEntry
  325. */
  326. public function setSize($size)
  327. {
  328. $this->size = $size;
  329. return $this;
  330. }
  331. /**
  332. * Return relative Offset Of Local File Header.
  333. *
  334. * @return int
  335. */
  336. public function getOffset()
  337. {
  338. return $this->offset;
  339. }
  340. /**
  341. * @param int $offset
  342. *
  343. * @return ZipEntry
  344. */
  345. public function setOffset($offset)
  346. {
  347. $this->offset = (int) $offset;
  348. return $this;
  349. }
  350. /**
  351. * Returns the General Purpose Bit Flags.
  352. *
  353. * @return int
  354. */
  355. public function getGeneralPurposeBitFlags()
  356. {
  357. return $this->generalPurposeBitFlags & 0xffff;
  358. }
  359. /**
  360. * Sets the General Purpose Bit Flags.
  361. *
  362. * @param mixed $general
  363. *
  364. * @throws ZipException
  365. *
  366. * @return ZipEntry
  367. *
  368. * @var int general
  369. */
  370. public function setGeneralPurposeBitFlags($general)
  371. {
  372. if ($general < 0x0000 || $general > 0xffff) {
  373. throw new ZipException('general out of range');
  374. }
  375. $this->generalPurposeBitFlags = $general;
  376. if ($this->method === ZipFile::METHOD_DEFLATED) {
  377. $bit1 = $this->getGeneralPurposeBitFlag(self::GPBF_COMPRESSION_FLAG1);
  378. $bit2 = $this->getGeneralPurposeBitFlag(self::GPBF_COMPRESSION_FLAG2);
  379. if ($bit1 && !$bit2) {
  380. $this->compressionLevel = ZipFile::LEVEL_BEST_COMPRESSION;
  381. } elseif (!$bit1 && $bit2) {
  382. $this->compressionLevel = ZipFile::LEVEL_FAST;
  383. } elseif ($bit1 && $bit2) {
  384. $this->compressionLevel = ZipFile::LEVEL_SUPER_FAST;
  385. } else {
  386. $this->compressionLevel = ZipFile::LEVEL_DEFAULT_COMPRESSION;
  387. }
  388. }
  389. return $this;
  390. }
  391. /**
  392. * Returns true if and only if this ZIP entry is encrypted.
  393. *
  394. * @return bool
  395. */
  396. public function isEncrypted()
  397. {
  398. return $this->getGeneralPurposeBitFlag(self::GPBF_ENCRYPTED);
  399. }
  400. /**
  401. * Returns the indexed General Purpose Bit Flag.
  402. *
  403. * @param int $mask
  404. *
  405. * @return bool
  406. */
  407. public function getGeneralPurposeBitFlag($mask)
  408. {
  409. return ($this->generalPurposeBitFlags & $mask) !== 0;
  410. }
  411. /**
  412. * Sets the encryption property to false and removes any other
  413. * encryption artifacts.
  414. *
  415. * @throws ZipException
  416. *
  417. * @return ZipEntry
  418. */
  419. public function disableEncryption()
  420. {
  421. $this->setEncrypted(false);
  422. $headerId = WinZipAesEntryExtraField::getHeaderId();
  423. if (isset($this->extraFieldsCollection[$headerId])) {
  424. /** @var WinZipAesEntryExtraField $field */
  425. $field = $this->extraFieldsCollection[$headerId];
  426. if ($this->getMethod() === self::METHOD_WINZIP_AES) {
  427. $this->setMethod($field === null ? self::UNKNOWN : $field->getMethod());
  428. }
  429. unset($this->extraFieldsCollection[$headerId]);
  430. }
  431. $this->password = null;
  432. return $this;
  433. }
  434. /**
  435. * Sets the encryption flag for this ZIP entry.
  436. *
  437. * @param bool $encrypted
  438. *
  439. * @return ZipEntry
  440. */
  441. public function setEncrypted($encrypted)
  442. {
  443. $encrypted = (bool) $encrypted;
  444. $this->setGeneralPurposeBitFlag(self::GPBF_ENCRYPTED, $encrypted);
  445. return $this;
  446. }
  447. /**
  448. * Returns the compression method for this entry.
  449. *
  450. * @return int
  451. */
  452. public function getMethod()
  453. {
  454. return $this->method;
  455. }
  456. /**
  457. * Sets the compression method for this entry.
  458. *
  459. * @param int $method
  460. *
  461. * @throws ZipException if method is not STORED, DEFLATED, BZIP2 or UNKNOWN
  462. *
  463. * @return ZipEntry
  464. */
  465. public function setMethod($method)
  466. {
  467. if ($method === self::UNKNOWN) {
  468. $this->method = $method;
  469. return $this;
  470. }
  471. if ($method < 0x0000 || $method > 0xffff) {
  472. throw new ZipException('method out of range: ' . $method);
  473. }
  474. switch ($method) {
  475. case self::METHOD_WINZIP_AES:
  476. case ZipFile::METHOD_STORED:
  477. case ZipFile::METHOD_DEFLATED:
  478. case ZipFile::METHOD_BZIP2:
  479. $this->method = $method;
  480. break;
  481. default:
  482. throw new ZipException($this->name . " (unsupported compression method {$method})");
  483. }
  484. return $this;
  485. }
  486. /**
  487. * Get Unix Timestamp.
  488. *
  489. * @return int
  490. */
  491. public function getTime()
  492. {
  493. if ($this->getDosTime() === self::UNKNOWN) {
  494. return self::UNKNOWN;
  495. }
  496. return DateTimeConverter::toUnixTimestamp($this->getDosTime());
  497. }
  498. /**
  499. * Get Dos Time.
  500. *
  501. * @return int
  502. */
  503. public function getDosTime()
  504. {
  505. return $this->dosTime;
  506. }
  507. /**
  508. * Set Dos Time.
  509. *
  510. * @param int $dosTime
  511. *
  512. * @throws ZipException
  513. *
  514. * @return ZipEntry
  515. */
  516. public function setDosTime($dosTime)
  517. {
  518. $dosTime = (int) $dosTime;
  519. if ($dosTime < 0x00000000 || $dosTime > 0xffffffff) {
  520. throw new ZipException('DosTime out of range');
  521. }
  522. $this->dosTime = $dosTime;
  523. return $this;
  524. }
  525. /**
  526. * Set time from unix timestamp.
  527. *
  528. * @param int $unixTimestamp
  529. *
  530. * @throws ZipException
  531. *
  532. * @return ZipEntry
  533. */
  534. public function setTime($unixTimestamp)
  535. {
  536. $known = $unixTimestamp !== self::UNKNOWN;
  537. if ($known) {
  538. $this->dosTime = DateTimeConverter::toDosTime($unixTimestamp);
  539. } else {
  540. $this->dosTime = 0;
  541. }
  542. return $this;
  543. }
  544. /**
  545. * Returns the external file attributes.
  546. *
  547. * @return int the external file attributes
  548. */
  549. public function getExternalAttributes()
  550. {
  551. return $this->externalAttributes;
  552. }
  553. /**
  554. * Sets the external file attributes.
  555. *
  556. * @param int $externalAttributes the external file attributes
  557. *
  558. * @return ZipEntry
  559. */
  560. public function setExternalAttributes($externalAttributes)
  561. {
  562. $this->externalAttributes = $externalAttributes;
  563. return $this;
  564. }
  565. /**
  566. * Sets the internal file attributes.
  567. *
  568. * @param int $attributes the internal file attributes
  569. *
  570. * @return ZipEntry
  571. */
  572. public function setInternalAttributes($attributes)
  573. {
  574. $this->internalAttributes = (int) $attributes;
  575. return $this;
  576. }
  577. /**
  578. * Returns the internal file attributes.
  579. *
  580. * @return int the internal file attributes
  581. */
  582. public function getInternalAttributes()
  583. {
  584. return $this->internalAttributes;
  585. }
  586. /**
  587. * Returns true if and only if this ZIP entry represents a directory entry
  588. * (i.e. end with '/').
  589. *
  590. * @return bool
  591. */
  592. public function isDirectory()
  593. {
  594. return StringUtil::endsWith($this->name, '/');
  595. }
  596. /**
  597. * @return ExtraFieldsCollection
  598. */
  599. public function &getExtraFieldsCollection()
  600. {
  601. return $this->extraFieldsCollection;
  602. }
  603. /**
  604. * Returns a protective copy of the serialized Extra Fields.
  605. *
  606. * @throws ZipException
  607. *
  608. * @return string
  609. */
  610. public function getExtra()
  611. {
  612. return ExtraFieldsFactory::createSerializedData($this->extraFieldsCollection);
  613. }
  614. /**
  615. * Sets the serialized Extra Fields by making a protective copy.
  616. * Note that this method parses the serialized Extra Fields according to
  617. * the ZIP File Format Specification and limits its size to 64 KB.
  618. * Therefore, this property cannot not be used to hold arbitrary
  619. * (application) data.
  620. * Consider storing such data in a separate entry instead.
  621. *
  622. * @param string $data the byte array holding the serialized Extra Fields
  623. *
  624. * @throws ZipException if the serialized Extra Fields exceed 64 KB
  625. *
  626. * @return ZipEntry
  627. */
  628. public function setExtra($data)
  629. {
  630. $this->extraFieldsCollection = ExtraFieldsFactory::createExtraFieldCollections($data, $this);
  631. return $this;
  632. }
  633. /**
  634. * Returns comment entry.
  635. *
  636. * @return string
  637. */
  638. public function getComment()
  639. {
  640. return $this->comment !== null ? $this->comment : '';
  641. }
  642. /**
  643. * Set entry comment.
  644. *
  645. * @param string|null $comment
  646. *
  647. * @throws ZipException
  648. *
  649. * @return ZipEntry
  650. */
  651. public function setComment($comment)
  652. {
  653. if ($comment !== null) {
  654. $commentLength = \strlen($comment);
  655. if ($commentLength < 0x0000 || $commentLength > 0xffff) {
  656. throw new ZipException('Comment too long');
  657. }
  658. $this->setGeneralPurposeBitFlag(self::GPBF_UTF8, true);
  659. }
  660. $this->comment = $comment;
  661. return $this;
  662. }
  663. /**
  664. * @return bool
  665. */
  666. public function isDataDescriptorRequired()
  667. {
  668. return ($this->getCrc() | $this->getCompressedSize() | $this->getSize()) === self::UNKNOWN;
  669. }
  670. /**
  671. * Return crc32 content or 0 for WinZip AES v2.
  672. *
  673. * @return int
  674. */
  675. public function getCrc()
  676. {
  677. return $this->crc;
  678. }
  679. /**
  680. * Set crc32 content.
  681. *
  682. * @param int $crc
  683. *
  684. * @return ZipEntry
  685. */
  686. public function setCrc($crc)
  687. {
  688. $this->crc = (int) $crc;
  689. return $this;
  690. }
  691. /**
  692. * @return string
  693. */
  694. public function getPassword()
  695. {
  696. return $this->password;
  697. }
  698. /**
  699. * Set password and encryption method from entry.
  700. *
  701. * @param string $password
  702. * @param int|null $encryptionMethod
  703. *
  704. * @throws ZipException
  705. *
  706. * @return ZipEntry
  707. */
  708. public function setPassword($password, $encryptionMethod = null)
  709. {
  710. $this->password = $password;
  711. if ($encryptionMethod !== null) {
  712. $this->setEncryptionMethod($encryptionMethod);
  713. }
  714. if (!empty($this->password)) {
  715. $this->setEncrypted(true);
  716. } else {
  717. $this->disableEncryption();
  718. }
  719. return $this;
  720. }
  721. /**
  722. * @return int
  723. */
  724. public function getEncryptionMethod()
  725. {
  726. return $this->encryptionMethod;
  727. }
  728. /**
  729. * Set encryption method.
  730. *
  731. * @param int $encryptionMethod
  732. *
  733. * @throws ZipException
  734. *
  735. * @return ZipEntry
  736. *
  737. * @see ZipFile::ENCRYPTION_METHOD_WINZIP_AES_256
  738. * @see ZipFile::ENCRYPTION_METHOD_TRADITIONAL
  739. * @see ZipFile::ENCRYPTION_METHOD_WINZIP_AES_128
  740. * @see ZipFile::ENCRYPTION_METHOD_WINZIP_AES_192
  741. */
  742. public function setEncryptionMethod($encryptionMethod)
  743. {
  744. if ($encryptionMethod !== null) {
  745. if (
  746. $encryptionMethod !== ZipFile::ENCRYPTION_METHOD_TRADITIONAL
  747. && $encryptionMethod !== ZipFile::ENCRYPTION_METHOD_WINZIP_AES_128
  748. && $encryptionMethod !== ZipFile::ENCRYPTION_METHOD_WINZIP_AES_192
  749. && $encryptionMethod !== ZipFile::ENCRYPTION_METHOD_WINZIP_AES_256
  750. ) {
  751. throw new ZipException('Invalid encryption method');
  752. }
  753. $this->encryptionMethod = $encryptionMethod;
  754. }
  755. return $this;
  756. }
  757. /**
  758. * @return int
  759. */
  760. public function getCompressionLevel()
  761. {
  762. return $this->compressionLevel;
  763. }
  764. /**
  765. * @param int $compressionLevel
  766. *
  767. * @return ZipEntry
  768. */
  769. public function setCompressionLevel($compressionLevel = ZipFile::LEVEL_DEFAULT_COMPRESSION)
  770. {
  771. if ($compressionLevel < ZipFile::LEVEL_DEFAULT_COMPRESSION ||
  772. $compressionLevel > ZipFile::LEVEL_BEST_COMPRESSION
  773. ) {
  774. throw new InvalidArgumentException(
  775. 'Invalid compression level. Minimum level ' .
  776. ZipFile::LEVEL_DEFAULT_COMPRESSION . '. Maximum level ' . ZipFile::LEVEL_BEST_COMPRESSION
  777. );
  778. }
  779. $this->compressionLevel = $compressionLevel;
  780. return $this;
  781. }
  782. /**
  783. * Clone extra fields.
  784. */
  785. public function __clone()
  786. {
  787. $this->extraFieldsCollection = clone $this->extraFieldsCollection;
  788. }
  789. }