TypechoJoeTheme

Dcr163的博客

统计

PHP打包zip压缩包

2021-06-03
/
0 评论
/
1,226 阅读
/
正在检测是否收录...
06/03

PHP打包zip压缩包

有些虚拟空间没有控制面板在线打包的功能,,这里使用PHP打包某个目录为zip,直接在浏览器执行即可打包,直接上代码

<?php
/**
 * php zip压缩文件夹
 * Author: Dcr163
 * Date: 2021/6/3
 * Time: 14:33
 */
class zip{
    public $files = []; //添加到zip中的文件
    public $rootDir = ''; //当前工作目录
    public $openExclude = ''; //是否开启过滤指定后缀
    /**
     * 文件夹打包成zip
     * @param $dist  需要打包的目录
     * @param $zipFile 打包的文件名
     * @throws ErrorException
     */
    public function index($dist,$zipFile,$openExclude=false){
        if( !class_exists('ZipArchive') ) throw new ErrorException('ZipArchive 扩展未启用');
        $stime = $this->formatMicotime(microtime());
        $this->rootDir = str_replace('\\','/',getcwd());
        $this->openExclude = $openExclude;
        $zipDir = $this->rootDir .'/'.$dist;
        $this->listDirs($zipDir);
        if( empty($this->files) ) throw new ErrorException('打包失败,需要打包的文件为空');
        //实例化zip类
        $zip = new ZipArchive();
        //打开压缩包 新建或写
        $zipRes = $zip->open($zipFile,ZipArchive::CREATE | ZipArchive::OVERWRITE );
        if( $zipRes !== true ) throw new ErrorException('zip文件打开失败,错误代码:'.$this->zipErrorMsg($zipRes));
        $addRes = [];//添加压缩文件状态
        foreach ($this->files as $v){
            //把文件添加到压缩包里
            $res = $zip->addFile($v,str_replace($this->rootDir,'',$v));
            $addRes[]=$res;
        }
        //关闭资源
        $zip->close();
        $etime = $this->formatMicotime(microtime());
        if( count($addRes) ) {
            echo '打包成功,成功打包:'.count($addRes).' 个文件;运行时间:'.round($etime-$stime,2).' S';
        } else {
            echo '打包失败';
        }
    }
    /**
     * 遍历所有文件
     * @param $dir
     */
    public function listDirs($dir){
        $dirRes = opendir($dir);
        while( false !== ($file = readdir($dirRes)) ){
            if( in_array($file,['.','..'])) continue;
            $realFile = $dir.$file;
            //如果是文件夹,则递归 / 这个不能少
            if( is_dir($realFile) ){
                $this->listDirs($realFile.'/');
            } else {
                if( $this->openExclude ){
                    //排除压缩文件不添加
                    $excludeArr = ['zip','tar','gz','ara','7z'];
                    //分隔文件,用于查询文件后缀
                    $tmpArr = (explode('.',$realFile));
                    if( count($tmpArr) >1 ){
                        //获取文件的后缀
                        $ext = strtolower($tmpArr[count($tmpArr)-1]);
                        //排除指定后缀的文件
                        if( in_array($ext,$excludeArr) ) continue;
                    }
                }
                //添加到文件列表中
                $this->files[] = $realFile;
            }
        }
    }
    /**
     * 错误提示
     * @param $code
     * @return mixed|string
     */
    protected function zipErrorMsg($code){
        $errorCode = array(
            4=>'Seek error',
            5=>' Read error',
            9=>'No such file.',
            10=>'File already exists.',
            11=>'Can\'t open file.',
            14=>'Malloc failure.',
            18=>'Invalid argument.',
            19=>'Not a zip archive.',
            21=>'Zip archive inconsistent',
        );
        return array_key_exists($code,$errorCode) ? $errorCode[$code] : '未知错误';
    }

    /**
     * 格式化 时间戳和微秒数
     * @param $microtime
     * @return mixed
     */
    protected function formatMicotime($microtime){
        list($usec,$sec) = explode(' ',$microtime);
        return  $sec+$usec;
    }
}

try{
    //需要打包zip的目录
    $dist = './';
    //打包后的zip文件名称
    $zipFile = 'web.zip';
    //实例化类
    $zip = new zip();
    //开始打包zip
    $zip->index($dist ,$zipFile);
}catch (Exception $e){
     exit($e->getMessage());
}

使用方法

把代码复制,保存到一个文件里,例如:/zip.php,最后在浏览器运行文件即可。

朗读
赞(1)
版权属于:

Dcr163的博客

本文链接:

https://dcr163.cn/478.html(转载时请注明本文出处及文章链接)

评论 (0)