酒店预订平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 41 KiB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. `PhpZip`
  2. ========
  3. `PhpZip` is a php-library for extended work with ZIP-archives.
  4. [![Build Status](https://travis-ci.org/Ne-Lexa/php-zip.svg?branch=master)](https://travis-ci.org/Ne-Lexa/php-zip)
  5. [![Code Coverage](https://scrutinizer-ci.com/g/Ne-Lexa/php-zip/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/Ne-Lexa/php-zip/?branch=master)
  6. [![Latest Stable Version](https://poser.pugx.org/nelexa/zip/v/stable)](https://packagist.org/packages/nelexa/zip)
  7. [![Total Downloads](https://poser.pugx.org/nelexa/zip/downloads)](https://packagist.org/packages/nelexa/zip)
  8. [![Minimum PHP Version](http://img.shields.io/badge/php-%3E%3D%205.5-8892BF.svg)](https://php.net/)
  9. [![License](https://poser.pugx.org/nelexa/zip/license)](https://packagist.org/packages/nelexa/zip)
  10. [Russian Documentation](README.RU.md)
  11. Table of contents
  12. -----------------
  13. - [Features](#Features)
  14. - [Requirements](#Requirements)
  15. - [Installation](#Installation)
  16. - [Examples](#Examples)
  17. - [Glossary](#Glossary)
  18. - [Documentation](#Documentation)
  19. + [Overview of methods of the class `\PhpZip\ZipFile`](#Documentation-Overview)
  20. + [Creation/Opening of ZIP-archive](#Documentation-Open-Zip-Archive)
  21. + [Reading entries from the archive](#Documentation-Open-Zip-Entries)
  22. + [Iterating entries](#Documentation-Zip-Iterate)
  23. + [Getting information about entries](#Documentation-Zip-Info)
  24. + [Adding entries to the archive](#Documentation-Add-Zip-Entries)
  25. + [Deleting entries from the archive](#Documentation-Remove-Zip-Entries)
  26. + [Working with entries and archive](#Documentation-Entries)
  27. + [Working with passwords](#Documentation-Password)
  28. + [zipalign - alignment tool for Android (APK) files](#Documentation-ZipAlign-Usage)
  29. + [Undo changes](#Documentation-Unchanged)
  30. + [Saving a file or output to a browser](#Documentation-Save-Or-Output-Entries)
  31. + [Closing the archive](#Documentation-Close-Zip-Archive)
  32. - [Running the tests](#Running-Tests)
  33. - [Changelog](#Changelog)
  34. - [Upgrade](#Upgrade)
  35. + [Upgrade version 2 to version 3.0](#Upgrade-v2-to-v3)
  36. ### <a name="Features"></a> Features
  37. - Opening and unzipping zip files.
  38. - Creating ZIP-archives.
  39. - Modifying ZIP archives.
  40. - Pure php (not require extension `php-zip` and class `\ZipArchive`).
  41. - It supports saving the archive to a file, outputting the archive to the browser, or outputting it as a string without saving it to a file.
  42. - Archival comments and comments of individual entry are supported.
  43. - Get information about each entry in the archive.
  44. - Only the following compression methods are supported:
  45. + No compressed (Stored).
  46. + Deflate compression.
  47. + BZIP2 compression with the extension `php-bz2`.
  48. - Support for `ZIP64` (file size is more than 4 GB or the number of entries in the archive is more than 65535).
  49. - Built-in support for aligning the archive to optimize Android packages (APK) [`zipalign`](https://developer.android.com/studio/command-line/zipalign.html).
  50. - Working with passwords for PHP 5.5
  51. > **Attention!**
  52. >
  53. > For 32-bit systems, the `Traditional PKWARE Encryption (ZipCrypto)` encryption method is not currently supported.
  54. > Use the encryption method `WinZIP AES Encryption`, whenever possible.
  55. + Set the password to read the archive for all entries or only for some.
  56. + Change the password for the archive, including for individual entries.
  57. + Delete the archive password for all or individual entries.
  58. + Set the password and/or the encryption method, both for all, and for individual entries in the archive.
  59. + Set different passwords and encryption methods for different entries.
  60. + Delete the password for all or some entries.
  61. + Support `Traditional PKWARE Encryption (ZipCrypto)` and `WinZIP AES Encryption` encryption methods.
  62. + Set the encryption method for all or individual entries in the archive.
  63. ### <a name="Requirements"></a> Requirements
  64. - `PHP` >= 5.5 (preferably 64-bit).
  65. - Optional php-extension `bzip2` for BZIP2 compression.
  66. - Optional php-extension `openssl` or `mcrypt` for `WinZip Aes Encryption` support.
  67. ### <a name="Installation"></a> Installation
  68. `composer require nelexa/zip`
  69. Latest stable version: [![Latest Stable Version](https://poser.pugx.org/nelexa/zip/v/stable)](https://packagist.org/packages/nelexa/zip)
  70. ### <a name="Examples"></a> Examples
  71. ```php
  72. // create new archive
  73. $zipFile = new \PhpZip\ZipFile();
  74. try{
  75. $zipFile
  76. ->addFromString('zip/entry/filename', 'Is file content') // add an entry from the string
  77. ->addFile('/path/to/file', 'data/tofile') // add an entry from the file
  78. ->addDir(__DIR__, 'to/path/') // add files from the directory
  79. ->saveAsFile($outputFilename) // save the archive to a file
  80. ->close(); // close archive
  81. // open archive, extract, add files, set password and output to browser.
  82. $zipFile
  83. ->openFile($outputFilename) // open archive from file
  84. ->extractTo($outputDirExtract) // extract files to the specified directory
  85. ->deleteFromRegex('~^\.~') // delete all hidden (Unix) files
  86. ->addFromString('dir/file.txt', 'Test file') // add a new entry from the string
  87. ->setPassword('password') // set password for all entries
  88. ->outputAsAttachment('library.jar'); // output to the browser without saving to a file
  89. }
  90. catch(\PhpZip\Exception\ZipException $e){
  91. // handle exception
  92. }
  93. finally{
  94. $zipFile->close();
  95. }
  96. ```
  97. Other examples can be found in the `tests/` folder
  98. ### <a name="Glossary"></a> Glossary
  99. **Zip Entry** - file or folder in a ZIP-archive. Each entry in the archive has certain properties, for example: file name, compression method, encryption method, file size before compression, file size after compression, CRC32 and others.
  100. ### <a name="Documentation"></a> Documentation:
  101. #### <a name="Documentation-Overview"></a> Overview of methods of the class `\PhpZip\ZipFile`
  102. - [ZipFile::__construct](#Documentation-ZipFile-__construct) - initializes the ZIP archive.
  103. - [ZipFile::addAll](#Documentation-ZipFile-addAll) - adds all entries from an array.
  104. - [ZipFile::addDir](#Documentation-ZipFile-addDir) - adds files to the archive from the directory on the specified path without subdirectories.
  105. - [ZipFile::addDirRecursive](#Documentation-ZipFile-addDirRecursive) - adds files to the archive from the directory on the specified path with subdirectories.
  106. - [ZipFile::addEmptyDir](#Documentation-ZipFile-addEmptyDir) - add a new directory.
  107. - [ZipFile::addFile](#Documentation-ZipFile-addFile) - adds a file to a ZIP archive from the given path.
  108. - [ZipFile::addSplFile](#Documentation-ZipFile-addSplFile) - adds a `\SplFileInfo` to a ZIP archive.
  109. - [ZipFile::addFromFinder](#Documentation-ZipFile-addFromFinder) - adds files from the `Symfony\Component\Finder\Finder` to a ZIP archive.
  110. - [ZipFile::addFilesFromIterator](#Documentation-ZipFile-addFilesFromIterator) - adds files from the iterator of directories.
  111. - [ZipFile::addFilesFromGlob](#Documentation-ZipFile-addFilesFromGlob) - adds files from a directory by glob pattern without subdirectories.
  112. - [ZipFile::addFilesFromGlobRecursive](#Documentation-ZipFile-addFilesFromGlobRecursive) - adds files from a directory by glob pattern with subdirectories.
  113. - [ZipFile::addFilesFromRegex](#Documentation-ZipFile-addFilesFromRegex) - adds files from a directory by PCRE pattern without subdirectories.
  114. - [ZipFile::addFilesFromRegexRecursive](#Documentation-ZipFile-addFilesFromRegexRecursive) - adds files from a directory by PCRE pattern with subdirectories.
  115. - [ZipFile::addFromStream](#Documentation-ZipFile-addFromStream) - adds a entry from the stream to the ZIP archive.
  116. - [ZipFile::addFromString](#Documentation-ZipFile-addFromString) - adds a file to a ZIP archive using its contents.
  117. - [ZipFile::close](#Documentation-ZipFile-close) - close the archive.
  118. - [ZipFile::count](#Documentation-ZipFile-count) - returns the number of entries in the archive.
  119. - [ZipFile::deleteFromName](#Documentation-ZipFile-deleteFromName) - deletes an entry in the archive using its name.
  120. - [ZipFile::deleteFromGlob](#Documentation-ZipFile-deleteFromGlob) - deletes a entries in the archive using glob pattern.
  121. - [ZipFile::deleteFromRegex](#Documentation-ZipFile-deleteFromRegex) - deletes a entries in the archive using PCRE pattern.
  122. - [ZipFile::deleteAll](#Documentation-ZipFile-deleteAll) - deletes all entries in the ZIP archive.
  123. - [ZipFile::disableEncryption](#Documentation-ZipFile-disableEncryption) - disable encryption for all entries that are already in the archive.
  124. - [ZipFile::disableEncryptionEntry](#Documentation-ZipFile-disableEncryptionEntry) - disable encryption of an entry defined by its name.
  125. - [ZipFile::extractTo](#Documentation-ZipFile-extractTo) - extract the archive contents.
  126. - [ZipFile::getAllInfo](#Documentation-ZipFile-getAllInfo) - returns detailed information about all entries in the archive.
  127. - [ZipFile::getArchiveComment](#Documentation-ZipFile-getArchiveComment) - returns the Zip archive comment.
  128. - [ZipFile::getEntryComment](#Documentation-ZipFile-getEntryComment) - returns the comment of an entry using the entry name.
  129. - [ZipFile::getEntryContent](#Documentation-ZipFile-getEntryContent) - returns the entry contents using its name.
  130. - [ZipFile::getEntryInfo](#Documentation-ZipFile-getEntryInfo) - returns detailed information about the entry in the archive.
  131. - [ZipFile::getListFiles](#Documentation-ZipFile-getListFiles) - returns list of archive files.
  132. - [ZipFile::hasEntry](#Documentation-ZipFile-hasEntry) - checks if there is an entry in the archive.
  133. - [ZipFile::isDirectory](#Documentation-ZipFile-isDirectory) - checks that the entry in the archive is a directory.
  134. - [ZipFile::matcher](#Documentation-ZipFile-matcher) - selecting entries in the archive to perform operations on them.
  135. - [ZipFile::openFile](#Documentation-ZipFile-openFile) - opens a zip-archive from a file.
  136. - [ZipFile::openFromString](#Documentation-ZipFile-openFromString) - opens a zip-archive from a string.
  137. - [ZipFile::openFromStream](#Documentation-ZipFile-openFromStream) - opens a zip-archive from the stream.
  138. - [ZipFile::outputAsAttachment](#Documentation-ZipFile-outputAsAttachment) - outputs a ZIP-archive to the browser.
  139. - [ZipFile::outputAsResponse](#Documentation-ZipFile-outputAsResponse) - outputs a ZIP-archive as PSR-7 Response.
  140. - [ZipFile::outputAsString](#Documentation-ZipFile-outputAsString) - outputs a ZIP-archive as string.
  141. - [ZipFile::rename](#Documentation-ZipFile-rename) - renames an entry defined by its name.
  142. - [ZipFile::rewrite](#Documentation-ZipFile-rewrite) - save changes and re-open the changed archive.
  143. - [ZipFile::saveAsFile](#Documentation-ZipFile-saveAsFile) - saves the archive to a file.
  144. - [ZipFile::saveAsStream](#Documentation-ZipFile-saveAsStream) - writes the archive to the stream.
  145. - [ZipFile::setArchiveComment](#Documentation-ZipFile-setArchiveComment) - set the comment of a ZIP archive.
  146. - [ZipFile::setCompressionLevel](#Documentation-ZipFile-setCompressionLevel) - set the compression level for all files in the archive.
  147. - [ZipFile::setCompressionLevelEntry](#Documentation-ZipFile-setCompressionLevelEntry) - sets the compression level for the entry by its name.
  148. - [ZipFile::setCompressionMethodEntry](#Documentation-ZipFile-setCompressionMethodEntry) - sets the compression method for the entry by its name.
  149. - [ZipFile::setEntryComment](#Documentation-ZipFile-setEntryComment) - set the comment of an entry defined by its name.
  150. - [ZipFile::setReadPassword](#Documentation-ZipFile-setReadPassword) - set the password for the open archive.
  151. - [ZipFile::setReadPasswordEntry](#Documentation-ZipFile-setReadPasswordEntry) - sets a password for reading of an entry defined by its name.
  152. - ~~ZipFile::withNewPassword~~ - is an deprecated method, use the [ZipFile::setPassword](#Documentation-ZipFile-setPassword) method.
  153. - [ZipFile::setPassword](#Documentation-ZipFile-setPassword) - sets a new password for all files in the archive.
  154. - [ZipFile::setPasswordEntry](#Documentation-ZipFile-setPasswordEntry) - sets a new password of an entry defined by its name.
  155. - [ZipFile::setZipAlign](#Documentation-ZipFile-setZipAlign) - sets the alignment of the archive to optimize APK files (Android packages).
  156. - [ZipFile::unchangeAll](#Documentation-ZipFile-unchangeAll) - undo all changes done in the archive.
  157. - [ZipFile::unchangeArchiveComment](#Documentation-ZipFile-unchangeArchiveComment) - undo changes to the archive comment.
  158. - [ZipFile::unchangeEntry](#Documentation-ZipFile-unchangeEntry) - undo changes of an entry defined by its name.
  159. - ~~ZipFile::withoutPassword~~ - is an deprecated method, use the [ZipFile::disableEncryption](#Documentation-ZipFile-disableEncryption) method.
  160. - ~~ZipFile::withReadPassword~~ - is an deprecated method, use the [ZipFile::setReadPassword](#Documentation-ZipFile-setReadPassword) method.
  161. #### <a name="Documentation-Open-Zip-Archive"></a> Creation/Opening of ZIP-archive
  162. <a name="Documentation-ZipFile-__construct"></a>**ZipFile::__construct** - initializes the ZIP archive.
  163. ```php
  164. $zipFile = new \PhpZip\ZipFile();
  165. ```
  166. <a name="Documentation-ZipFile-openFile"></a> **ZipFile::openFile** - opens a zip-archive from a file.
  167. ```php
  168. $zipFile = new \PhpZip\ZipFile();
  169. $zipFile->openFile('file.zip');
  170. ```
  171. <a name="Documentation-ZipFile-openFromString"></a> **ZipFile::openFromString** - opens a zip-archive from a string.
  172. ```php
  173. $zipFile = new \PhpZip\ZipFile();
  174. $zipFile->openFromString($stringContents);
  175. ```
  176. <a name="Documentation-ZipFile-openFromStream"></a> **ZipFile::openFromStream** - opens a zip-archive from the stream.
  177. ```php
  178. $stream = fopen('file.zip', 'rb');
  179. $zipFile = new \PhpZip\ZipFile();
  180. $zipFile->openFromStream($stream);
  181. ```
  182. #### <a name="Documentation-Open-Zip-Entries"></a> Reading entries from the archive
  183. <a name="Documentation-ZipFile-count"></a> **ZipFile::count** - returns the number of entries in the archive.
  184. ```php
  185. $zipFile = new \PhpZip\ZipFile();
  186. $count = count($zipFile);
  187. // or
  188. $count = $zipFile->count();
  189. ```
  190. <a name="Documentation-ZipFile-getListFiles"></a> **ZipFile::getListFiles** - returns list of archive files.
  191. ```php
  192. $zipFile = new \PhpZip\ZipFile();
  193. $listFiles = $zipFile->getListFiles();
  194. // example array contents:
  195. // array (
  196. // 0 => 'info.txt',
  197. // 1 => 'path/to/file.jpg',
  198. // 2 => 'another path/',
  199. // 3 => '0',
  200. // )
  201. ```
  202. <a name="Documentation-ZipFile-getEntryContent"></a> **ZipFile::getEntryContent** - returns the entry contents using its name.
  203. ```php
  204. // $entryName = 'path/to/example-entry-name.txt';
  205. $zipFile = new \PhpZip\ZipFile();
  206. $contents = $zipFile[$entryName];
  207. // or
  208. $contents = $zipFile->getEntryContents($entryName);
  209. ```
  210. <a name="Documentation-ZipFile-hasEntry"></a> **ZipFile::hasEntry** - checks if there is an entry in the archive.
  211. ```php
  212. // $entryName = 'path/to/example-entry-name.txt';
  213. $zipFile = new \PhpZip\ZipFile();
  214. $hasEntry = isset($zipFile[$entryName]);
  215. // or
  216. $hasEntry = $zipFile->hasEntry($entryName);
  217. ```
  218. <a name="Documentation-ZipFile-isDirectory"></a> **ZipFile::isDirectory** - checks that the entry in the archive is a directory.
  219. ```php
  220. // $entryName = 'path/to/';
  221. $zipFile = new \PhpZip\ZipFile();
  222. $isDirectory = $zipFile->isDirectory($entryName);
  223. ```
  224. <a name="Documentation-ZipFile-extractTo"></a> **ZipFile::extractTo** - extract the archive contents.
  225. The directory must exist.
  226. ```php
  227. $zipFile = new \PhpZip\ZipFile();
  228. $zipFile->extractTo($directory);
  229. ```
  230. Extract some files to the directory.
  231. The directory must exist.
  232. ```php
  233. // $toDirectory = '/tmp';
  234. $extractOnlyFiles = [
  235. 'filename1',
  236. 'filename2',
  237. 'dir/dir/dir/'
  238. ];
  239. $zipFile = new \PhpZip\ZipFile();
  240. $zipFile->extractTo($toDirectory, $extractOnlyFiles);
  241. ```
  242. #### <a name="Documentation-Zip-Iterate"></a> Iterating entries
  243. `ZipFile` is an iterator.
  244. Can iterate all the entries in the `foreach` loop.
  245. ```php
  246. foreach($zipFile as $entryName => $contents){
  247. echo "Filename: $entryName" . PHP_EOL;
  248. echo "Contents: $contents" . PHP_EOL;
  249. echo '-----------------------------' . PHP_EOL;
  250. }
  251. ```
  252. Can iterate through the `Iterator`.
  253. ```php
  254. $iterator = new \ArrayIterator($zipFile);
  255. while ($iterator->valid())
  256. {
  257. $entryName = $iterator->key();
  258. $contents = $iterator->current();
  259. echo "Filename: $entryName" . PHP_EOL;
  260. echo "Contents: $contents" . PHP_EOL;
  261. echo '-----------------------------' . PHP_EOL;
  262. $iterator->next();
  263. }
  264. ```
  265. #### <a name="Documentation-Zip-Info"></a> Getting information about entries
  266. <a name="Documentation-ZipFile-getArchiveComment"></a> **ZipFile::getArchiveComment** - returns the Zip archive comment.
  267. ```php
  268. $zipFile = new \PhpZip\ZipFile();
  269. $commentArchive = $zipFile->getArchiveComment();
  270. ```
  271. <a name="Documentation-ZipFile-getEntryComment"></a> **ZipFile::getEntryComment** - returns the comment of an entry using the entry name.
  272. ```php
  273. $zipFile = new \PhpZip\ZipFile();
  274. $commentEntry = $zipFile->getEntryComment($entryName);
  275. ```
  276. <a name="Documentation-ZipFile-getEntryInfo"></a> **ZipFile::getEntryInfo** - returns detailed information about the entry in the archive
  277. ```php
  278. $zipFile = new \PhpZip\ZipFile();
  279. $zipInfo = $zipFile->getEntryInfo('file.txt');
  280. ```
  281. <a name="Documentation-ZipFile-getAllInfo"></a> **ZipFile::getAllInfo** - returns detailed information about all entries in the archive.
  282. ```php
  283. $zipAllInfo = $zipFile->getAllInfo();
  284. ```
  285. #### <a name="Documentation-Add-Zip-Entries"></a> Adding entries to the archive
  286. All methods of adding entries to a ZIP archive allow you to specify a method for compressing content.
  287. The following methods of compression are available:
  288. - `\PhpZip\Constants\ZipCompressionMethod::STORED` - no compression
  289. - `\PhpZip\Constants\ZipCompressionMethod::DEFLATED` - Deflate compression
  290. - `\PhpZip\Constants\ZipCompressionMethod::BZIP2` - Bzip2 compression with the extension `ext-bz2`
  291. <a name="Documentation-ZipFile-addFile"></a> **ZipFile::addFile** - adds a file to a ZIP archive from the given path.
  292. ```php
  293. $zipFile = new \PhpZip\ZipFile();
  294. // $file = '...../file.ext';
  295. // $entryName = 'file2.ext'
  296. $zipFile->addFile($file);
  297. // you can specify the name of the entry in the archive (if null, then the last component from the file name is used)
  298. $zipFile->addFile($file, $entryName);
  299. // you can specify a compression method
  300. $zipFile->addFile($file, $entryName, \PhpZip\Constants\ZipCompressionMethod::STORED); // No compression
  301. $zipFile->addFile($file, $entryName, \PhpZip\Constants\ZipCompressionMethod::DEFLATED); // Deflate compression
  302. $zipFile->addFile($file, $entryName, \PhpZip\Constants\ZipCompressionMethod::BZIP2); // BZIP2 compression
  303. ```
  304. <a name="Documentation-ZipFile-addSplFile"></a>
  305. **ZipFile::addSplFile"** - adds a `\SplFileInfo` to a ZIP archive.
  306. ```php
  307. // $file = '...../file.ext';
  308. // $entryName = 'file2.ext'
  309. $zipFile = new \PhpZip\ZipFile();
  310. $splFile = new \SplFileInfo('README.md');
  311. $zipFile->addSplFile($splFile);
  312. $zipFile->addSplFile($splFile, $entryName);
  313. // or
  314. $zipFile[$entryName] = new \SplFileInfo($file);
  315. // set compression method
  316. $zipFile->addSplFile($splFile, $entryName, $options = [
  317. \PhpZip\Constants\ZipOptions::COMPRESSION_METHOD => \PhpZip\Constants\ZipCompressionMethod::DEFLATED,
  318. ]);
  319. ```
  320. <a name="Documentation-ZipFile-addFromFinder"></a>
  321. **ZipFile::addFromFinder"** - adds files from the `Symfony\Component\Finder\Finder` to a ZIP archive.
  322. https://symfony.com/doc/current/components/finder.html
  323. ```php
  324. $finder = new \Symfony\Component\Finder\Finder();
  325. $finder
  326. ->files()
  327. ->name('*.{jpg,jpeg,gif,png}')
  328. ->name('/^[0-9a-f]\./')
  329. ->contains('/lorem\s+ipsum$/i')
  330. ->in('path');
  331. $zipFile = new \PhpZip\ZipFile();
  332. $zipFile->addFromFinder($finder, $options = [
  333. \PhpZip\Constants\ZipOptions::COMPRESSION_METHOD => \PhpZip\Constants\ZipCompressionMethod::DEFLATED,
  334. \PhpZip\Constants\ZipOptions::MODIFIED_TIME => new \DateTimeImmutable('-1 day 5 min')
  335. ]);
  336. ```
  337. <a name="Documentation-ZipFile-addFromString"></a> **ZipFile::addFromString** - adds a file to a ZIP archive using its contents.
  338. ```php
  339. $zipFile = new \PhpZip\ZipFile();
  340. $zipFile[$entryName] = $contents;
  341. // or
  342. $zipFile->addFromString($entryName, $contents);
  343. // you can specify a compression method
  344. $zipFile->addFromString($entryName, $contents, \PhpZip\Constants\ZipCompressionMethod::STORED); // No compression
  345. $zipFile->addFromString($entryName, $contents, \PhpZip\Constants\ZipCompressionMethod::DEFLATED); // Deflate compression
  346. $zipFile->addFromString($entryName, $contents, \PhpZip\Constants\ZipCompressionMethod::BZIP2); // BZIP2 compression
  347. ```
  348. <a name="Documentation-ZipFile-addFromStream"></a> **ZipFile::addFromStream** - adds a entry from the stream to the ZIP archive.
  349. ```php
  350. $zipFile = new \PhpZip\ZipFile();
  351. // $stream = fopen(..., 'rb');
  352. $zipFile->addFromStream($stream, $entryName);
  353. // or
  354. $zipFile[$entryName] = $stream;
  355. // you can specify a compression method
  356. $zipFile->addFromStream($stream, $entryName, \PhpZip\Constants\ZipCompressionMethod::STORED); // No compression
  357. $zipFile->addFromStream($stream, $entryName, \PhpZip\Constants\ZipCompressionMethod::DEFLATED); // Deflate compression
  358. $zipFile->addFromStream($stream, $entryName, \PhpZip\Constants\ZipCompressionMethod::BZIP2); // BZIP2 compression
  359. ```
  360. <a name="Documentation-ZipFile-addEmptyDir"></a> **ZipFile::addEmptyDir** - add a new directory.
  361. ```php
  362. $zipFile = new \PhpZip\ZipFile();
  363. // $path = "path/to/";
  364. $zipFile->addEmptyDir($path);
  365. // or
  366. $zipFile[$path] = null;
  367. ```
  368. <a name="Documentation-ZipFile-addAll"></a> **ZipFile::addAll** - adds all entries from an array.
  369. ```php
  370. $entries = [
  371. 'file.txt' => 'file contents', // add an entry from the string contents
  372. 'empty dir/' => null, // add empty directory
  373. 'path/to/file.jpg' => fopen('..../filename', 'rb'), // add an entry from the stream
  374. 'path/to/file.dat' => new \SplFileInfo('..../filename'), // add an entry from the file
  375. ];
  376. $zipFile = new \PhpZip\ZipFile();
  377. $zipFile->addAll($entries);
  378. ```
  379. <a name="Documentation-ZipFile-addDir"></a> **ZipFile::addDir** - adds files to the archive from the directory on the specified path without subdirectories.
  380. ```php
  381. $zipFile = new \PhpZip\ZipFile();
  382. $zipFile->addDir($dirName);
  383. // you can specify the path in the archive to which you want to put entries
  384. $localPath = 'to/path/';
  385. $zipFile->addDir($dirName, $localPath);
  386. // you can specify a compression method
  387. $zipFile->addDir($dirName, $localPath, \PhpZip\Constants\ZipCompressionMethod::STORED); // No compression
  388. $zipFile->addDir($dirName, $localPath, \PhpZip\Constants\ZipCompressionMethod::DEFLATED); // Deflate compression
  389. $zipFile->addDir($dirName, $localPath, \PhpZip\Constants\ZipCompressionMethod::BZIP2); // BZIP2 compression
  390. ```
  391. <a name="Documentation-ZipFile-addDirRecursive"></a> **ZipFile::addDirRecursive** - adds files to the archive from the directory on the specified path with subdirectories.
  392. ```php
  393. $zipFile = new \PhpZip\ZipFile();
  394. $zipFile->addDirRecursive($dirName);
  395. // you can specify the path in the archive to which you want to put entries
  396. $localPath = 'to/path/';
  397. $zipFile->addDirRecursive($dirName, $localPath);
  398. // you can specify a compression method
  399. $zipFile->addDirRecursive($dirName, $localPath, \PhpZip\Constants\ZipCompressionMethod::STORED); // No compression
  400. $zipFile->addDirRecursive($dirName, $localPath, \PhpZip\Constants\ZipCompressionMethod::DEFLATED); // Deflate compression
  401. $zipFile->addDirRecursive($dirName, $localPath, \PhpZip\Constants\ZipCompressionMethod::BZIP2); // BZIP2 compression
  402. ```
  403. <a name="Documentation-ZipFile-addFilesFromIterator"></a> **ZipFile::addFilesFromIterator** - adds files from the iterator of directories.
  404. ```php
  405. // $directoryIterator = new \DirectoryIterator($dir); // without subdirectories
  406. // $directoryIterator = new \RecursiveDirectoryIterator($dir); // with subdirectories
  407. $zipFile = new \PhpZip\ZipFile();
  408. $zipFile->addFilesFromIterator($directoryIterator);
  409. // you can specify the path in the archive to which you want to put entries
  410. $localPath = 'to/path/';
  411. $zipFile->addFilesFromIterator($directoryIterator, $localPath);
  412. // or
  413. $zipFile[$localPath] = $directoryIterator;
  414. // you can specify a compression method
  415. $zipFile->addFilesFromIterator($directoryIterator, $localPath, \PhpZip\Constants\ZipCompressionMethod::STORED); // No compression
  416. $zipFile->addFilesFromIterator($directoryIterator, $localPath, \PhpZip\Constants\ZipCompressionMethod::DEFLATED); // Deflate compression
  417. $zipFile->addFilesFromIterator($directoryIterator, $localPath, \PhpZip\Constants\ZipCompressionMethod::BZIP2); // BZIP2 compression
  418. ```
  419. Example with some files ignoring:
  420. ```php
  421. $ignoreFiles = [
  422. 'file_ignore.txt',
  423. 'dir_ignore/sub dir ignore/'
  424. ];
  425. // $directoryIterator = new \DirectoryIterator($dir); // without subdirectories
  426. // $directoryIterator = new \RecursiveDirectoryIterator($dir); // with subdirectories
  427. // use \PhpZip\Util\Iterator\IgnoreFilesFilterIterator for non-recursive search
  428. $zipFile = new \PhpZip\ZipFile();
  429. $ignoreIterator = new \PhpZip\Util\Iterator\IgnoreFilesRecursiveFilterIterator(
  430. $directoryIterator,
  431. $ignoreFiles
  432. );
  433. $zipFile->addFilesFromIterator($ignoreIterator);
  434. ```
  435. <a name="Documentation-ZipFile-addFilesFromGlob"></a> **ZipFile::addFilesFromGlob** - adds files from a directory by [glob pattern](https://en.wikipedia.org/wiki/Glob_(programming)) without subdirectories.
  436. ```php
  437. $globPattern = '**.{jpg,jpeg,png,gif}'; // example glob pattern -> add all .jpg, .jpeg, .png and .gif files
  438. $zipFile = new \PhpZip\ZipFile();
  439. $zipFile->addFilesFromGlob($dir, $globPattern);
  440. // you can specify the path in the archive to which you want to put entries
  441. $localPath = 'to/path/';
  442. $zipFile->addFilesFromGlob($dir, $globPattern, $localPath);
  443. // you can specify a compression method
  444. $zipFile->addFilesFromGlob($dir, $globPattern, $localPath, \PhpZip\Constants\ZipCompressionMethod::STORED); // No compression
  445. $zipFile->addFilesFromGlob($dir, $globPattern, $localPath, \PhpZip\Constants\ZipCompressionMethod::DEFLATED); // Deflate compression
  446. $zipFile->addFilesFromGlob($dir, $globPattern, $localPath, \PhpZip\Constants\ZipCompressionMethod::BZIP2); // BZIP2 compression
  447. ```
  448. <a name="Documentation-ZipFile-addFilesFromGlobRecursive"></a> **ZipFile::addFilesFromGlobRecursive** - adds files from a directory by [glob pattern](https://en.wikipedia.org/wiki/Glob_(programming)) with subdirectories.
  449. ```php
  450. $globPattern = '**.{jpg,jpeg,png,gif}'; // example glob pattern -> add all .jpg, .jpeg, .png and .gif files
  451. $zipFile = new \PhpZip\ZipFile();
  452. $zipFile->addFilesFromGlobRecursive($dir, $globPattern);
  453. // you can specify the path in the archive to which you want to put entries
  454. $localPath = 'to/path/';
  455. $zipFile->addFilesFromGlobRecursive($dir, $globPattern, $localPath);
  456. // you can specify a compression method
  457. $zipFile->addFilesFromGlobRecursive($dir, $globPattern, $localPath, \PhpZip\Constants\ZipCompressionMethod::STORED); // No compression
  458. $zipFile->addFilesFromGlobRecursive($dir, $globPattern, $localPath, \PhpZip\Constants\ZipCompressionMethod::DEFLATED); // Deflate compression
  459. $zipFile->addFilesFromGlobRecursive($dir, $globPattern, $localPath, \PhpZip\Constants\ZipCompressionMethod::BZIP2); // BZIP2 compression
  460. ```
  461. <a name="Documentation-ZipFile-addFilesFromRegex"></a> **ZipFile::addFilesFromRegex** - adds files from a directory by [PCRE pattern](https://en.wikipedia.org/wiki/Regular_expression) without subdirectories.
  462. ```php
  463. $regexPattern = '/\.(jpe?g|png|gif)$/si'; // example regex pattern -> add all .jpg, .jpeg, .png and .gif files
  464. $zipFile = new \PhpZip\ZipFile();
  465. $zipFile->addFilesFromRegex($dir, $regexPattern);
  466. // you can specify the path in the archive to which you want to put entries
  467. $localPath = 'to/path/';
  468. $zipFile->addFilesFromRegex($dir, $regexPattern, $localPath);
  469. // you can specify a compression method
  470. $zipFile->addFilesFromRegex($dir, $regexPattern, $localPath, \PhpZip\Constants\ZipCompressionMethod::STORED); // No compression
  471. $zipFile->addFilesFromRegex($dir, $regexPattern, $localPath, \PhpZip\Constants\ZipCompressionMethod::DEFLATED); // Deflate compression
  472. $zipFile->addFilesFromRegex($dir, $regexPattern, $localPath, \PhpZip\Constants\ZipCompressionMethod::BZIP2); // BZIP2 compression
  473. ```
  474. <a name="Documentation-ZipFile-addFilesFromRegexRecursive"></a> **ZipFile::addFilesFromRegexRecursive** - adds files from a directory by [PCRE pattern](https://en.wikipedia.org/wiki/Regular_expression) with subdirectories.
  475. ```php
  476. $regexPattern = '/\.(jpe?g|png|gif)$/si'; // example regex pattern -> add all .jpg, .jpeg, .png and .gif files
  477. $zipFile->addFilesFromRegexRecursive($dir, $regexPattern);
  478. // you can specify the path in the archive to which you want to put entries
  479. $localPath = 'to/path/';
  480. $zipFile->addFilesFromRegexRecursive($dir, $regexPattern, $localPath);
  481. // you can specify a compression method
  482. $zipFile->addFilesFromRegexRecursive($dir, $regexPattern, $localPath, \PhpZip\Constants\ZipCompressionMethod::STORED); // No compression
  483. $zipFile->addFilesFromRegexRecursive($dir, $regexPattern, $localPath, \PhpZip\Constants\ZipCompressionMethod::DEFLATED); // Deflate compression
  484. $zipFile->addFilesFromRegexRecursive($dir, $regexPattern, $localPath, \PhpZip\Constants\ZipCompressionMethod::BZIP2); // BZIP2 compression
  485. ```
  486. #### <a name="Documentation-Remove-Zip-Entries"></a> Deleting entries from the archive
  487. <a name="Documentation-ZipFile-deleteFromName"></a> **ZipFile::deleteFromName** - deletes an entry in the archive using its name.
  488. ```php
  489. $zipFile = new \PhpZip\ZipFile();
  490. $zipFile->deleteFromName($entryName);
  491. ```
  492. <a name="Documentation-ZipFile-deleteFromGlob"></a> **ZipFile::deleteFromGlob** - deletes a entries in the archive using [glob pattern](https://en.wikipedia.org/wiki/Glob_(programming)).
  493. ```php
  494. $globPattern = '**.{jpg,jpeg,png,gif}'; // example glob pattern -> delete all .jpg, .jpeg, .png and .gif files
  495. $zipFile = new \PhpZip\ZipFile();
  496. $zipFile->deleteFromGlob($globPattern);
  497. ```
  498. <a name="Documentation-ZipFile-deleteFromRegex"></a> **ZipFile::deleteFromRegex** - deletes a entries in the archive using [PCRE pattern](https://en.wikipedia.org/wiki/Regular_expression).
  499. ```php
  500. $regexPattern = '/\.(jpe?g|png|gif)$/si'; // example regex pattern -> delete all .jpg, .jpeg, .png and .gif files
  501. $zipFile = new \PhpZip\ZipFile();
  502. $zipFile->deleteFromRegex($regexPattern);
  503. ```
  504. <a name="Documentation-ZipFile-deleteAll"></a> **ZipFile::deleteAll** - deletes all entries in the ZIP archive.
  505. ```php
  506. $zipFile = new \PhpZip\ZipFile();
  507. $zipFile->deleteAll();
  508. ```
  509. #### <a name="Documentation-Entries"></a> Working with entries and archive
  510. <a name="Documentation-ZipFile-rename"></a> **ZipFile::rename** - renames an entry defined by its name.
  511. ```php
  512. $zipFile = new \PhpZip\ZipFile();
  513. $zipFile->rename($oldName, $newName);
  514. ```
  515. <a name="Documentation-ZipFile-setCompressionLevel"></a> **ZipFile::setCompressionLevel** - set the compression level for all files in the archive.
  516. > _Note that this method does not apply to entries that are added after this method is run._
  517. By default, the compression level is 5 (`\PhpZip\Constants\ZipCompressionLevel::NORMAL`) or the compression level specified in the archive for Deflate compression.
  518. The values range from 1 (`\PhpZip\Constants\ZipCompressionLevel::SUPER_FAST`) to 9 (`\PhpZip\Constants\ZipCompressionLevel::MAXIMUM`) are supported. The higher the number, the better and longer the compression.
  519. ```php
  520. $zipFile = new \PhpZip\ZipFile();
  521. $zipFile->setCompressionLevel(\PhpZip\Constants\ZipCompressionLevel::MAXIMUM);
  522. ```
  523. <a name="Documentation-ZipFile-setCompressionLevelEntry"></a> **ZipFile::setCompressionLevelEntry** - sets the compression level for the entry by its name.
  524. The values range from 1 (`\PhpZip\Constants\ZipCompressionLevel::SUPER_FAST`) to 9 (`\PhpZip\Constants\ZipCompressionLevel::MAXIMUM`) are supported. The higher the number, the better and longer the compression.
  525. ```php
  526. $zipFile = new \PhpZip\ZipFile();
  527. $zipFile->setCompressionLevelEntry($entryName, \PhpZip\Constants\ZipCompressionLevel::FAST);
  528. ```
  529. <a name="Documentation-ZipFile-setCompressionMethodEntry"></a> **ZipFile::setCompressionMethodEntry** - sets the compression method for the entry by its name.
  530. The following compression methods are available:
  531. - `\PhpZip\Constants\ZipCompressionMethod::STORED` - No compression
  532. - `\PhpZip\Constants\ZipCompressionMethod::DEFLATED` - Deflate compression
  533. - `\PhpZip\Constants\ZipCompressionMethod::BZIP2` - Bzip2 compression with the extension `ext-bz2`
  534. ```php
  535. $zipFile = new \PhpZip\ZipFile();
  536. $zipFile->setCompressionMethodEntry($entryName, \PhpZip\Constants\ZipCompressionMethod::DEFLATED);
  537. ```
  538. <a name="Documentation-ZipFile-setArchiveComment"></a> **ZipFile::setArchiveComment** - set the comment of a ZIP archive.
  539. ```php
  540. $zipFile = new \PhpZip\ZipFile();
  541. $zipFile->setArchiveComment($commentArchive);
  542. ```
  543. <a name="Documentation-ZipFile-setEntryComment"></a> **ZipFile::setEntryComment** - set the comment of an entry defined by its name.
  544. ```php
  545. $zipFile = new \PhpZip\ZipFile();
  546. $zipFile->setEntryComment($entryName, $comment);
  547. ```
  548. <a name="Documentation-ZipFile-matcher"></a> **ZipFile::matcher** - selecting entries in the archive to perform operations on them.
  549. ```php
  550. $zipFile = new \PhpZip\ZipFile();
  551. $matcher = $zipFile->matcher();
  552. ```
  553. Selecting files from the archive one at a time:
  554. ```php
  555. $matcher
  556. ->add('entry name')
  557. ->add('another entry');
  558. ```
  559. Select multiple files in the archive:
  560. ```php
  561. $matcher->add([
  562. 'entry name',
  563. 'another entry name',
  564. 'path/'
  565. ]);
  566. ```
  567. Selecting files by regular expression:
  568. ```php
  569. $matcher->match('~\.jpe?g$~i');
  570. ```
  571. Select all files in the archive:
  572. ```php
  573. $matcher->all();
  574. ```
  575. count() - gets the number of selected entries:
  576. ```php
  577. $count = count($matcher);
  578. // or
  579. $count = $matcher->count();
  580. ```
  581. getMatches() - returns a list of selected entries:
  582. ```php
  583. $entries = $matcher->getMatches();
  584. // example array contents: ['entry name', 'another entry name'];
  585. ```
  586. invoke() - invoke a callable function on selected entries:
  587. ```php
  588. // example
  589. $matcher->invoke(static function($entryName) use($zipFile) {
  590. $newName = preg_replace('~\.(jpe?g)$~i', '.no_optimize.$1', $entryName);
  591. $zipFile->rename($entryName, $newName);
  592. });
  593. ```
  594. Functions for working on the selected entries:
  595. ```php
  596. $matcher->delete(); // remove selected entries from a ZIP archive
  597. $matcher->setPassword($password); // sets a new password for the selected entries
  598. $matcher->setPassword($password, $encryptionMethod); // sets a new password and encryption method to selected entries
  599. $matcher->setEncryptionMethod($encryptionMethod); // sets the encryption method to the selected entries
  600. $matcher->disableEncryption(); // disables encryption for selected entries
  601. ```
  602. #### <a name="Documentation-Password"></a> Working with passwords
  603. Implemented support for encryption methods:
  604. - `\PhpZip\Constants\ZipEncryptionMethod::PKWARE` - Traditional PKWARE encryption (legacy)
  605. - `\PhpZip\Constants\ZipEncryptionMethod::WINZIP_AES_256` - WinZip AES encryption 256 bit (recommended)
  606. - `\PhpZip\Constants\ZipEncryptionMethod::WINZIP_AES_192` - WinZip AES encryption 192 bit
  607. - `\PhpZip\Constants\ZipEncryptionMethod::WINZIP_AES_128` - WinZip AES encryption 128 bit
  608. <a name="Documentation-ZipFile-setReadPassword"></a> **ZipFile::setReadPassword** - set the password for the open archive.
  609. > _Setting a password is not required for adding new entries or deleting existing ones, but if you want to extract the content or change the method / compression level, the encryption method, or change the password, in this case the password must be specified._
  610. ```php
  611. $zipFile->setReadPassword($password);
  612. ```
  613. <a name="Documentation-ZipFile-setReadPasswordEntry"></a> **ZipFile::setReadPasswordEntry** - gets a password for reading of an entry defined by its name.
  614. ```php
  615. $zipFile->setReadPasswordEntry($entryName, $password);
  616. ```
  617. <a name="Documentation-ZipFile-setPassword"></a> **ZipFile::setPassword** - sets a new password for all files in the archive.
  618. > _Note that this method does not apply to entries that are added after this method is run._
  619. ```php
  620. $zipFile->setPassword($password);
  621. ```
  622. You can set the encryption method:
  623. ```php
  624. $encryptionMethod = \PhpZip\Constants\ZipEncryptionMethod::WINZIP_AES_256;
  625. $zipFile->setPassword($password, $encryptionMethod);
  626. ```
  627. <a name="Documentation-ZipFile-setPasswordEntry"></a> **ZipFile::setPasswordEntry** - sets a new password of an entry defined by its name.
  628. ```php
  629. $zipFile->setPasswordEntry($entryName, $password);
  630. ```
  631. You can set the encryption method:
  632. ```php
  633. $encryptionMethod = \PhpZip\Constants\ZipEncryptionMethod::WINZIP_AES_256;
  634. $zipFile->setPasswordEntry($entryName, $password, $encryptionMethod);
  635. ```
  636. <a name="Documentation-ZipFile-disableEncryption"></a> **ZipFile::disableEncryption** - disable encryption for all entries that are already in the archive.
  637. > _Note that this method does not apply to entries that are added after this method is run._
  638. ```php
  639. $zipFile->disableEncryption();
  640. ```
  641. <a name="Documentation-ZipFile-disableEncryptionEntry"></a> **ZipFile::disableEncryptionEntry** - disable encryption of an entry defined by its name.
  642. ```php
  643. $zipFile->disableEncryptionEntry($entryName);
  644. ```
  645. #### <a name="Documentation-ZipAlign-Usage"></a> zipalign
  646. <a name="Documentation-ZipFile-setZipAlign"></a> **ZipFile::setZipAlign** - sets the alignment of the archive to optimize APK files (Android packages).
  647. This method adds padding to unencrypted and not compressed entries, to optimize memory consumption in the Android system. It is recommended to use for `APK` files. The file may grow slightly.
  648. This method is an alternative to executing the `zipalign -f -v 4 filename.zip`.
  649. More details can be found on the [link](https://developer.android.com/studio/command-line/zipalign.html).
  650. ```php
  651. $zipFile->setZipAlign(4);
  652. ```
  653. #### <a name="Documentation-Unchanged"></a> Undo changes
  654. <a name="Documentation-ZipFile-unchangeAll"></a> **ZipFile::unchangeAll** - undo all changes done in the archive.
  655. ```php
  656. $zipFile->unchangeAll();
  657. ```
  658. <a name="Documentation-ZipFile-unchangeArchiveComment"></a> **ZipFile::unchangeArchiveComment** - undo changes to the archive comment.
  659. ```php
  660. $zipFile->unchangeArchiveComment();
  661. ```
  662. <a name="Documentation-ZipFile-unchangeEntry"></a> **ZipFile::unchangeEntry** - undo changes of an entry defined by its name.
  663. ```php
  664. $zipFile->unchangeEntry($entryName);
  665. ```
  666. #### <a name="Documentation-Save-Or-Output-Entries"></a> Saving a file or output to a browser
  667. <a name="Documentation-ZipFile-saveAsFile"></a> **ZipFile::saveAsFile** - saves the archive to a file.
  668. ```php
  669. $zipFile->saveAsFile($filename);
  670. ```
  671. <a name="Documentation-ZipFile-saveAsStream"></a> **ZipFile::saveAsStream** - writes the archive to the stream.
  672. ```php
  673. // $fp = fopen($filename, 'w+b');
  674. $zipFile->saveAsStream($fp);
  675. ```
  676. <a name="Documentation-ZipFile-outputAsString"></a> **ZipFile::outputAsString** - outputs a ZIP-archive as string.
  677. ```php
  678. $rawZipArchiveBytes = $zipFile->outputAsString();
  679. ```
  680. <a name="Documentation-ZipFile-outputAsAttachment"></a> **ZipFile::outputAsAttachment** - outputs a ZIP-archive to the browser.
  681. ```php
  682. $zipFile->outputAsAttachment($outputFilename);
  683. ```
  684. You can set the Mime-Type:
  685. ```php
  686. $mimeType = 'application/zip';
  687. $zipFile->outputAsAttachment($outputFilename, $mimeType);
  688. ```
  689. <a name="Documentation-ZipFile-outputAsResponse"></a> **ZipFile::outputAsResponse** - outputs a ZIP-archive as [PSR-7 Response](http://www.php-fig.org/psr/psr-7/).
  690. The output method can be used in any PSR-7 compatible framework.
  691. ```php
  692. // $response = ....; // instance Psr\Http\Message\ResponseInterface
  693. $zipFile->outputAsResponse($response, $outputFilename);
  694. ```
  695. You can set the Mime-Type:
  696. ```php
  697. $mimeType = 'application/zip';
  698. $zipFile->outputAsResponse($response, $outputFilename, $mimeType);
  699. ```
  700. <a name="Documentation-ZipFile-rewrite"></a> **ZipFile::rewrite** - save changes and re-open the changed archive.
  701. ```php
  702. $zipFile->rewrite();
  703. ```
  704. #### <a name="Documentation-Close-Zip-Archive"></a> Closing the archive
  705. <a name="Documentation-ZipFile-close"></a> **ZipFile::close** - close the archive.
  706. ```php
  707. $zipFile->close();
  708. ```
  709. ### <a name="Running-Tests"></a> Running the tests
  710. Install the dependencies for the development:
  711. ```bash
  712. composer install --dev
  713. ```
  714. Run the tests:
  715. ```bash
  716. vendor/bin/phpunit
  717. ```
  718. ### <a name="Changelog"></a> Changelog
  719. Changes are documented in the [releases page](https://github.com/Ne-Lexa/php-zip/releases).
  720. ### <a name="Upgrade"></a> Upgrade
  721. #### <a name="Upgrade-v2-to-v3"></a> Upgrade version 2 to version 3.0
  722. Update the major version in the file `composer.json` to `^3.0`.
  723. ```json
  724. {
  725. "require": {
  726. "nelexa/zip": "^3.0"
  727. }
  728. }
  729. ```
  730. Then install updates using `Composer`:
  731. ```bash
  732. composer update nelexa/zip
  733. ```
  734. Update your code to work with the new version:
  735. - Class `ZipOutputFile` merged to `ZipFile` and removed.
  736. + `new \PhpZip\ZipOutputFile()` to `new \PhpZip\ZipFile()`
  737. - Static initialization methods are now not static.
  738. + `\PhpZip\ZipFile::openFromFile($filename);` to `(new \PhpZip\ZipFile())->openFile($filename);`
  739. + `\PhpZip\ZipOutputFile::openFromFile($filename);` to `(new \PhpZip\ZipFile())->openFile($filename);`
  740. + `\PhpZip\ZipFile::openFromString($contents);` to `(new \PhpZip\ZipFile())->openFromString($contents);`
  741. + `\PhpZip\ZipFile::openFromStream($stream);` to `(new \PhpZip\ZipFile())->openFromStream($stream);`
  742. + `\PhpZip\ZipOutputFile::create()` to `new \PhpZip\ZipFile()`
  743. + `\PhpZip\ZipOutputFile::openFromZipFile(\PhpZip\ZipFile $zipFile)` &gt; `(new \PhpZip\ZipFile())->openFile($filename);`
  744. - Rename methods:
  745. + `addFromFile` to `addFile`
  746. + `setLevel` to `setCompressionLevel`
  747. + `ZipFile::setPassword` to `ZipFile::withReadPassword`
  748. + `ZipOutputFile::setPassword` to `ZipFile::withNewPassword`
  749. + `ZipOutputFile::disableEncryptionAllEntries` to `ZipFile::withoutPassword`
  750. + `ZipOutputFile::setComment` to `ZipFile::setArchiveComment`
  751. + `ZipFile::getComment` to `ZipFile::getArchiveComment`
  752. - Changed signature for methods `addDir`, `addFilesFromGlob`, `addFilesFromRegex`.
  753. - Remove methods:
  754. + `getLevel`
  755. + `setCompressionMethod`
  756. + `setEntryPassword`