php目录文件在线解压缩程序
本文章提供一款完整的php目录 文件在线解压缩程序,他可打包指定目录并且把目录下所有目录与文件名都打包好,按rar的方式打包,目录结构不变,同时也提供解压功能.代码如下:
- $fz = new fmzip;
- $fz->setzipname("打包文件名");
- #打包/压缩
- $fz->setsource("待打包目录");
- $fz->compress($silent,$compress);
- #解包/解压
- $fz->settarget("待解包目录");
- $fz->uncompress($silent);
- */
- class fmzip
- {
- var $source; //压缩源
- var $target; //解压目的文件夹
- var $zipname;//压缩文件名
- var $handle; //打开压缩文件的句柄
- var $silent; //是否输出
- var $count_dir; //计数器_文件夹
- var $count_file;//计数器_文件
- var $dirlist;
- function setsource($source)//设置压缩源
- {
- if(!file_exists($source))
- die("source <$source> does not exist.");
- $this->source = $source;
- }
- function settarget($target)//设置解压目的文件夹
- {
- if(!file_exists($target))
- $this->makedir($target);
- chdir(dirname($_server['script_filename']));
- if(substr($target,-1)=="/")
- $target = substr($target,0,strlen($target)-1);
- if(!file_exists($target))
- {
- die("target <$target> does not exist.");
- }
- $this->target = $target;
- }
- function setzipname($zipname)//设置压缩文件名
- {
- if(emptyempty($zipname)) $zipname = "fmzip.fz";
- $this->zipname = $zipname;
- }
- function compress($silent = false, $compress = true) //压缩
- {
- $this->silent = $silent;
- if($silent===false)echo "<pre>compressing...rn";
- if(is_file("$this->zipname"))unlink("$this->zipname");
- $this->handle = fopen($this->zipname,"w");//创建压缩文件
- if($this->handle == null) die("error creating $this->zipname");//打开失败
- $this->count_dir = 0; $this->count_file = 0; //初始化计数器
- $this->merge($this->source);//压缩
- fwrite($this->handle,"-1");//结束标志
- fclose($this->handle);//关闭文件
- echo "rndirectory: $this->count_dir";
- echo "rnfile: $this->count_filern";
- if(function_exists("gzcompress") && $compress==true)
- {
- file_put_contents("$this->zipname.gz",gzcompress(file_get_contents("$this->zipname")));
- unlink("$this->zipname");
- }
- if($silent===false)
- {
- echo $this->listfile();
- echo "</pre>";
- }
- }
- function listfile()
- {
- if(file_exists("$this->zipname.gz"))
- return "<a href="$this->zipname.gz" target="_blank">download $this->zipname.gz</a>";
- if(file_exists("$this->zipname"))
- return "<a href="$this->zipname" target="_blank">download $this->zipname</a>";
- }
- function merge($dir)//合并文件、文件夹(递归)
- {
- /* 说明:不处理link。 */
- if(is_dir($dir))//如果压缩源是文件夹
- {
- $list = scandir($dir);//扫描文件列表
- natcasesort($list);
- foreach($list as $file)//先处理文件夹
- {
- $full = "$dir/$file";
- if(!is_dir($full)||$file=="."||$file=="..")continue;//只处理文件夹
- $this->count_dir++;
- if($this->silent===false)
- echo "[dir] $fullrn"; //输出提示
- fwrite($this->handle,$this->file_info($full));//写入文件夹信息
- $this->merge($full);//递归合并下级文件夹
- }//文件夹处理完毕;
- foreach($list as $file)//处理文件
- {
- $full = "$dir/$file";
- if(!is_file($full)||$file=="."||$file=="..")continue; //只处理文件
- $this->count_file++;
- if($this->silent===false)
- echo "[file] $fullrn";//输出提示
- fwrite($this->handle,$this->file_info($full));//写入文件信息
- }//文件处理完毕
- }
- else
- {
- $this->count_file++;
- if($this->silent===false)echo "[file] $fullrn";//输出提示
- fwrite($this->handle,$this->file_info($file));//写入文件信息
- }
- }//end function merge
- function file_info($file)
- {
- $perm = substr(sprintf('%o',fileperms($file)), -3); //权限
- $filename = str_replace($this->source,"",$file);
- if(is_file($file))//文件
- {
- $size = filesize($file); //文件大小
- return "1rn$filenamern$permrn$sizern".file_get_contents($file)."rn";// .文件内容
- }
- if(is_dir($file))//目录
- return "0rn$filenamern$permrn";
- }//end function file_info
- function uncompress($silent = false)
- {
- $this->silent = $silent;
- if($silent===false)echo "<pre>uncompressing...rn";
- if(substr($this->zipname,-3)==".gz")
- $this->zipname = substr($this->zipname,0,strlen($this->zipname)-3);
- if(file_exists("$this->zipname.gz"))
- {
- if(!function_exists(gzuncompress))
- die("function gzuncompress is not supported. unable to continue.");
- file_put_contents($this->zipname,gzuncompress(file_get_contents("$this->zipname.gz")));
- }
- $this->handle = fopen($this->zipname,"r");
- if($this->handle == null) die("error reading $this->zipname");//打开失败
- $count = 0;
- while(1)
- {
- $count ++;
- $type = $this->read_line(); //读取类型
- if($type === "-1")break;//处理完毕,退出
- $filename = $this->target.$this->read_line();//读取文件名
- $permission = $this->read_line();//读取权限
- /* <文件夹> [0]n[file_name]n[perm]n */
- if($type === "0")//目录
- {
- if($this->silent === false)//输出提示
- echo "[dir] $filename [$permission]rn";
- $this->makedir($filename);//创建文件夹
- chdir(dirname($_server['script_filename']));
- chmod($filename,$permission);
- $this->dirlist[$filename] = 1;
- continue;
- }
- /* <文件> [1]n[file_name]n[perm]n[size]n[contents]n */
- if($type === "1")//文件
- {
- $this->count_file++;
- $size = $this->read_line(); //读取文件大小
- if($this->silent === false)//输出提示
- echo "[file] $filename [$permission] [size = $size]rn";
- if($size!=0)
- {
- $fp = fopen($filename,"w");
- $contents = fread($this->handle,$size);
- fwrite($fp,$contents);
- fclose($fp);
- chmod($filename,$permission);
- }
- $this->read_line();//内容后的一个回车
- continue;
- }
- }
- $this->count_dir = count($this->dirlist);
- if($silent===false)
- echo "ndirectory: $this->count_dir";
- echo "nfile: $this->count_filen</pre>n";
- fclose($this->handle);
- if(file_exists("$this->zipname.gz"))unlink("$this->zipname");
- }//end function uncompress;
- function read_line()
- {
- $a = fgets($this->handle);
- $a = str_replace("rn","",$a);
- $a = str_replace("n","",$a);
- return $a;
- }
- function makedir($full)
- {
- list($a,$b) = split("/",$full,2);
- if($a == "") return;
- if(file_exists($a)&&!is_dir($a))die("can't create dir $a");
- if(!file_exists($a))@mkdir($a);
- chdir($a);//开源代码phpfensi.com
- if($b!=="")
- $this->makedir($b);
- chdir("..");
- }//end function makedir
- } //end class fmzip
- /*
- //使用方法:
- #必须
- include("包含这个class的php文件");
- $fz = new fmzip;
- $fz->setzipname("打包文件名");
- #打包/压缩
- $fz->setsource("待打包目录");
- $fz->compress($silent,$compress);
- #解包/解压
- $fz->settarget("待解包目录");
- $fz->uncompress($silent);
- $silent : true|false (不加引号!) 是否产生输出 默认为true,不产生
- $compress : true|false (不加引号!) 是否压缩 默认为true,压缩