ZipFileInterface.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. <?php
  2. namespace PhpZip;
  3. use PhpZip\Exception\ZipEntryNotFoundException;
  4. use PhpZip\Exception\ZipException;
  5. use PhpZip\Model\ZipEntry;
  6. use PhpZip\Model\ZipEntryMatcher;
  7. use PhpZip\Model\ZipInfo;
  8. use Psr\Http\Message\ResponseInterface;
  9. /**
  10. * Create, open .ZIP files, modify, get info and extract files.
  11. *
  12. * Implemented support traditional PKWARE encryption and WinZip AES encryption.
  13. * Implemented support ZIP64.
  14. * Implemented support skip a preamble like the one found in self extracting archives.
  15. * Support ZipAlign functional.
  16. *
  17. * @see https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT .ZIP File Format Specification
  18. *
  19. * @author Ne-Lexa alexey@nelexa.ru
  20. * @license MIT
  21. */
  22. interface ZipFileInterface extends \Countable, \ArrayAccess, \Iterator
  23. {
  24. /**
  25. * Method for Stored (uncompressed) entries.
  26. *
  27. * @see ZipEntry::setMethod()
  28. */
  29. const METHOD_STORED = 0;
  30. /**
  31. * Method for Deflated compressed entries.
  32. *
  33. * @see ZipEntry::setMethod()
  34. */
  35. const METHOD_DEFLATED = 8;
  36. /**
  37. * Method for BZIP2 compressed entries.
  38. * Require php extension bz2.
  39. *
  40. * @see ZipEntry::setMethod()
  41. */
  42. const METHOD_BZIP2 = 12;
  43. /** Default compression level. */
  44. const LEVEL_DEFAULT_COMPRESSION = -1;
  45. /** Compression level for fastest compression. */
  46. const LEVEL_FAST = 2;
  47. /** Compression level for fastest compression. */
  48. const LEVEL_BEST_SPEED = 1;
  49. const LEVEL_SUPER_FAST = self::LEVEL_BEST_SPEED;
  50. /** Compression level for best compression. */
  51. const LEVEL_BEST_COMPRESSION = 9;
  52. /** No specified method for set encryption method to Traditional PKWARE encryption. */
  53. const ENCRYPTION_METHOD_TRADITIONAL = 0;
  54. /**
  55. * No specified method for set encryption method to WinZip AES encryption.
  56. * Default value 256 bit.
  57. */
  58. const ENCRYPTION_METHOD_WINZIP_AES = self::ENCRYPTION_METHOD_WINZIP_AES_256;
  59. /** No specified method for set encryption method to WinZip AES encryption 128 bit. */
  60. const ENCRYPTION_METHOD_WINZIP_AES_128 = 2;
  61. /** No specified method for set encryption method to WinZip AES encryption 194 bit. */
  62. const ENCRYPTION_METHOD_WINZIP_AES_192 = 3;
  63. /** No specified method for set encryption method to WinZip AES encryption 256 bit. */
  64. const ENCRYPTION_METHOD_WINZIP_AES_256 = 1;
  65. /**
  66. * Open zip archive from file.
  67. *
  68. * @param string $filename
  69. *
  70. * @throws ZipException if can't open file
  71. *
  72. * @return ZipFileInterface
  73. */
  74. public function openFile($filename);
  75. /**
  76. * Open zip archive from raw string data.
  77. *
  78. * @param string $data
  79. *
  80. * @throws ZipException if can't open temp stream
  81. *
  82. * @return ZipFileInterface
  83. */
  84. public function openFromString($data);
  85. /**
  86. * Open zip archive from stream resource.
  87. *
  88. * @param resource $handle
  89. *
  90. * @return ZipFileInterface
  91. */
  92. public function openFromStream($handle);
  93. /**
  94. * @return string[] returns the list files
  95. */
  96. public function getListFiles();
  97. /**
  98. * Returns the file comment.
  99. *
  100. * @return string the file comment
  101. */
  102. public function getArchiveComment();
  103. /**
  104. * Set archive comment.
  105. *
  106. * @param string|null $comment
  107. *
  108. * @return ZipFileInterface
  109. */
  110. public function setArchiveComment($comment = null);
  111. /**
  112. * Checks that the entry in the archive is a directory.
  113. * Returns true if and only if this ZIP entry represents a directory entry
  114. * (i.e. end with '/').
  115. *
  116. * @param string $entryName
  117. *
  118. * @throws ZipEntryNotFoundException
  119. *
  120. * @return bool
  121. */
  122. public function isDirectory($entryName);
  123. /**
  124. * Returns entry comment.
  125. *
  126. * @param string $entryName
  127. *
  128. * @throws ZipEntryNotFoundException
  129. *
  130. * @return string
  131. */
  132. public function getEntryComment($entryName);
  133. /**
  134. * Set entry comment.
  135. *
  136. * @param string $entryName
  137. * @param string|null $comment
  138. *
  139. * @throws ZipEntryNotFoundException
  140. *
  141. * @return ZipFileInterface
  142. */
  143. public function setEntryComment($entryName, $comment = null);
  144. /**
  145. * Returns the entry contents.
  146. *
  147. * @param string $entryName
  148. *
  149. * @return string
  150. */
  151. public function getEntryContents($entryName);
  152. /**
  153. * Checks if there is an entry in the archive.
  154. *
  155. * @param string $entryName
  156. *
  157. * @return bool
  158. */
  159. public function hasEntry($entryName);
  160. /**
  161. * Get info by entry.
  162. *
  163. * @param string|ZipEntry $entryName
  164. *
  165. * @throws ZipEntryNotFoundException
  166. *
  167. * @return ZipInfo
  168. */
  169. public function getEntryInfo($entryName);
  170. /**
  171. * Get info by all entries.
  172. *
  173. * @return ZipInfo[]
  174. */
  175. public function getAllInfo();
  176. /**
  177. * @return ZipEntryMatcher
  178. */
  179. public function matcher();
  180. /**
  181. * Extract the archive contents.
  182. *
  183. * Extract the complete archive or the given files to the specified destination.
  184. *
  185. * @param string $destination location where to extract the files
  186. * @param array|string|null $entries The entries to extract. It accepts either
  187. * a single entry name or an array of names.
  188. *
  189. * @throws ZipException
  190. *
  191. * @return ZipFileInterface
  192. */
  193. public function extractTo($destination, $entries = null);
  194. /**
  195. * Add entry from the string.
  196. *
  197. * @param string $localName zip entry name
  198. * @param string $contents string contents
  199. * @param int|null $compressionMethod Compression method.
  200. * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or ZipFile::METHOD_BZIP2.
  201. * If null, then auto choosing method.
  202. *
  203. * @return ZipFileInterface
  204. *
  205. * @see ZipFile::METHOD_STORED
  206. * @see ZipFile::METHOD_DEFLATED
  207. * @see ZipFile::METHOD_BZIP2
  208. */
  209. public function addFromString($localName, $contents, $compressionMethod = null);
  210. /**
  211. * Add entry from the file.
  212. *
  213. * @param string $filename destination file
  214. * @param string|null $localName zip Entry name
  215. * @param int|null $compressionMethod Compression method.
  216. * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or
  217. * ZipFile::METHOD_BZIP2. If null, then auto choosing method.
  218. *
  219. * @return ZipFileInterface
  220. *
  221. * @see ZipFile::METHOD_STORED
  222. * @see ZipFile::METHOD_DEFLATED
  223. * @see ZipFile::METHOD_BZIP2
  224. */
  225. public function addFile($filename, $localName = null, $compressionMethod = null);
  226. /**
  227. * Add entry from the stream.
  228. *
  229. * @param resource $stream stream resource
  230. * @param string $localName zip Entry name
  231. * @param int|null $compressionMethod Compression method.
  232. * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or ZipFile::METHOD_BZIP2.
  233. * If null, then auto choosing method.
  234. *
  235. * @return ZipFileInterface
  236. *
  237. * @see ZipFile::METHOD_STORED
  238. * @see ZipFile::METHOD_DEFLATED
  239. * @see ZipFile::METHOD_BZIP2
  240. */
  241. public function addFromStream($stream, $localName, $compressionMethod = null);
  242. /**
  243. * Add an empty directory in the zip archive.
  244. *
  245. * @param string $dirName
  246. *
  247. * @return ZipFileInterface
  248. */
  249. public function addEmptyDir($dirName);
  250. /**
  251. * Add directory not recursively to the zip archive.
  252. *
  253. * @param string $inputDir Input directory
  254. * @param string $localPath add files to this directory, or the root
  255. * @param int|null $compressionMethod Compression method.
  256. * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or ZipFile::METHOD_BZIP2.
  257. * If null, then auto choosing method.
  258. *
  259. * @return ZipFileInterface
  260. */
  261. public function addDir($inputDir, $localPath = '/', $compressionMethod = null);
  262. /**
  263. * Add recursive directory to the zip archive.
  264. *
  265. * @param string $inputDir Input directory
  266. * @param string $localPath add files to this directory, or the root
  267. * @param int|null $compressionMethod Compression method.
  268. * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or ZipFile::METHOD_BZIP2.
  269. * If null, then auto choosing method.
  270. *
  271. * @return ZipFileInterface
  272. *
  273. * @see ZipFile::METHOD_STORED
  274. * @see ZipFile::METHOD_DEFLATED
  275. * @see ZipFile::METHOD_BZIP2
  276. */
  277. public function addDirRecursive($inputDir, $localPath = '/', $compressionMethod = null);
  278. /**
  279. * Add directories from directory iterator.
  280. *
  281. * @param \Iterator $iterator directory iterator
  282. * @param string $localPath add files to this directory, or the root
  283. * @param int|null $compressionMethod Compression method.
  284. * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or
  285. * ZipFile::METHOD_BZIP2. If null, then auto choosing method.
  286. *
  287. * @return ZipFileInterface
  288. *
  289. * @see ZipFile::METHOD_STORED
  290. * @see ZipFile::METHOD_DEFLATED
  291. * @see ZipFile::METHOD_BZIP2
  292. */
  293. public function addFilesFromIterator(\Iterator $iterator, $localPath = '/', $compressionMethod = null);
  294. /**
  295. * Add files from glob pattern.
  296. *
  297. * @param string $inputDir Input directory
  298. * @param string $globPattern glob pattern
  299. * @param string|null $localPath add files to this directory, or the root
  300. * @param int|null $compressionMethod Compression method.
  301. * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or
  302. * ZipFile::METHOD_BZIP2. If null, then auto choosing method.
  303. *
  304. * @return ZipFileInterface
  305. * @sse https://en.wikipedia.org/wiki/Glob_(programming) Glob pattern syntax
  306. */
  307. public function addFilesFromGlob($inputDir, $globPattern, $localPath = '/', $compressionMethod = null);
  308. /**
  309. * Add files recursively from glob pattern.
  310. *
  311. * @param string $inputDir Input directory
  312. * @param string $globPattern glob pattern
  313. * @param string|null $localPath add files to this directory, or the root
  314. * @param int|null $compressionMethod Compression method.
  315. * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or
  316. * ZipFile::METHOD_BZIP2. If null, then auto choosing method.
  317. *
  318. * @return ZipFileInterface
  319. * @sse https://en.wikipedia.org/wiki/Glob_(programming) Glob pattern syntax
  320. */
  321. public function addFilesFromGlobRecursive($inputDir, $globPattern, $localPath = '/', $compressionMethod = null);
  322. /**
  323. * Add files from regex pattern.
  324. *
  325. * @param string $inputDir search files in this directory
  326. * @param string $regexPattern regex pattern
  327. * @param string|null $localPath add files to this directory, or the root
  328. * @param int|null $compressionMethod Compression method.
  329. * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or
  330. * ZipFile::METHOD_BZIP2. If null, then auto choosing method.
  331. *
  332. * @return ZipFileInterface
  333. *
  334. * @internal param bool $recursive Recursive search
  335. */
  336. public function addFilesFromRegex($inputDir, $regexPattern, $localPath = '/', $compressionMethod = null);
  337. /**
  338. * Add files recursively from regex pattern.
  339. *
  340. * @param string $inputDir search files in this directory
  341. * @param string $regexPattern regex pattern
  342. * @param string|null $localPath add files to this directory, or the root
  343. * @param int|null $compressionMethod Compression method.
  344. * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or
  345. * ZipFile::METHOD_BZIP2. If null, then auto choosing method.
  346. *
  347. * @return ZipFileInterface
  348. *
  349. * @internal param bool $recursive Recursive search
  350. */
  351. public function addFilesFromRegexRecursive($inputDir, $regexPattern, $localPath = '/', $compressionMethod = null);
  352. /**
  353. * Add array data to archive.
  354. * Keys is local names.
  355. * Values is contents.
  356. *
  357. * @param array $mapData associative array for added to zip
  358. */
  359. public function addAll(array $mapData);
  360. /**
  361. * Rename the entry.
  362. *
  363. * @param string $oldName old entry name
  364. * @param string $newName new entry name
  365. *
  366. * @throws ZipEntryNotFoundException
  367. *
  368. * @return ZipFileInterface
  369. */
  370. public function rename($oldName, $newName);
  371. /**
  372. * Delete entry by name.
  373. *
  374. * @param string $entryName zip Entry name
  375. *
  376. * @throws ZipEntryNotFoundException if entry not found
  377. *
  378. * @return ZipFileInterface
  379. */
  380. public function deleteFromName($entryName);
  381. /**
  382. * Delete entries by glob pattern.
  383. *
  384. * @param string $globPattern Glob pattern
  385. *
  386. * @return ZipFileInterface
  387. * @sse https://en.wikipedia.org/wiki/Glob_(programming) Glob pattern syntax
  388. */
  389. public function deleteFromGlob($globPattern);
  390. /**
  391. * Delete entries by regex pattern.
  392. *
  393. * @param string $regexPattern Regex pattern
  394. *
  395. * @return ZipFileInterface
  396. */
  397. public function deleteFromRegex($regexPattern);
  398. /**
  399. * Delete all entries.
  400. *
  401. * @return ZipFileInterface
  402. */
  403. public function deleteAll();
  404. /**
  405. * Set compression level for new entries.
  406. *
  407. * @param int $compressionLevel
  408. *
  409. * @return ZipFileInterface
  410. *
  411. * @see ZipFile::LEVEL_SUPER_FAST
  412. * @see ZipFile::LEVEL_FAST
  413. * @see ZipFile::LEVEL_BEST_COMPRESSION
  414. * @see ZipFile::LEVEL_DEFAULT_COMPRESSION
  415. */
  416. public function setCompressionLevel($compressionLevel = self::LEVEL_DEFAULT_COMPRESSION);
  417. /**
  418. * @param string $entryName
  419. * @param int $compressionLevel
  420. *
  421. * @throws ZipException
  422. *
  423. * @return ZipFileInterface
  424. *
  425. * @see ZipFile::LEVEL_DEFAULT_COMPRESSION
  426. * @see ZipFile::LEVEL_SUPER_FAST
  427. * @see ZipFile::LEVEL_FAST
  428. * @see ZipFile::LEVEL_BEST_COMPRESSION
  429. */
  430. public function setCompressionLevelEntry($entryName, $compressionLevel);
  431. /**
  432. * @param string $entryName
  433. * @param int $compressionMethod
  434. *
  435. * @throws ZipException
  436. *
  437. * @return ZipFileInterface
  438. *
  439. * @see ZipFile::METHOD_STORED
  440. * @see ZipFile::METHOD_DEFLATED
  441. * @see ZipFile::METHOD_BZIP2
  442. */
  443. public function setCompressionMethodEntry($entryName, $compressionMethod);
  444. /**
  445. * zipalign is optimization to Android application (APK) files.
  446. *
  447. * @param int|null $align
  448. *
  449. * @return ZipFileInterface
  450. *
  451. * @see https://developer.android.com/studio/command-line/zipalign.html
  452. */
  453. public function setZipAlign($align = null);
  454. /**
  455. * Set password to all input encrypted entries.
  456. *
  457. * @param string $password Password
  458. *
  459. * @return ZipFileInterface
  460. *
  461. * @deprecated using ZipFile::setReadPassword()
  462. */
  463. public function withReadPassword($password);
  464. /**
  465. * Set password to all input encrypted entries.
  466. *
  467. * @param string $password Password
  468. *
  469. * @return ZipFileInterface
  470. */
  471. public function setReadPassword($password);
  472. /**
  473. * Set password to concrete input entry.
  474. *
  475. * @param string $entryName
  476. * @param string $password Password
  477. *
  478. * @return ZipFileInterface
  479. */
  480. public function setReadPasswordEntry($entryName, $password);
  481. /**
  482. * Set password for all entries for update.
  483. *
  484. * @param string $password If password null then encryption clear
  485. * @param int|null $encryptionMethod Encryption method
  486. *
  487. * @return ZipFileInterface
  488. *
  489. * @deprecated using ZipFile::setPassword()
  490. */
  491. public function withNewPassword($password, $encryptionMethod = self::ENCRYPTION_METHOD_WINZIP_AES_256);
  492. /**
  493. * Sets a new password for all files in the archive.
  494. *
  495. * @param string $password
  496. * @param int|null $encryptionMethod Encryption method
  497. *
  498. * @return ZipFileInterface
  499. */
  500. public function setPassword($password, $encryptionMethod = self::ENCRYPTION_METHOD_WINZIP_AES_256);
  501. /**
  502. * Sets a new password of an entry defined by its name.
  503. *
  504. * @param string $entryName
  505. * @param string $password
  506. * @param int|null $encryptionMethod
  507. *
  508. * @return ZipFileInterface
  509. */
  510. public function setPasswordEntry($entryName, $password, $encryptionMethod = null);
  511. /**
  512. * Remove password for all entries for update.
  513. *
  514. * @return ZipFileInterface
  515. *
  516. * @deprecated using ZipFile::disableEncryption()
  517. */
  518. public function withoutPassword();
  519. /**
  520. * Disable encryption for all entries that are already in the archive.
  521. *
  522. * @return ZipFileInterface
  523. */
  524. public function disableEncryption();
  525. /**
  526. * Disable encryption of an entry defined by its name.
  527. *
  528. * @param string $entryName
  529. *
  530. * @return ZipFileInterface
  531. */
  532. public function disableEncryptionEntry($entryName);
  533. /**
  534. * Undo all changes done in the archive.
  535. *
  536. * @return ZipFileInterface
  537. */
  538. public function unchangeAll();
  539. /**
  540. * Undo change archive comment.
  541. *
  542. * @return ZipFileInterface
  543. */
  544. public function unchangeArchiveComment();
  545. /**
  546. * Revert all changes done to an entry with the given name.
  547. *
  548. * @param string|ZipEntry $entry Entry name or ZipEntry
  549. *
  550. * @return ZipFileInterface
  551. */
  552. public function unchangeEntry($entry);
  553. /**
  554. * Save as file.
  555. *
  556. * @param string $filename Output filename
  557. *
  558. * @throws ZipException
  559. *
  560. * @return ZipFileInterface
  561. */
  562. public function saveAsFile($filename);
  563. /**
  564. * Save as stream.
  565. *
  566. * @param resource $handle Output stream resource
  567. *
  568. * @throws ZipException
  569. *
  570. * @return ZipFileInterface
  571. */
  572. public function saveAsStream($handle);
  573. /**
  574. * Output .ZIP archive as attachment.
  575. * Die after output.
  576. *
  577. * @param string $outputFilename Output filename
  578. * @param string|null $mimeType Mime-Type
  579. * @param bool $attachment Http Header 'Content-Disposition' if true then attachment otherwise inline
  580. */
  581. public function outputAsAttachment($outputFilename, $mimeType = null, $attachment = true);
  582. /**
  583. * Output .ZIP archive as PSR-7 Response.
  584. *
  585. * @param ResponseInterface $response Instance PSR-7 Response
  586. * @param string $outputFilename Output filename
  587. * @param string|null $mimeType Mime-Type
  588. * @param bool $attachment Http Header 'Content-Disposition' if true then attachment otherwise inline
  589. *
  590. * @return ResponseInterface
  591. */
  592. public function outputAsResponse(
  593. ResponseInterface $response,
  594. $outputFilename,
  595. $mimeType = null,
  596. $attachment = true
  597. );
  598. /**
  599. * Returns the zip archive as a string.
  600. *
  601. * @return string
  602. */
  603. public function outputAsString();
  604. /**
  605. * Save and reopen zip archive.
  606. *
  607. * @throws ZipException
  608. *
  609. * @return ZipFileInterface
  610. */
  611. public function rewrite();
  612. /**
  613. * Close zip archive and release input stream.
  614. */
  615. public function close();
  616. }