php目錄 檔案線上解壓縮程式

來源:互聯網
上載者:User

$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(empty($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);
  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,壓縮

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.