GzipDownloader.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Downloader;
  12. use Composer\Config;
  13. use Composer\Cache;
  14. use Composer\EventDispatcher\EventDispatcher;
  15. use Composer\Package\PackageInterface;
  16. use Composer\Util\ProcessExecutor;
  17. use Composer\IO\IOInterface;
  18. /**
  19. * GZip archive downloader.
  20. *
  21. * @author Pavel Puchkin <i@neoascetic.me>
  22. */
  23. class GzipDownloader extends ArchiveDownloader
  24. {
  25. protected $process;
  26. public function __construct(IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null, Cache $cache = null, ProcessExecutor $process = null)
  27. {
  28. $this->process = $process ?: new ProcessExecutor($io);
  29. parent::__construct($io, $config, $eventDispatcher, $cache);
  30. }
  31. protected function extract($file, $path)
  32. {
  33. $targetFilepath = $path . DIRECTORY_SEPARATOR . basename(substr($file, 0, -3));
  34. // Try to use gunzip on *nix
  35. if (!defined('PHP_WINDOWS_VERSION_BUILD')) {
  36. $command = 'gzip -cd ' . ProcessExecutor::escape($file) . ' > ' . ProcessExecutor::escape($targetFilepath);
  37. if (0 === $this->process->execute($command, $ignoredOutput)) {
  38. return;
  39. }
  40. $processError = 'Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput();
  41. throw new \RuntimeException($processError);
  42. }
  43. // Windows version of PHP has built-in support of gzip functions
  44. $archiveFile = gzopen($file, 'rb');
  45. $targetFile = fopen($targetFilepath, 'wb');
  46. while ($string = gzread($archiveFile, 4096)) {
  47. fwrite($targetFile, $string, strlen($string));
  48. }
  49. gzclose($archiveFile);
  50. fclose($targetFile);
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. protected function getFileName(PackageInterface $package, $path)
  56. {
  57. return $path.'/'.pathinfo(parse_url($package->getDistUrl(), PHP_URL_PATH), PATHINFO_BASENAME);
  58. }
  59. }