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

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

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