参考了上传插件后要解压的功能
依赖
use GuzzleHttp\Client;use GuzzleHttp\Exception\TransferException;
use PhpZip\Exception\ZipException;
use PhpZip\ZipFile;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Symfony\Component\VarExporter\VarExporter;
具体方法
public function unzip($filepath,$extractTo) {
$result = false;
// 打开插件压缩包
$zip = new ZipFile();
try {
$zip->openFile($filepath);
} catch (ZipException $e) {
$zip->close();
throw new Exception('Unable to open the zip file');
}
$dir = $extractTo;//self::getAddonDir($name);
if(is_dir($dir)){//如果存在先删除
$this->delDirAndFile($dir,true);
}
sleep(2);//要加上这个判断,否则出现过创建不成功
if(!is_dir($dir)){//如果存在先删除。
@mkdir($dir, 0755);//再创建
}
// 解压插件压缩包
try {
$zip->extractTo($dir);
$result = true;
} catch (ZipException $e) {
throw new Exception('Unable to extract the file');
} finally {
$zip->close();
}
return $result;
}