ZipHelper.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 中闽 < 1464674022@qq.com >
  5. * Date: 2020/7/23
  6. * Time: 17:56
  7. */
  8. namespace file;
  9. use think\Exception;
  10. /**
  11. * Zip 文件包工具
  12. *
  13. * @author wengxianhu
  14. * @date 2013-08-05
  15. */
  16. class ZipHelper
  17. {
  18. protected $zip;
  19. protected $root;
  20. protected $ignored_names;
  21. public function __construct()
  22. {
  23. $this->zip = new \ZipArchive();//使用此扩展,linux需开启zlib,windows需取消php_zip.dll前的注释
  24. }
  25. /**
  26. * 解压zip文件到指定文件夹
  27. *
  28. * @access public
  29. * @param string $zipfile 压缩文件路径
  30. * @param string $path 压缩包解压到的目标路径
  31. * @return bool 解压成功返回 true 否则返回 false
  32. */
  33. public function unzip($zipfile, $path)
  34. {
  35. if ($this->zip->open($zipfile) === true) {
  36. $file_tmp = @fopen($zipfile, "rb");
  37. $bin = fread($file_tmp, 15); //只读15字节 各个不同文件类型,头信息不一样。
  38. fclose($file_tmp);
  39. /* 只针对zip的压缩包进行处理 */
  40. if (true === $this->getTypeList($bin)) {
  41. $result = $this->zip->extractTo($path);
  42. $this->zip->close();
  43. return $result;
  44. } else {
  45. return false;
  46. }
  47. }
  48. return false;
  49. }
  50. //解压,并处理中文
  51. public function unzip_gbk($zipfile, $path)
  52. {
  53. if ($this->zip->open($zipfile) === true) {
  54. $fileNum = $this->zip->numFiles;
  55. for ($i = 0; $i < $fileNum; $i++) {
  56. $statInfo = $this->zip->statIndex($i, \ZipArchive::FL_ENC_RAW);
  57. $encode = mb_detect_encoding($statInfo['name'], array("ASCII", 'UTF-8', "GB2312", "GBK", 'BIG5'));
  58. $this->zip->renameIndex($i, mb_convert_encoding($statInfo['name'], 'utf-8', $encode));
  59. }
  60. $this->zip->close();
  61. $this->zip->open($zipfile);
  62. $file_tmp = @fopen($zipfile, "rb");
  63. $bin = fread($file_tmp, 15); //只读15字节 各个不同文件类型,头信息不一样。
  64. fclose($file_tmp);
  65. /* 只针对zip的压缩包进行处理 */
  66. if (true === $this->getTypeList($bin)) {
  67. $result = $this->zip->extractTo($path);
  68. $this->zip->close();
  69. return $result;
  70. } else {
  71. return false;
  72. }
  73. }
  74. return false;
  75. }
  76. /**
  77. * 创建压缩文件
  78. *
  79. * @param string $zipfile 将要生成的压缩文件路径
  80. * @param string $folder 将要被压缩的文件夹路径
  81. * @param array $ignored 要忽略的文件列表
  82. * @return bool 压缩包生成成功返回true 否则返回 false
  83. * @throws Exception
  84. */
  85. public function zip($zipfile, $folder, $ignored = null)
  86. {
  87. $this->ignored_names = is_array($ignored) ? $ignored : $ignored ? array($ignored) : array();
  88. if ($this->zip->open($zipfile, \ZipArchive::CREATE) !== true) {
  89. throw new Exception("cannot open <$zipfile>\n");
  90. }
  91. if (is_file($folder)) {
  92. $this->zip->addFile($folder, basename($folder));
  93. } elseif (is_dir($folder)) {
  94. $folder = substr($folder, -1) == '/' ? substr($folder, 0, strlen($folder) - 1) : $folder;
  95. if (strstr($folder, '/')) {
  96. $this->root = substr($folder, 0, strrpos($folder, '/') + 1);
  97. $folder = substr($folder, strrpos($folder, '/') + 1);
  98. }
  99. $this->createZip($folder);
  100. }
  101. return $this->zip->close();
  102. }
  103. /**
  104. * 递归添加文件到压缩包
  105. *
  106. * @access private
  107. * @param string $folder 添加到压缩包的文件夹路径
  108. * @param string $parent 添加到压缩包的文件夹上级路径
  109. * @return void
  110. */
  111. private function createZip($folder, $parent = null)
  112. {
  113. $full_path = $this->root . $parent . $folder;
  114. $zip_path = $parent . $folder;
  115. $this->zip->addEmptyDir($zip_path);
  116. $dir = new \DirectoryIterator($full_path);
  117. foreach ($dir as $file) {
  118. if (!$file->isDot()) {
  119. $filename = $file->getFilename();
  120. if (!in_array($filename, $this->ignored_names)) {
  121. if ($file->isDir()) {
  122. $this->createZip($filename, $zip_path . '/');
  123. } else {
  124. $this->zip->addFile($full_path . '/' . $filename, $zip_path . '/' . $filename);
  125. }
  126. }
  127. }
  128. }
  129. }
  130. /**
  131. * 读取压缩包文件与目录列表
  132. *
  133. * @access public
  134. * @param string $zipfile 压缩包文件
  135. * @return array 文件与目录列表
  136. */
  137. public function fileList($zipfile)
  138. {
  139. $file_dir_list = array();
  140. $file_list = array();
  141. if ($this->zip->open($zipfile) == true) {
  142. for ($i = 0; $i < $this->zip->numFiles; $i++) {
  143. $numfiles = $this->zip->getNameIndex($i);
  144. if (preg_match('/\/$/i', $numfiles)) {
  145. $file_dir_list[] = $numfiles;
  146. } else {
  147. $file_list[] = $numfiles;
  148. }
  149. }
  150. }
  151. return array('files' => $file_list, 'dirs' => $file_dir_list);
  152. }
  153. /**
  154. * 得到文件头与文件类型映射表
  155. *
  156. * @author wengxianhu
  157. * @date 2013-08-10
  158. * @param $bin string 文件的二进制前一段字符
  159. * @return boolean
  160. */
  161. private function getTypeList($bin)
  162. {
  163. $array = array(
  164. array("504B0304", "zip")
  165. );
  166. foreach ($array as $v) {
  167. $blen = strlen(pack("H*", $v[0])); //得到文件头标记字节数
  168. $tbin = substr($bin, 0, intval($blen)); ///需要比较文件头长度
  169. $temp1 = unpack("H*", $tbin);
  170. $temp2 = array_shift($temp1);
  171. if (strtolower($v[0]) == strtolower($temp2)) {
  172. return true;
  173. }
  174. }
  175. return false;
  176. }
  177. }