123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- namespace addons\yexam\library\makezip;
- class Makezip{
- /**
- * description:主方法:生成压缩包
- * @author: MY
- * @param $dir_path 想要压缩的目录:如 './demo/'
- * @param $zipName 压缩后的文件名:如 './folder/demo.zip'
- * @return string
- */
- public static function zip($dir_path, $zipName)
- {
- $relationArr = array(
- $dir_path => array(
- 'originName' => $dir_path,
- 'is_dir' => true,
- 'children' => array()
- )
- );
- self::modifiyFileName($dir_path, $relationArr[$dir_path]['children']);
- $key = array_keys($relationArr);
- $val = array_values($relationArr);
- $zip = new \ZipArchive();
- //ZIPARCHIVE::CREATE没有即是创建
- $zip->open($zipName, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
- self::zipDir($key[0], '', $zip, $val[0]['children']);
- $zip->close();
- self::restoreFileName($key[0], $val[0]['children']);
- return true;
- }
- public static function zipDir($real_path, $zip_path, &$zip, $relationArr)
- {
- $sub_zip_path = empty($zip_path) ? '' : $zip_path . '\\';
- if (is_dir($real_path)) {
- foreach ($relationArr as $k => $v) {
- if ($v['is_dir']) { //是文件夹
- $zip->addEmptyDir($sub_zip_path . $v['originName']);
- self::zipDir($real_path . '\\' . $k, $sub_zip_path . $v['originName'], $zip, $v['children']);
- } else { //不是文件夹
- $zip->addFile($real_path . $k, $sub_zip_path . $k);
- $zip->deleteName($sub_zip_path . $v['originName']);
- $zip->renameName($sub_zip_path . $k, $sub_zip_path . $v['originName']);
- }
- }
- }
- }
- public static function modifiyFileName($path, &$relationArr)
- {
- if (!is_dir($path) || !is_array($relationArr)) {
- return false;
- }
- if ($dh = opendir($path)) {
- $count = 0;
- while (($file = readdir($dh)) !== false) {
- if(in_array($file,array('.', '..', null))) continue; //无效文件,重来
- if (is_dir($path . '/' . $file)) {
- $newName = md5(rand(0, 99999) . rand(0, 99999) . rand(0, 99999) . microtime() . 'dir' . $count);
- $relationArr[$newName] = array(
- 'originName' => iconv('GBK', 'UTF-8', $file),
- 'is_dir' => true,
- 'children' => array()
- );
- rename($path . '/' . $file, $path . '/' . $newName);
- self::modifiyFileName($path . '/' . $newName, $relationArr[$newName]['children']);
- $count++;
- } else {
- $extension = strchr($file, '.');
- $newName = md5(rand(0, 99999) . rand(0, 99999) . rand(0, 99999) . microtime() . 'file' . $count);
- $relationArr[$newName . $extension] = array(
- 'originName' => $file,
- 'is_dir' => false,
- 'children' => array()
- );
- rename($path . '/' . $file, $path . '/' . $newName . $extension);
- $count++;
- }
- }
- }
- }
- public static function restoreFileName($path, $relationArr)
- {
- foreach ($relationArr as $k => $v) {
- if (!empty($v['children'])) {
- self::restoreFileName($path . '\\' . $k, $v['children']);
- rename($path . '/' . $k, iconv('UTF-8', 'GBK', $path . '/' . $v['originName']));
- } else {
- rename($path . '/' . $k, iconv('UTF-8', 'GBK', $path . '/' . $v['originName']));
- }
- }
- }
- }
|