侧边栏壁纸
  • 累计撰写 100 篇文章
  • 累计创建 55 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录
PHP

PHP 解压ZIP

Malson
2022-06-25 / 0 评论 / 0 点赞 / 147 阅读 / 205 字
<?php
class unZip
{

    private $zipFileName;

    public function findZipFile()
    {
        $_path = opendir(__DIR__);

        while ($_fileNameInPath = readdir($_path)) {
            $_fileInfo = pathinfo($_fileNameInPath);
            if ('zip' === $_fileInfo['extension']) {
                $this->zipFileName = $_fileInfo['basename'];
                closedir($_path);
                return '0000';
            }
        }
        closedir($_path);
        return '1001';
    }

    public function doIt()
    {
        $_zip = new ZipArchive();
        if (true !== $_zip->open($this->zipFileName)) {
            return '2001';
        }

        if (true !== $_zip->extractTo('./')) {
            $_zip->close();
            return '2002';
        }

        $_zip->close();

        return '0000';
    }
}

function returnError($code)
{
    switch ($code) {
        case '0000':
            $_msg = '解压完成';
            break;
        case '1001':
            $_msg = '服务器中不存在zip文件';
            break;
        case '2001':
            $_msg = '无法打开指定的zip文件';
            break;
        case '2002':
            $_msg = '压缩文件解压失败';
            break;
        default:
            $_msg = '未知错误';
    }

    exit(json_encode([
        'code' => $code,
        'msg' => $_msg
    ],JSON_UNESCAPED_UNICODE));
}

$_unZip = new unZip();

if ('0000' !== $_result = $_unZip->findZipFile()) {
    returnError($_result);
}

if ('0000' !== $_result = $_unZip->doIt()) {
    returnError($_result);
}

returnError('0000');
0
博主关闭了所有页面的评论