Home >php教程 >php手册 >zip压缩解压函数

zip压缩解压函数

WBOY
WBOYOriginal
2016-06-07 11:44:201499browse

zip压缩解压函数
<?php <br /> function ezip($zip, $hedef = ''){<br>     $dirname=preg_replace('/.zip/', '', $zip);<br>     $root = $_SERVER['DOCUMENT_ROOT'].'/zip/';<br>     // echo $root. $zip;<br>     $zip = zip_open($root . $zip);<br>     // var_dump($zip);<br>     @mkdir($root . $hedef . $dirname.'/'.$zip_dosya);<br>     while($zip_icerik = zip_read($zip)){<br> <br>         $zip_dosya = zip_entry_name($zip_icerik);<br>         if(strpos($zip_dosya, '.')){<br>             $hedef_yol = $root . $hedef . $dirname.'/'.$zip_dosya;<br>             @touch($hedef_yol);<br>             // echo $hedef_yol;<br>             $yeni_dosya = @fopen($hedef_yol, 'w+');<br>             @fwrite($yeni_dosya, zip_entry_read($zip_icerik));<br>             @fclose($yeni_dosya); <br>             // $yeni_dosya;<br> <br>         }else{<br>             @mkdir($root . $hedef . $dirname.'/'.$zip_dosya);<br>             // echo $root . $hedef . 'x/'.$zip_dosya;<br>         };<br>     };<br> }<br> // ezip('yuol.zip','./tr/');<br> <br> function zip($path) {<br>     $path=preg_replace('/\/$/', '', $path);<br>     preg_match('/\/([\d\D][^\/]*)$/', $path, $matches, PREG_OFFSET_CAPTURE);<br>     $filename=$matches[1][0].".zip";<br>     // var_dump($filename);<br>     // set_time_limit(0);<br>     $zip = new ZipArchive();<br>     $zip->open($filename,ZIPARCHIVE::OVERWRITE);//return ;<br>     // var_dump($path);<br>     if (is_file($path)) {<br>         $path=preg_replace('/\/\//', '/', $path);<br>         $base_dir=preg_replace('/\/[\d\D][^\/]*$/', '/', $path);<br>         $base_dir=addcslashes($base_dir, '/:');<br> <br>         $localname=preg_replace('/'.$base_dir.'/', '', $path);<br>         // var_dump($localname);<br>         $zip->addFile($path,$localname);<br>         // var_dump($path);<br>         $zip->close();<br>         return;<br>     }elseif (is_dir($path)) {<br>         $path=preg_replace('/\/[\d\D][^\/]*$/', '', $path);<br>         $base_dir=$path.'/';//基目录<br>         $base_dir=addcslashes($base_dir, '/:');<br>         // var_dump($base_dir);<br>     }<br>     $path=preg_replace('/\/\//', '/', $path);<br>     // var_dump($path);<br>     function addItem($path,&$zip,&$base_dir){<br>         // var_dump($path);<br>         $handle = opendir($path);<br>         // var_dump($path);<br>         while (false !== ($file = readdir($handle))) {<br>             if (($file!='.')&&($file!='..')){<br>                 // var_dump($file);<br>                 $ipath=$path.'/'.$file;<br>                 if (is_file($ipath)){//条目是文件<br>                     $localname=preg_replace('/'.$base_dir.'/', '', $ipath);<br>                     var_dump($localname);<br>                     $zip->addFile($ipath,$localname);<br>                     // var_dump($r);<br>                 } else if (is_dir($ipath)){<br>                     addItem($ipath,$zip,$base_dir);<br>                     $localname=preg_replace('/'.$base_dir.'/', '', $ipath);<br>                     var_dump($localname);<br>                     $zip->addEmptyDir($localname);<br>                 }<br>                 // var_dump($path);<br>             }<br>         }<br>     }<br>     // var_dump($base_dir);<br>     addItem($path,$zip,$base_dir);<br>     $zip->close();<br> }<br> zip('D:/wamp/www/sample/main/');

附件 zip.zip ( 1.03 KB 下载:310 次 )

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn