php原生zip测试

//简单测试1
 public function zipTest(){
        $zip = new \ZipArchive();
        $res = $zip->open('static/download/zip/test.zip',\ZipArchive::CREATE);
        if ($res === TRUE) {
            echo 'ok';
            $zip->addFromString('test.txt', 'file content goes here');
            //解压缩到test文件夹
            //$zip->extractTo('test');
            $zip->close();
        } else {
            echo 'failed, code:' . $res;
        }
    }

    //处理类
class FilePackUtil
{


    /**
     * 压缩成ZIP文件
     * @param array $file 文件数组
     * $file = array('E:/Resources/download.kekedj.com/mp3/XX/20180611/XXX/XXX.mp3', 'E:/Resources/download.kekedj.com/mp3/XX/20180611/XXX/XXX.mp3');
     * @param $name 存放名字.zip
     * @param $output 存放路径
     * @return bool|string
     */
    public static function zip($file = array(), $name, $output)
    {
        if(empty($file)) {
            return "打包文件为空";
        }
        if(empty($name)) {
            return "请选择输出文件名";
        }
        if(empty($output)) {
            return "请选择输出目录";
        }
        foreach ($file as $key => $value) {
            $file [$key] = iconv("UTF-8", "gb2312//IGNORE", $value); //中文名转换
        }
        if (! is_dir ( $output )) {
            mkdir ( $output, 0777 );
        }
        $filename = iconv("UTF-8", "gb2312//IGNORE", $output . $name); // 中文名转换 最终生成的文件名以及服务器保存的路径
        if (!file_exists($filename)) {
            //重新生成文件
            $zip = new ZipArchive (); //使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释
            if ($zip->open($filename, ZIPARCHIVE::CREATE) !== TRUE) {
                return '无法打开文件,或者文件创建失败。请检查路径、权限以及环境变量的配置';
            }
            foreach ($file as $val) {
                if (file_exists($val)) {
                    $fileInfoArr = self::pathInfo($val);
                    $zip->addFromString($fileInfoArr ['basename'], file_get_contents($val)); //中文使用这个
                }
            }
            $zip->close(); //关闭
        }
        if (!file_exists($filename)) {
            return ("无法找到文件"); //即使创建,仍有可能失败。。。。
        }
        return true;
    }


    public static function remove($file)
    {
        if(!$file) return false;
        $file = iconv("UTF-8", "gb2312//IGNORE", $file); // 中文名转换
        @unlink($file);
        exec("rm -rf " . $file);
        return true;
    }

    /**
     * 读取文件信息
     * @author ZhiyuanLi < [email protected] >
     * @param $filePath
     * @return array
     */
    private function pathInfo($filePath)
    {
        $path_parts = array();
        $path_parts ['dirname'] = rtrim(substr($filePath, 0, strrpos($filePath, '/')), "/") . "/";
        $path_parts ['basename'] = ltrim(substr($filePath, strrpos($filePath, '/')), "/");
        $path_parts ['extension'] = substr(strrchr($filePath, '.'), 1);
        $path_parts ['filename'] = ltrim(substr($path_parts ['basename'], 0, strrpos($path_parts ['basename'], '.')), "/");
        return $path_parts;
    }

猜你喜欢

转载自blog.csdn.net/qq_40393605/article/details/81238212