ZipArchiver.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * @package Grav\Common\Filesystem
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Filesystem;
  9. class ZipArchiver extends Archiver
  10. {
  11. public function extract($destination, callable $status = null)
  12. {
  13. $zip = new \ZipArchive();
  14. $archive = $zip->open($this->archive_file);
  15. if ($archive === true) {
  16. Folder::create($destination);
  17. if (!$zip->extractTo($destination)) {
  18. throw new \RuntimeException('ZipArchiver: ZIP failed to extract ' . $this->archive_file . ' to ' . $destination);
  19. }
  20. $zip->close();
  21. return $this;
  22. }
  23. throw new \RuntimeException('ZipArchiver: Failed to open ' . $this->archive_file);
  24. }
  25. public function compress($source, callable $status = null)
  26. {
  27. if (!extension_loaded('zip')) {
  28. throw new \InvalidArgumentException('ZipArchiver: Zip PHP module not installed...');
  29. }
  30. if (!file_exists($source)) {
  31. throw new \InvalidArgumentException('ZipArchiver: ' . $source . ' cannot be found...');
  32. }
  33. $zip = new \ZipArchive();
  34. if (!$zip->open($this->archive_file, \ZipArchive::CREATE)) {
  35. throw new \InvalidArgumentException('ZipArchiver:' . $this->archive_file . ' cannot be created...');
  36. }
  37. // Get real path for our folder
  38. $rootPath = realpath($source);
  39. $files = $this->getArchiveFiles($rootPath);
  40. $status && $status([
  41. 'type' => 'count',
  42. 'steps' => iterator_count($files),
  43. ]);
  44. foreach ($files as $file) {
  45. $filePath = $file->getPathname();
  46. $relativePath = ltrim(substr($filePath, strlen($rootPath)), '/');
  47. if ($file->isDir()) {
  48. $zip->addEmptyDir($relativePath);
  49. } else {
  50. $zip->addFile($filePath, $relativePath);
  51. }
  52. $status && $status([
  53. 'type' => 'progress',
  54. ]);
  55. }
  56. $status && $status([
  57. 'type' => 'message',
  58. 'message' => 'Compressing...'
  59. ]);
  60. $zip->close();
  61. return $this;
  62. }
  63. public function addEmptyFolders($folders, callable $status = null)
  64. {
  65. if (!extension_loaded('zip')) {
  66. throw new \InvalidArgumentException('ZipArchiver: Zip PHP module not installed...');
  67. }
  68. $zip = new \ZipArchive();
  69. if (!$zip->open($this->archive_file)) {
  70. throw new \InvalidArgumentException('ZipArchiver: ' . $this->archive_file . ' cannot be opened...');
  71. }
  72. $status && $status([
  73. 'type' => 'message',
  74. 'message' => 'Adding empty folders...'
  75. ]);
  76. foreach($folders as $folder) {
  77. $zip->addEmptyDir($folder);
  78. $status && $status([
  79. 'type' => 'progress',
  80. ]);
  81. }
  82. $zip->close();
  83. return $this;
  84. }
  85. }