PHP
·
发表于 5年以前
·
阅读量:8298
±¾ÎÄʵÀý½²ÊoÁËPHP½aѹZIPÎļþµ½Ö¸¶¨Îļþ¼ÐµÄ½¨¡£*ÖÏi¸ø´o¼Ò¹(C)´o¼Ò²Î¿¼£¬¾ßÌaÈçÏ£º
/**
* function: ½aѹzip ¸ñʽµÄÎļþ
* author£ºfriker
* date:2015-15-14
* reference£ºhttp://php.net/manual/zh/ref.zip.php
* all rights reserved:wujiangwei123@126.com
*/
class Unzip{
public function __construct(){
//init code here...
header("content-type:text/html;charset=utf8");
}
/**
* ½aѹÎļþµ½Ö¸¶¨Ä¿Â¼
*
* @param string zipѹËoÎļþµÄÂ*¾¶
* @param string ½aѹÎļþµÄÄ¿µÄÂ*¾¶
* @param boolean ÊÇ*ñÒÔѹËoÎļþµÄÃu×Ö´´½¨Ä¿±eÎļþ¼Ð
* @param boolean ÊÇ*ñÖØдÒѾ´aeÔÚµÄÎļþ
*
* @return boolean *µ»Ø³É¹¦ »oʧ°Ü
*/
public function unzip($src_file, $dest_dir=false, $create_zip_name_dir=true, $overwrite=true){
if ($zip = zip_open($src_file)){
if ($zip){
$splitter = ($create_zip_name_dir === true) ? "." : "/";
if($dest_dir === false){
$dest_dir = substr($src_file, 0, strrpos($src_file, $splitter))."/";
}
// Èç¹u²»´aeÔÚ ´´½¨Ä¿±e½aѹĿ¼
$this->create_dirs($dest_dir);
// ¶Ôÿ¸oÎļþ½øÐнaѹ
while ($zip_entry = zip_read($zip)){
// Îļþ²»ÔÚ¸uĿ¼
$pos_last_slash = strrpos(zip_entry_name($zip_entry), "/");
if ($pos_last_slash !== false){
// ´´½¨Ä¿Â¼ ÔÚÄ(C)β´ø /
$this->create_dirs($dest_dir.substr(zip_entry_name($zip_entry), 0, $pos_last_slash+1));
}
// ´o¿ª°u
if (zip_entry_open($zip,$zip_entry,"r")){
// ÎļþÃu±£´aeÔÚ´ÅÅÌÉÏ
$file_name = $dest_dir.zip_entry_name($zip_entry);
// ¼i²eÎļþÊÇ*ñÐeÒªÖØд
if ($overwrite === true || $overwrite === false && !is_file($file_name)){
// ¶ÁȡѹËoÎļþµÄÄÚÈÝ
$fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
@file_put_contents($file_name, $fstream);
// ÉeÖÃȨÏÞ
chmod($file_name, 0777);
echo "save: ".$file_name."<br />";
}
// ¹Ø±ÕÈe¿Ú
zip_entry_close($zip_entry);
}
}
// ¹Ø±ÕѹËo°u
zip_close($zip);
}
}else{
return false;
}
return true;
}
/**
* ´´½¨Ä¿Â¼
*/
public function create_dirs($path){
if (!is_dir($path)){
$directory_path = "";
$directories = explode("/",$path);
array_pop($directories);
foreach($directories as $directory){
$directory_path .= $directory."/";
if (!is_dir($directory_path)){
mkdir($directory_path);
chmod($directory_path, 0777);
}
}
}
}
}
/*
using:
$z = new Unzip();
$z->unzip("./bootstrap-3.3.4.zip",'./unzipres/', true, false);
*/
¸u¶a¹ØÓÚPHPÏa¹ØÄÚÈݸÐÐËȤµÄ¶ÁÕ߿ɲe¿´±¾Õ¾×¨Ìa£º¡¶PHP²Ù×÷zipÎļþ¼°Ñ¹Ëo¼¼ÇÉ×ܽa¡¡¢¡¶phpÎļþ²Ù×÷×ܽa¡¡¢¡¶phpÕýÔo±i´iʽÓÃ*¨×ܽa¡¡¢¡¶[PHPÔËËaÓeÔËËauÓè×ܽa](http://www.jb51.net/Special/357.htm)¡¡¢¡¶PHP»u±¾Ói*¨ÈeÃŽ̡̳¡¢¡¶phpÃaeÏo¶ÔÏo³ÌÐoÉe¼ÆÈeÃŽ̡̳¡¢¡¶php×Öu´®(string)Óè×ܽa¡¡¢¡¶php+mysqlÊý¾Ý¿a²Ù×÷ÈeÃŽ̡̳¼°¡¶php³£¼uÊý¾Ý¿a²Ù×÷¼¼ÇÉ»a×Ü¡*
Ï£Íu±¾ÎÄËuÊo¶Ô´o¼ÒPHP³ÌÐoÉe¼ÆÓÐËu°iÖu¡£