用ziparchive压缩文件,这个是php的扩展类,自php5.2版本以后就已经支持这个扩展,如果你在使用的时候出现错误,查看下php.ini里面的extension=php_zip.dll前面的分号有没有去掉,然后再重启apache这样才能使用这个类库。
例1、生成zip 压缩文件
复制代码 代码如下:
open($destination,$overwrite ? ziparchive::overwrite : ziparchive::create) !== true) {
return false;
}
//add the files
foreach($valid_files as $file) {
$file_info_arr= pathinfo($file);
$zip->addfile($file,$file_info_arr['basename']);//去掉层级目录
}
//debug
//echo 'the zip archive contains ',$zip->numfiles,' files with a status of ',$zip->status;
//close the zip -- done!
$zip->close();
//check to make sure the file exists
return file_exists($destination);
}
else
{
return false;
}
}
define('rootpath',dirname ( __file__ )); //网站路径
$files_to_zip = array(
rootpath.directory_separator.'php+jquery+cookbook.pdf',
rootpath.directory_separator.'turbolisterzerotemplate.csv'
);
//if true, good; if false, zip creation failed
$filename='my-archive.zip';
$result = create_zip($files_to_zip,$filename);
例2 、压缩文件夹下面的所有文
复制代码 代码如下:
addfile($filepath, $localpath);
} elseif (is_dir($filepath)) {
// 添加子文件夹
$zipfile->addemptydir($localpath);
self::foldertozip($filepath, $zipfile, $exclusivelength);
}
}
}
closedir($handle);
}
/**
* zip a folder (include itself).
* usage:
* hzip::zipdir('/path/to/sourcedir', '/path/to/out.zip');
*
* @param string $sourcepath path of directory to be zip.
* @param string $outzippath path of output zip file.
*/
public static function zipdir($sourcepath, $outzippath)
{
$pathinfo = pathinfo($sourcepath);
$parentpath = $pathinfo['dirname'];
$dirname = $pathinfo['basename'];
$sourcepath=$parentpath.'/'.$dirname;//防止传递'folder' 文件夹产生bug
$z = new ziparchive();
$z->open($outzippath, ziparchive::create);//建立zip文件
$z->addemptydir($dirname);//建立文件夹
self::foldertozip($sourcepath, $z, strlen($parentpath/));
$z->close();
}
}
//使用方法
hzip::zipdir('yourlife', 'yourlife.zip');
?>
1.ziparchive::addemptydir
添加一个新的文件目录
2.ziparchive::addfile
将文件添加到指定zip压缩包中。
3.ziparchive::addfromstring
添加的文件同时将内容添加进去
4.ziparchive::close
关闭ziparchive
5.ziparchive::extractto
将压缩包解压
6.ziparchive::open
打开一个zip压缩包
7.ziparchive::getstatusstring
返回压缩时的状态内容,包括错误信息,压缩信息等等
8.ziparchive::deleteindex
删除压缩包中的某一个文件,如:deleteindex(0)删除第一个文件
9.ziparchive::deletename
删除压缩包中的某一个文件名称,同时也将文件删除。
http://www.bkjia.com/phpjc/825095.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/825095.htmltecharticle用ziparchive压缩文件,这个是php的扩展类,自php5.2版本以后就已经支持这个扩展,如果你在使用的时候出现错误,查看下php.ini里面的extensi...