本文章提供一款完整的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)
- {
-
- 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));
- }
- }
-
- 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";
- }
-
- 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();
-
- 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;
- }
-
- 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");
- }
-
- 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);
- if($b!=="")
- $this->makedir($b);
- chdir("..");
- }
-
- }
-
- /*
-
-
-
-
- #必须
- 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,压缩
|