zip函数是php_zip扩展针对zip压缩包做的简单的函数处理,比如打开获取压缩包中栏目的名称,大小,压缩后的大小,以及内容读取等功能
工具/原料
php_zip扩展
php官方api
常用函数讲解
1、打开/关闭zip资源文件resource zip_read ( resource $zip )void zip_close ( resource $zip ) 关闭zip资源
2、逐步获取zip压缩包中的条目resource zip_read ( resource $zip )
3、获取条目名称,实际大小和压缩后的大小string zip_entry_name ( resource $zip_entry ) 返回条目名称int zip_entry_filesize ( resource $zip_entry ) 返回条目实际大小int zip_entry_compressedsize ( resource $zip_entry ) 返回条目压缩后的大小
4、关闭条目资源bool zip_entry_close ( resource $zip_entry )
获取zip压缩包中内容列表的实例
1、$zip = zip_open("demo.zip")挢旗扦渌;if($zip == true){ $htm造婷用痃l ="<table><tr><th>名称</th><th>实际大小</th><th>压缩大小</th></tr>"; while ($entry = zip_read($zip)) { $html .="<tr><td>".zip_entry_name($entry)."</td><td>".zip_entry_filesize($entry)."</td><td>".zip_entry_compressedsize($entry)."</td></tr>"; } $html .="</table>"; echo $html;}zip_close($zip);

Zip函数版zip压缩包解压
1、该版本可能就比较复杂function ezip($file,$path){ $zip = zip_open烫喇霰嘴($file); if($zip == true){ while ($entry = zip_read($zip)) { $name = zip_entry_name($entry); $rel_path = is_dir($name) ? $name : dirname($name); $basePath = $path."/".$rel_path; mkdirs($basePath); if(zip_entry_open($zip, $entry)){ $fw = @fopen($path."/".$name, "w+"); $content = zip_entry_read($entry); @fwrite($fw, $content); @fclose($fw); } zip_entry_close($entry); } } zip_close($zip);}function mkdirs($dir){ return is_dir($dir) or mkdirs(dirname($dir)) and @mkdir($dir,0777);}ezip("demo.zip",'h');
