index.rst 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. Documentation
  2. =============
  3. Introduction
  4. ------------
  5. Zippy is an Object Oriented PHP library that aim to ease the use of the archive
  6. manipulation by providing a set of adapters that will use command line utilities or PHP
  7. extensions depending on the environment your run it
  8. Zippy currently supports the following utilities :
  9. - `GNU TAR`_
  10. - `BSD TAR`_
  11. - `ZIP`_
  12. And deals with the following archive formats :
  13. - tar
  14. - zip
  15. - tbz2
  16. - tbz
  17. - tgz
  18. Installation
  19. ------------
  20. We rely on `composer`_ to use this library. If you do
  21. not still use composer for your project, you can start with this ``composer.json``
  22. at the root of your project :
  23. .. code-block:: json
  24. {
  25. "require": {
  26. "alchemy/zippy": " ~0.1"
  27. }
  28. }
  29. Install composer :
  30. .. code-block:: bash
  31. # Install composer
  32. curl -s http://getcomposer.org/installer | php
  33. # Upgrade your install
  34. php composer.phar install
  35. You now just have to autoload the library to use it :
  36. .. code-block:: php
  37. <?php
  38. require 'vendor/autoload.php';
  39. use Zippy\Zippy;
  40. $zippy = Zippy::load();
  41. This is a very short intro to composer.
  42. If you ever experience an issue or want to know more about composer,
  43. you will find help on their web site `composer`_.
  44. Basic Usage
  45. -----------
  46. The Zippy library is very simple and consists of a collection of adapters that
  47. take over for you the most common (de)compression operations (create, list
  48. update, extract, delete) for the chosen format.
  49. **Example usage**
  50. .. code-block:: php
  51. <?php
  52. use Alchemy\Zippy;
  53. $zippy = Zippy::load();
  54. // creates
  55. $archiveZip = $zippy->create('archive.zip');
  56. // updates
  57. $archiveZip->addMembers(array(
  58. '/path/to/file',
  59. '/path/to/file2',
  60. '/path/to/dir'
  61. ),
  62. $recursive = false
  63. );
  64. // deletes
  65. $archiveZip->removeMembers('/path/to/file2');
  66. // lists
  67. foreach ($archiveZip as $member) {
  68. if ($member->isDir()) {
  69. continue;
  70. }
  71. echo $member->getLocation(); // outputs /path/to/file
  72. }
  73. // extracts
  74. $archiveZip->extract('/to/directory');
  75. Zippy comes with a strategy pattern to get the best adapter according to the
  76. platform you use and the availability of the utilities.
  77. The right adapter will be matched when you open or create a new archive.
  78. **Creates or opens one archive**
  79. .. code-block:: php
  80. <?php
  81. use Alchemy\Zippy;
  82. $zippy = Zippy::load();
  83. $archiveZip = $zippy->create('archive.zip');
  84. $archiveTar = $zippy->open('/an/existing/archive.tar');
  85. However you may want sometimes gets the adapter for future reuse as the previous
  86. example is good for one shot only because it will create a new adapter object
  87. instance each time you create or open an archive.
  88. **Creates or opens a lot of archives**
  89. .. code-block:: php
  90. <?php
  91. use Alchemy\Zippy;
  92. $zippy = Zippy::load();
  93. $zipAdapter = $zippy->getAdapterFor('zip');
  94. foreach(array('archive.zip', 'archive2.zip', 'archive3.zip') as $path) {
  95. $archive = zipAdapter->open(path);
  96. }
  97. Also sometimes you will face the problem where Zippy will not be able to handle
  98. a specific archive format because archive extension is not recognized or follow
  99. specific named rules.
  100. Luckily with Zippy You can easily define your strategy to get a specific adapter
  101. that handle (de)compression for a specific archive format.
  102. The discrimination factor for getting the right adapter is based upon the
  103. archive extension.
  104. So every time you will work with an archive format not
  105. handled by Zippy you must declare a new strategy for this extension
  106. to match the proper adapter, see :ref:`add-custom-strategy`.
  107. Recipes
  108. -------
  109. Define custom binary path
  110. ^^^^^^^^^^^^^^^^^^^^^^^^^
  111. Each binary utility comes with two binary path one for the inflator and the other
  112. for the deflator. By default if none is provided, zippy will look to find
  113. the executable by its name;
  114. .. code-block:: php
  115. <?php
  116. use Alchemy\Zippy;
  117. $zippy = Zippy::load();
  118. // customize GNU Tar inflator
  119. $zippy->adapters['gnu-tar.inflator'] = '/usr/local/bin/tar';
  120. // customize ZIP deflator
  121. $zippy->adapters['zip.deflator'] = '/usr/local/bin/unzip';
  122. The following binary are customable
  123. - gnu-tar.inflator
  124. - gnu-tar.deflator
  125. - bsd-tar.inflator
  126. - bsd-tar.deflator
  127. - zip.inflator
  128. - zip.deflator
  129. .. _add-custom-strategy:
  130. Add custom utility strategy
  131. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  132. Zippy provides a way to define your custom strategy based on the file extension
  133. to get the most adapted adapters according to your needs.
  134. Each adapters implements a *isSupported()* method which will be executed for
  135. the defined list of adapters. The first supported adapter will be chosen as
  136. the archive adapter.
  137. **Define your custom adapter**
  138. Your custom adapter class must implements the
  139. ``Alchemy\Zippy\Adapter\AdapterInterface``.
  140. .. code-block:: php
  141. <?php
  142. use Alchemy\Zippy;
  143. class CustomAdapter implements Zippy\Adapter\AdapterInterface
  144. {
  145. ...
  146. }
  147. **Define a new strategy**
  148. Your custom strategy class must implements the
  149. ``Alchemy\Zippy\Strategy\FileStrategy``.
  150. .. code-block:: php
  151. <?php
  152. use Alchemy\Zippy;
  153. class CustomStrategy implements Zippy\Strategy\FileStrategy
  154. {
  155. public function getAdapters()
  156. {
  157. return array(CustomAdapter::newInstance());
  158. }
  159. public function getFileExtension()
  160. {
  161. return 'tar.custom';
  162. }
  163. }
  164. **Add your custom strategy into zippy**
  165. .. code-block:: php
  166. <?php
  167. $zippy = Alchemy\Zippy::load();
  168. // add your strategy
  169. // This strategy for `tar.custom` files has priority over all previously
  170. // registered strategies for this extension
  171. $zippy->addStrategy(new CustomStrategy());
  172. // use it
  173. $archiveTarCustom = $zippy->create('archive.tar.custom');
  174. Handling Exceptions
  175. -------------------
  176. Zippy throws different types of exception :
  177. - ``\Alchemy\Zippy\Exception\NotSupportedException``
  178. is thrown when current operation is not supported.
  179. - ``\Alchemy\Zippy\Exception\RunTimeException``
  180. - ``\Alchemy\Zippy\Exception\InvalidArgumentException``
  181. All these Exception implements ``\Alchemy\Zippy\Exception\ExceptionInterface``
  182. so you can catch any of these exceptions by catching this exception interface.
  183. Report a bug
  184. ------------
  185. If you experience an issue, please report it in our `issue tracker`_. Before
  186. reporting an issue, please be sure that it is not already reported by browsing
  187. open issues.
  188. Contribute
  189. ----------
  190. You find a bug and resolved it ? You added a feature and want to share ? You
  191. found a typo in this doc and fixed it ? Feel free to send a `Pull Request`_ on
  192. GitHub, we will be glad to merge your code.
  193. Run tests
  194. ---------
  195. Zippy relies on `PHPUnit`_ for unit tests. To run tests on your system, ensure
  196. you have `PHPUnit`_ installed, and, at the root of Zippy execute it :
  197. .. code-block:: bash
  198. phpunit
  199. About
  200. -----
  201. Zippy has been written by the `Alchemy`_ dev team for `Phraseanet`_, our DAM
  202. software. Try it, it's awesome !
  203. License
  204. -------
  205. Zippy is licensed under the `MIT License`_
  206. .. _composer: http://getcomposer.org/
  207. .. _GNU TAR: http://www.gnu.org/software/tar/manual/
  208. .. _BSD TAR: http://www.freebsd.org/cgi/man.cgi?query=tar&sektion=1
  209. .. _ZIP: http://www.info-zip.org/
  210. .. _issue tracker: https://github.com/alchemy-fr/Zippy/issues
  211. .. _Pull Request: http://help.github.com/send-pull-requests/
  212. .. _PHPUnit: http://www.phpunit.de/manual/current/en/
  213. .. _Alchemy: http://alchemy.fr/
  214. .. _Phraseanet: https://github.com/alchemy-fr/Phraseanet
  215. .. _MIT License: http://opensource.org/licenses/MIT