PHP压缩、解压缩ZIP 无 ?php/** * @version 1.0 * @date 2014-08-11 * @author 十七号 xialeistudio@gmail.com * @license MIT * 压缩、解压缩类 */class Zip{/** * 打包 * @param $path * @param $save */public static function archive($path, $save){$zip
PHP压缩、解压缩ZIP
<?php
/**
* @version 1.0
* @date 2014-08-11
* @author 十七号 <xialeistudio@gmail.com>
* @license MIT
* 压缩、解压缩类
*/
class Zip
{
/**
* 打包
* @param $path
* @param $save
*/
public static function archive($path, $save)
{
$zip = new ZipArchive();
if ($zip->open($save, ZipArchive::OVERWRITE) === true)
{
self::addZip($path, $zip);
$zip->close();
}
}
/**
* 添加文件或文件夹到zip对象
* @param string $path
* @param ZipArchive $zip
*/
private static function addZip($path, $zip)
{
$handler = opendir($path);
while (($file = readdir($handler)) !== false)
{
if ($file != '.' && $file != '..')
{
if (is_dir($path . DIRECTORY_SEPARATOR . $file))
{
self::addZip($path . DIRECTORY_SEPARATOR . $file, $zip);
}
else
{
$zip->addFile($path . DIRECTORY_SEPARATOR . $file);
}
}
}
closedir($handler);
}
/**
* 解压文件
* @param string $file 压缩文件路径
* @param string $path 解压路径,为空则以文件名为路径
*/
public static function extra($file, $path = null)
{
if(!isset($path)){
$array = explode('.',$file);
$path = reset($array);
}
$zip = new ZipArchive();
if($zip->open($file) === true){
$zip->extractTo($path);
$zip->close();
}
}
}
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号