国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > PHP > 正文

PHP zip壓縮包操作類(lèi)完整實(shí)例

2024-05-05 00:04:22
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了PHP zip壓縮包操作類(lèi)。分享給大家供大家參考,具體如下:

<?php/** * Zip 文件包工具 * * @author wengxianhu * @date 2013-08-05 */class ZipFolder{  protected $zip;  protected $root;  protected $ignored_names;  public function __construct(){    $this->zip = new ZipArchive;  }  /**   * 解壓zip文件到指定文件夾   *   * @access public   * @param string $zipfile 壓縮文件路徑   * @param string $path  壓縮包解壓到的目標(biāo)路徑   * @return booleam 解壓成功返回 true 否則返回 false  */  public function unzip ($zipfile, $path) {    if ($this->zip->open($zipfile) === true) {      $file_tmp = @fopen($zipfile, "rb");      $bin = fread($file_tmp, 15); //只讀15字節(jié) 各個(gè)不同文件類(lèi)型,頭信息不一樣。      fclose($file_tmp);      /* 只針對(duì)zip的壓縮包進(jìn)行處理 */      if (true === $this->getTypeList($bin))      {        $result = $this->zip->extractTo($path);        $this->zip->close();        return $result;      }      else      {        return false;      }    }    return false;  }  /**   * 創(chuàng)建壓縮文件   *   * @access public   * @param string $zipfile 將要生成的壓縮文件路徑   * @param strng $folder 將要被壓縮的文件夾路徑   * @param array $ignored 要忽略的文件列表   * @return booleam 壓縮包生成成功返回true 否則返回 false  */   public function zip ($zipfile, $folder, $ignored = null) {    $this->ignored_names = is_array($ignored) ? $ignored : $ignored ? array($ignored) : array();    if ($this->zip->open($zipfile, ZIPARCHIVE::CREATE) !== true) {      throw new Exception("cannot open <$zipfile>/n");    }    $folder = substr($folder, -1) == '/' ? substr($folder, 0, strlen($folder)-1) : $folder;    if(strstr($folder, '/')) {      $this->root = substr($folder, 0, strrpos($folder, '/')+1);      $folder = substr($folder, strrpos($folder, '/')+1);    }    $this->createZip($folder);    return $this->zip->close();  }  /**   * 遞歸添加文件到壓縮包   *   * @access private   * @param string $folder 添加到壓縮包的文件夾路徑   * @param string $parent 添加到壓縮包的文件夾上級(jí)路徑   * @return void  */  private function createZip ($folder, $parent=null) {    $full_path = $this->root . $parent . $folder;    $zip_path = $parent . $folder;    $this->zip->addEmptyDir($zip_path);    $dir = new DirectoryIterator($full_path);    foreach($dir as $file) {      if(!$file->isDot()) {        $filename = $file->getFilename();        if(!in_array($filename, $this->ignored_names)) {          if($file->isDir()) {            $this->createZip($filename, $zip_path.'/');          }else {            $this->zip->addFile($full_path.'/'.$filename, $zip_path.'/'.$filename);          }        }      }    }  }  /**   * 讀取壓縮包文件與目錄列表   *   * @access public   * @param string $zipfile 壓縮包文件   * @return array 文件與目錄列表  */  public function fileList($zipfile) {    $file_dir_list = array();    $file_list = array();    if ($this->zip->open($zipfile) == true) {      for ($i = 0; $i < $this->zip->numFiles; $i++) {        $numfiles = $this->zip->getNameIndex($i);        if (preg_match('///$/i', $numfiles))        {          $file_dir_list[] = $numfiles;        }        else        {          $file_list[] = $numfiles;        }      }    }    return array('files'=>$file_list, 'dirs'=>$file_dir_list);  }  /**  * 得到文件頭與文件類(lèi)型映射表  *  * @author wengxianhu  * @date 2013-08-10  * @param $bin string 文件的二進(jìn)制前一段字符  * @return boolean  */  private function getTypeList ($bin)  {    $array = array(      array("504B0304", "zip")    );    foreach ($array as $v)    {      $blen = strlen(pack("H*", $v[0])); //得到文件頭標(biāo)記字節(jié)數(shù)      $tbin = substr($bin, 0, intval($blen)); ///需要比較文件頭長(zhǎng)度      if(strtolower($v[0]) == strtolower(array_shift(unpack("H*", $tbin))))      {        return true;      }    }    return false;  }}

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到PHP教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 那坡县| 互助| 桓仁| 自贡市| 鄯善县| 房产| 曲阜市| 清远市| 伊金霍洛旗| 许昌市| 墨江| 台东县| 东台市| 进贤县| 囊谦县| 南安市| 罗田县| 芜湖县| 肥东县| 丰宁| 广水市| 黑山县| 深泽县| 西藏| 贵州省| 大石桥市| 革吉县| 宁乡县| 甘德县| 江阴市| 舞阳县| 彭山县| 高邑县| 曲周县| 凤凰县| 丽江市| 广东省| 阿荣旗| 那坡县| 琼中| 哈尔滨市|