当前位置:首页 > PHP教程 > php文件操作 > 列表

php目录文件在线解压缩程序

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-15 14:42:29 浏览: 评论:0 

本文章提供一款完整的php目录 文件在线解压缩程序,他可打包指定目录并且把目录下所有目录与文件名都打包好,按rar的方式打包,目录结构不变,同时也提供解压功能.代码如下:

  1. $fz = new fmzip; 
  2. $fz->setzipname("打包文件名"); 
  3.  
  4. #打包/压缩 
  5. $fz->setsource("待打包目录"); 
  6. $fz->compress($silent,$compress); 
  7.  
  8. #解包/解压 
  9. $fz->settarget("待解包目录"); 
  10. $fz->uncompress($silent); 
  11. */ 
  12. class fmzip 
  13. { 
  14.  
  15. var $source; //压缩源 
  16. var $target; //解压目的文件夹 
  17. var $zipname;//压缩文件名 
  18. var $handle; //打开压缩文件的句柄 
  19. var $silent; //是否输出 
  20. var $count_dir; //计数器_文件夹 
  21. var $count_file;//计数器_文件 
  22. var $dirlist; 
  23.  
  24. function setsource($source)//设置压缩源 
  25. { 
  26.   if(!file_exists($source)) 
  27.     die("source <$source> does not exist."); 
  28.   $this->source = $source; 
  29. } 
  30.  
  31. function settarget($target)//设置解压目的文件夹 
  32. { 
  33.   if(!file_exists($target)) 
  34.     $this->makedir($target); 
  35.   chdir(dirname($_server['script_filename'])); 
  36.   if(substr($target,-1)=="/") 
  37.     $target = substr($target,0,strlen($target)-1); 
  38.   if(!file_exists($target)) 
  39.   { 
  40.     die("target <$target> does not exist."); 
  41.   } 
  42.   $this->target = $target; 
  43. } 
  44.  
  45. function setzipname($zipname)//设置压缩文件名 
  46. { 
  47.   if(emptyempty($zipname)) $zipname = "fmzip.fz"; 
  48.   $this->zipname = $zipname; 
  49. } 
  50.  
  51. function compress($silent = false, $compress = true) //压缩 
  52. { 
  53.   $this->silent = $silent; 
  54.   if($silent===false)echo "<pre>compressing...rn"; 
  55.   if(is_file("$this->zipname"))unlink("$this->zipname"); 
  56.   $this->handle = fopen($this->zipname,"w");//创建压缩文件 
  57.   if($this->handle == null) die("error creating $this->zipname");//打开失败 
  58.   $this->count_dir = 0; $this->count_file = 0; //初始化计数器 
  59.   $this->merge($this->source);//压缩 
  60.   fwrite($this->handle,"-1");//结束标志 
  61.   fclose($this->handle);//关闭文件 
  62.   echo "rndirectory: $this->count_dir"; 
  63.   echo "rnfile: $this->count_filern"; 
  64.   if(function_exists("gzcompress") && $compress==true) 
  65.   { 
  66.     file_put_contents("$this->zipname.gz",gzcompress(file_get_contents("$this->zipname"))); 
  67.     unlink("$this->zipname"); 
  68.   } 
  69.   if($silent===false) 
  70.   { 
  71.     echo $this->listfile(); 
  72.     echo "</pre>"; 
  73.   } 
  74. } 
  75.  
  76. function listfile() 
  77. { 
  78.   if(file_exists("$this->zipname.gz")) 
  79.     return "<a href="$this->zipname.gz" target="_blank">download $this->zipname.gz</a>"; 
  80.   if(file_exists("$this->zipname")) 
  81.     return "<a href="$this->zipname" target="_blank">download $this->zipname</a>"; 
  82. } 
  83.  
  84. function merge($dir)//合并文件、文件夹(递归) 
  85. { 
  86. /* 说明:不处理link。 */ 
  87.  if(is_dir($dir))//如果压缩源是文件夹 
  88.  { 
  89.   $list = scandir($dir);//扫描文件列表 
  90.   natcasesort($list); 
  91.   foreach($list as $file)//先处理文件夹 
  92.   { 
  93.     $full = "$dir/$file"; 
  94.     if(!is_dir($full)||$file=="."||$file=="..")continue;//只处理文件夹 
  95.     $this->count_dir++; 
  96.     if($this->silent===false) 
  97.       echo "[dir] $fullrn"; //输出提示 
  98.     fwrite($this->handle,$this->file_info($full));//写入文件夹信息 
  99.     $this->merge($full);//递归合并下级文件夹 
  100.   }//文件夹处理完毕; 
  101.   foreach($list as $file)//处理文件 
  102.   { 
  103.     $full = "$dir/$file"; 
  104.     if(!is_file($full)||$file=="."||$file=="..")continue; //只处理文件 
  105.     $this->count_file++; 
  106.     if($this->silent===false) 
  107.       echo "[file] $fullrn";//输出提示 
  108.     fwrite($this->handle,$this->file_info($full));//写入文件信息 
  109.   }//文件处理完毕 
  110.  } 
  111.  else 
  112.  { 
  113.    $this->count_file++; 
  114.    if($this->silent===false)echo "[file] $fullrn";//输出提示 
  115.    fwrite($this->handle,$this->file_info($file));//写入文件信息 
  116.  } 
  117. }//end function merge 
  118.  
  119. function file_info($file) 
  120. { 
  121.   $perm = substr(sprintf('%o',fileperms($file)), -3); //权限 
  122.   $filename = str_replace($this->source,"",$file); 
  123.   if(is_file($file))//文件 
  124.   { 
  125.     $size = filesize($file); //文件大小 
  126.     return "1rn$filenamern$permrn$sizern".file_get_contents($file)."rn";// .文件内容 
  127.   } 
  128.   if(is_dir($file))//目录 
  129.     return "0rn$filenamern$permrn"; 
  130. }//end function file_info 
  131.  
  132. function uncompress($silent = false) 
  133. { 
  134.   $this->silent = $silent; 
  135.   if($silent===false)echo "<pre>uncompressing...rn"; 
  136.   if(substr($this->zipname,-3)==".gz") 
  137.     $this->zipname = substr($this->zipname,0,strlen($this->zipname)-3); 
  138.   if(file_exists("$this->zipname.gz")) 
  139.   { 
  140.     if(!function_exists(gzuncompress)) 
  141.       die("function gzuncompress is not supported. unable to continue."); 
  142.     file_put_contents($this->zipname,gzuncompress(file_get_contents("$this->zipname.gz"))); 
  143.   } 
  144.   $this->handle = fopen($this->zipname,"r"); 
  145.   if($this->handle == null) die("error reading $this->zipname");//打开失败 
  146.   $count = 0; 
  147.   while(1) 
  148.   { 
  149.     $count ++; 
  150.     $type = $this->read_line(); //读取类型 
  151.     if($type === "-1")break;//处理完毕,退出 
  152.     $filename = $this->target.$this->read_line();//读取文件名 
  153.     $permission = $this->read_line();//读取权限 
  154. /* <文件夹> [0]n[file_name]n[perm]n */ 
  155.     if($type === "0")//目录 
  156.     { 
  157.       if($this->silent === false)//输出提示 
  158.         echo "[dir] $filename  [$permission]rn"; 
  159.       $this->makedir($filename);//创建文件夹 
  160.       chdir(dirname($_server['script_filename'])); 
  161.       chmod($filename,$permission); 
  162.       $this->dirlist[$filename] = 1; 
  163.       continue; 
  164.     } 
  165. /* <文件> [1]n[file_name]n[perm]n[size]n[contents]n */ 
  166.     if($type === "1")//文件 
  167.     { 
  168.       $this->count_file++; 
  169.       $size = $this->read_line(); //读取文件大小 
  170.       if($this->silent === false)//输出提示 
  171.         echo "[file] $filename  [$permission] [size = $size]rn"; 
  172.       if($size!=0) 
  173.       { 
  174.         $fp = fopen($filename,"w"); 
  175.         $contents = fread($this->handle,$size); 
  176.         fwrite($fp,$contents); 
  177.         fclose($fp); 
  178.         chmod($filename,$permission); 
  179.       } 
  180.       $this->read_line();//内容后的一个回车 
  181.       continue; 
  182.     } 
  183.   } 
  184.   $this->count_dir = count($this->dirlist); 
  185.   if($silent===false) 
  186.     echo "ndirectory: $this->count_dir"; 
  187.     echo "nfile: $this->count_filen</pre>n"; 
  188.   fclose($this->handle); 
  189.   if(file_exists("$this->zipname.gz"))unlink("$this->zipname"); 
  190. }//end function uncompress; 
  191.  
  192. function read_line() 
  193. { 
  194.   $a = fgets($this->handle); 
  195.   $a = str_replace("rn","",$a); 
  196.   $a = str_replace("n","",$a); 
  197.   return $a; 
  198. } 
  199.  
  200. function makedir($full) 
  201. { 
  202.   list($a,$b) = split("/",$full,2); 
  203.   if($a == "") return; 
  204.   if(file_exists($a)&&!is_dir($a))die("can't create dir $a"); 
  205.   if(!file_exists($a))@mkdir($a); 
  206.   chdir($a);//开源代码phpfensi.com 
  207.   if($b!=="") 
  208.     $this->makedir($b); 
  209.   chdir(".."); 
  210. }//end function makedir 
  211.  
  212. } //end class fmzip 
  213.  
  214. /* 
  215.  
  216.  
  217. //使用方法: 
  218.  
  219. #必须 
  220. include("包含这个class的php文件"); 
  221. $fz = new fmzip; 
  222. $fz->setzipname("打包文件名"); 
  223.  
  224. #打包/压缩 
  225. $fz->setsource("待打包目录"); 
  226. $fz->compress($silent,$compress); 
  227.  
  228. #解包/解压 
  229. $fz->settarget("待解包目录"); 
  230. $fz->uncompress($silent); 
  231.  
  232. $silent : true|false (不加引号!) 是否产生输出 默认为true,不产生 
  233. $compress : true|false (不加引号!) 是否压缩 默认为true,压缩 

Tags: php目录文件 在线解压缩

分享到: