ZipAdapter.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. /*
  3. * This file is part of Zippy.
  4. *
  5. * (c) Alchemy <info@alchemy.fr>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Alchemy\Zippy\Adapter;
  11. use Alchemy\Zippy\Archive\Archive;
  12. use Alchemy\Zippy\Archive\Member;
  13. use Alchemy\Zippy\Adapter\Resource\ResourceInterface;
  14. use Alchemy\Zippy\Exception\RuntimeException;
  15. use Alchemy\Zippy\Exception\NotSupportedException;
  16. use Alchemy\Zippy\Exception\InvalidArgumentException;
  17. use Alchemy\Zippy\Resource\Resource;
  18. use Alchemy\Zippy\Resource\ResourceManager;
  19. use Alchemy\Zippy\Adapter\VersionProbe\ZipVersionProbe;
  20. use Alchemy\Zippy\Parser\ParserInterface;
  21. use Alchemy\Zippy\ProcessBuilder\ProcessBuilderFactoryInterface;
  22. use Symfony\Component\Process\Exception\ExceptionInterface as ProcessException;
  23. /**
  24. * ZipAdapter allows you to create and extract files from archives using Zip
  25. *
  26. * @see http://www.gnu.org/software/tar/manual/tar.html
  27. */
  28. class ZipAdapter extends AbstractBinaryAdapter
  29. {
  30. public function __construct(ParserInterface $parser, ResourceManager $manager, ProcessBuilderFactoryInterface $inflator, ProcessBuilderFactoryInterface $deflator)
  31. {
  32. parent::__construct($parser, $manager, $inflator, $deflator);
  33. $this->probe = new ZipVersionProbe($inflator, $deflator);
  34. }
  35. /**
  36. * @inheritdoc
  37. */
  38. protected function doCreate($path, $files, $recursive)
  39. {
  40. $files = (array) $files;
  41. $builder = $this
  42. ->inflator
  43. ->create();
  44. if (0 === count($files)) {
  45. throw new NotSupportedException('Can not create empty zip archive');
  46. }
  47. if ($recursive) {
  48. $builder->add('-r');
  49. }
  50. $builder->add($path);
  51. $collection = $this->manager->handle(getcwd(), $files);
  52. $builder->setWorkingDirectory($collection->getContext());
  53. $collection->forAll(function ($i, Resource $resource) use ($builder) {
  54. return $builder->add($resource->getTarget());
  55. });
  56. $process = $builder->getProcess();
  57. try {
  58. $process->run();
  59. } catch (ProcessException $e) {
  60. $this->manager->cleanup($collection);
  61. throw $e;
  62. }
  63. $this->manager->cleanup($collection);
  64. if (!$process->isSuccessful()) {
  65. throw new RuntimeException(sprintf(
  66. 'Unable to execute the following command %s {output: %s}',
  67. $process->getCommandLine(),
  68. $process->getErrorOutput()
  69. ));
  70. }
  71. return new Archive($this->createResource($path), $this, $this->manager);
  72. }
  73. /**
  74. * @inheritdoc
  75. */
  76. protected function doListMembers(ResourceInterface $resource)
  77. {
  78. $process = $this
  79. ->deflator
  80. ->create()
  81. ->add('-l')
  82. ->add($resource->getResource())
  83. ->getProcess();
  84. $process->run();
  85. if (!$process->isSuccessful()) {
  86. throw new RuntimeException(sprintf(
  87. 'Unable to execute the following command %s {output: %s}',
  88. $process->getCommandLine(),
  89. $process->getErrorOutput()
  90. ));
  91. }
  92. $members = array();
  93. foreach ($this->parser->parseFileListing($process->getOutput() ?: '') as $member) {
  94. $members[] = new Member(
  95. $resource,
  96. $this,
  97. $member['location'],
  98. $member['size'],
  99. $member['mtime'],
  100. $member['is_dir']
  101. );
  102. }
  103. return $members;
  104. }
  105. /**
  106. * @inheritdoc
  107. */
  108. protected function doAdd(ResourceInterface $resource, $files, $recursive)
  109. {
  110. $files = (array) $files;
  111. $builder = $this
  112. ->inflator
  113. ->create();
  114. if ($recursive) {
  115. $builder->add('-r');
  116. }
  117. $builder
  118. ->add('-u')
  119. ->add($resource->getResource());
  120. $collection = $this->manager->handle(getcwd(), $files);
  121. $builder->setWorkingDirectory($collection->getContext());
  122. $collection->forAll(function ($i, Resource $resource) use ($builder) {
  123. return $builder->add($resource->getTarget());
  124. });
  125. $process = $builder->getProcess();
  126. try {
  127. $process->run();
  128. } catch (ProcessException $e) {
  129. $this->manager->cleanup($collection);
  130. throw $e;
  131. }
  132. $this->manager->cleanup($collection);
  133. if (!$process->isSuccessful()) {
  134. throw new RuntimeException(sprintf(
  135. 'Unable to execute the following command %s {output: %s}',
  136. $process->getCommandLine(),
  137. $process->getErrorOutput()
  138. ));
  139. }
  140. }
  141. /**
  142. * @inheritdoc
  143. */
  144. protected function doGetDeflatorVersion()
  145. {
  146. $process = $this
  147. ->deflator
  148. ->create()
  149. ->add('-h')
  150. ->getProcess();
  151. $process->run();
  152. if (!$process->isSuccessful()) {
  153. throw new RuntimeException(sprintf(
  154. 'Unable to execute the following command %s {output: %s}',
  155. $process->getCommandLine(),
  156. $process->getErrorOutput()
  157. ));
  158. }
  159. return $this->parser->parseDeflatorVersion($process->getOutput() ?: '');
  160. }
  161. /**
  162. * @inheritdoc
  163. */
  164. protected function doGetInflatorVersion()
  165. {
  166. $process = $this
  167. ->inflator
  168. ->create()
  169. ->add('-h')
  170. ->getProcess();
  171. $process->run();
  172. if (!$process->isSuccessful()) {
  173. throw new RuntimeException(sprintf(
  174. 'Unable to execute the following command %s {output: %s}',
  175. $process->getCommandLine(),
  176. $process->getErrorOutput()
  177. ));
  178. }
  179. return $this->parser->parseInflatorVersion($process->getOutput() ?: '');
  180. }
  181. /**
  182. * @inheritdoc
  183. */
  184. protected function doRemove(ResourceInterface $resource, $files)
  185. {
  186. $files = (array) $files;
  187. $builder = $this
  188. ->inflator
  189. ->create();
  190. $builder
  191. ->add('-d')
  192. ->add($resource->getResource());
  193. if (!$this->addBuilderFileArgument($files, $builder)) {
  194. throw new InvalidArgumentException('Invalid files');
  195. }
  196. $process = $builder->getProcess();
  197. $process->run();
  198. if (!$process->isSuccessful()) {
  199. throw new RuntimeException(sprintf(
  200. 'Unable to execute the following command %s {output: %s}',
  201. $process->getCommandLine(),
  202. $process->getErrorOutput()
  203. ));
  204. }
  205. return $files;
  206. }
  207. /**
  208. * @inheritdoc
  209. */
  210. public static function getName()
  211. {
  212. return 'zip';
  213. }
  214. /**
  215. * @inheritdoc
  216. */
  217. public static function getDefaultDeflatorBinaryName()
  218. {
  219. return array('unzip');
  220. }
  221. /**
  222. * @inheritdoc
  223. */
  224. public static function getDefaultInflatorBinaryName()
  225. {
  226. return array('zip');
  227. }
  228. /**
  229. * @inheritdoc
  230. */
  231. protected function doExtract(ResourceInterface $resource, $to)
  232. {
  233. if (null !== $to && !is_dir($to)) {
  234. throw new InvalidArgumentException(sprintf("%s is not a directory", $to));
  235. }
  236. $builder = $this
  237. ->deflator
  238. ->create();
  239. $builder
  240. ->add('-o')
  241. ->add($resource->getResource());
  242. if (null !== $to) {
  243. $builder
  244. ->add('-d')
  245. ->add($to);
  246. }
  247. $process = $builder->getProcess();
  248. $process->run();
  249. if (!$process->isSuccessful()) {
  250. throw new RuntimeException(sprintf(
  251. 'Unable to execute the following command %s {output: %s}',
  252. $process->getCommandLine(),
  253. $process->getErrorOutput()
  254. ));
  255. }
  256. return new \SplFileInfo($to ?: $resource->getResource());
  257. }
  258. /**
  259. * @inheritdoc
  260. */
  261. protected function doExtractMembers(ResourceInterface $resource, $members, $to)
  262. {
  263. if (null !== $to && !is_dir($to)) {
  264. throw new InvalidArgumentException(sprintf("%s is not a directory", $to));
  265. }
  266. $members = (array) $members;
  267. $builder = $this
  268. ->deflator
  269. ->create();
  270. $builder
  271. ->add($resource->getResource());
  272. if (null !== $to) {
  273. $builder
  274. ->add('-d')
  275. ->add($to);
  276. }
  277. if (!$this->addBuilderFileArgument($members, $builder)) {
  278. throw new InvalidArgumentException('Invalid files');
  279. }
  280. $process = $builder->getProcess();
  281. $process->run();
  282. if (!$process->isSuccessful()) {
  283. throw new RuntimeException(sprintf(
  284. 'Unable to execute the following command %s {output: %s}',
  285. $process->getCommandLine(),
  286. $process->getErrorOutput()
  287. ));
  288. }
  289. return $members;
  290. }
  291. }