php生成zip文件类实例

yipeiwu_com5年前PHP代码库

本文实例讲述了php生成zip文件类。分享给大家供大家参考。具体如下:

<?php
 /*
  By:   Matt Ford
  Purpose: Basic class to create zipfiles
 */
class zipFile {
 public $files = array();
 public $settings = NULL;
 public $fileInfo = array (
   "name" => "",
   "numFiles" => 0,
   "fullFilePath" => ""
  );
 private $fileHash = "";
 private $zip = "";
 public function __construct($settings) {
  $this->zipFile($settings);
 }
 public function zipFile($settings) {
  $this->zip = new ZipArchive();
  $this->settings = new stdClass();
  foreach ($settings as $k => $v) {
   $this->settings->$k = $v;
  }
 }
 public function create() {
  $this->fileHash = md5(implode(",", $this->files));
  $this->fileInfo["name"] = $this->fileHash . ".zip";
  $this->fileInfo["numFiles"] = count($this->files);
  $this->fileInfo["fullFilePath"] = $this->settings->path . 
  "/" . $this->fileInfo["name"];
  if (file_exists($this->fileInfo["fullFilePath"])) {
   return array (
     false,
     "already created: " . $this->fileInfo["fullFilePath"]
     );
  }
  else {
   $this->zip->open($this->fileInfo["fullFilePath"], ZIPARCHIVE::CREATE);
   $this->addFiles();
   $this->zip->close();
   return array (
     true,
     "new file created: " . $this->fileInfo["fullFilePath"]
     );
  }
 }
 private function addFiles() {
  foreach ($this->files as $k) {
   $this->zip->addFile($k, basename($k));
  }
 }
}
$settings = array (
  "path" => dirname(__FILE__)
 );
$zipFile = new zipFile($settings);
$zipFile->files = array (
  "./images/navoff.jpg",
  "./images/navon.jpg"
 );
list($success, $error) = $zipFile->create();
if ($success === true) {
 //success
}
else {
 //error because: $error
}
?>

希望本文所述对大家的php程序设计有所帮助。

相关文章

PHP数组编码gbk与utf8互相转换的两种方法

一、利用var_export(), eval()方法 /** * 将含有GBK的中文数组转为utf-8 * * @param array $arr 数组 * @para...

php中判断字符串是否全是中文或含有中文的实现代码

一,判断全是中文 复制代码 代码如下: $str="'324是"; if(!eregi("[^\x80-\xff]","$str")){ echo "全是中文"; }else{ echo...

浅谈PHP强制类型转换,慎用!

PHP是一门弱类型的语言。这是它的优势和特点,但是有的时候你又不得不对类型进行相应的转换。 这个时候问题就来了。因为很多情况下,你会发现转换类型之后得到的数据和预期的值相差老大一截。 这...

浅谈php和js中json的编码和解码

php中 1)编码 $jsonStr = json_encode($array) 2)解码 $arr = json_decode($jsonStr) <?php echo...

实现了一个PHP5的getter/setter基类的代码

PHP3、PHP4都拥有类,但它们的类定义的实在很不像样,效率还挺难为情的,但资料上说PHP5重新构造了面向对象的支持,尽管并不是完全面向对象,但也算能拿出来见人了。 昨天晚上闲着无聊便...