class_zipFolder.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /** This file is part of KCFinder project. The class are taken from
  3. * http://www.php.net/manual/en/function.ziparchive-addemptydir.php
  4. *
  5. * @desc Directory to ZIP file archivator
  6. * @package KCFinder
  7. * @version 3.12
  8. * @author Pavel Tzonkov <sunhater@sunhater.com>
  9. * @copyright 2010-2014 KCFinder Project
  10. * @license http://opensource.org/licenses/GPL-3.0 GPLv3
  11. * @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
  12. * @link http://kcfinder.sunhater.com
  13. */
  14. namespace kcfinder;
  15. class zipFolder {
  16. protected $zip;
  17. protected $root;
  18. protected $ignored;
  19. function __construct($file, $folder, $ignored=null) {
  20. $this->zip = new \ZipArchive();
  21. $this->ignored = is_array($ignored)
  22. ? $ignored
  23. : ($ignored ? array($ignored) : array());
  24. if ($this->zip->open($file, \ZipArchive::CREATE) !== TRUE)
  25. throw new \Exception("cannot open <$file>\n");
  26. $folder = rtrim($folder, '/');
  27. if (strstr($folder, '/')) {
  28. $this->root = substr($folder, 0, strrpos($folder, '/') + 1);
  29. $folder = substr($folder, strrpos($folder, '/') + 1);
  30. }
  31. $this->zip($folder);
  32. $this->zip->close();
  33. }
  34. function zip($folder, $parent=null) {
  35. $full_path = "{$this->root}$parent$folder";
  36. $zip_path = "$parent$folder";
  37. $this->zip->addEmptyDir($zip_path);
  38. $dir = new \DirectoryIterator($full_path);
  39. foreach ($dir as $file)
  40. if (!$file->isDot()) {
  41. $filename = $file->getFilename();
  42. if (!in_array($filename, $this->ignored)) {
  43. if ($file->isDir())
  44. $this->zip($filename, "$zip_path/");
  45. else
  46. $this->zip->addFile("$full_path/$filename", "$zip_path/$filename");
  47. }
  48. }
  49. }
  50. }
  51. ?>