YZip.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: dingran
  5. * Date: 2018/12/4
  6. * Time: 上午1:51
  7. */
  8. namespace app\common\services;
  9. class YZip
  10. {
  11. public function __construct() {}
  12. private function _rglobRead($source, &$array = array()) {
  13. if (!$source || trim($source) == "") {
  14. $source = ".";
  15. }
  16. foreach ((array) glob($source . "/*/") as $key => $value) {
  17. $this->_rglobRead(str_replace("//", "/", $value), $array);
  18. }
  19. foreach ((array) glob($source . "*.*") as $key => $value) {
  20. $array[] = str_replace("//", "/", $value);
  21. }
  22. }
  23. private function _zip($array, $part, $destination) {
  24. $zip = new \ZipArchive;
  25. @mkdir($destination, 0777, true);
  26. if ($zip->open(str_replace("//", "/", "{$destination}/partz{$part}.zip"), ZipArchive::CREATE)) {
  27. foreach ((array) $array as $key => $value) {
  28. $zip->addFile($value, str_replace(array("../", "./"), NULL, $value));
  29. }
  30. $zip->close();
  31. }
  32. }
  33. public function zip($limit = 500, $source = NULL, $destination = "./") {
  34. if (!$destination || trim($destination) == "") {
  35. $destination = "./";
  36. }
  37. $this->_rglobRead($source, $input);
  38. $maxinput = count($input);
  39. $splitinto = (($maxinput / $limit) > round($maxinput / $limit, 0)) ? round($maxinput / $limit, 0) + 1 : round($maxinput / $limit, 0);
  40. for($i = 0; $i < $splitinto; $i ++) {
  41. $this->_zip(array_slice($input, ($i * $limit), $limit, true), $i, $destination);
  42. }
  43. unset($input);
  44. return;
  45. }
  46. public function unzip($source, $destination) {
  47. @mkdir($destination, 0777, true);
  48. foreach ((array) glob($source . "/*.zip") as $key => $value) {
  49. $zip = new \ZipArchive;
  50. if ($zip->open(str_replace("//", "/", $value)) === true) {
  51. $zip->extractTo($destination);
  52. $zip->close();
  53. }
  54. }
  55. }
  56. public function __destruct() {}
  57. }