<?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');
版权归属:
Malson
许可协议:
MIT