php生成zip文件类实例

yipeiwu_com6年前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实现的XML操作(读取)封装类完整实例

本文实例讲述了php实现的XML操作(读取)封装类。分享给大家供大家参考,具体如下: <?xml version="1.0" encoding="utf-8" stan...

PHP聚合式迭代器接口IteratorAggregate用法分析

本文实例讲述了PHP聚合式迭代器接口IteratorAggregate用法。分享给大家供大家参考,具体如下: PHP IteratorAggregate又叫聚合式迭代器,它提供了创建外部...

PHP json_decode函数详细解析

PHP json_decode函数详细解析

一、函数简介1、此函数有四个参数,一般在使用时会用到前两个,具体的参数可以看手册。 2、第一个参数是json格式的字符串,第二个参数是boolean值(false转化成对象,true转化...

Linux下PHP加速器APC的安装与配置笔记

当前我用的是APC-3.1.9 stable ,可以自己到 http://pecl.php.net/package/APC 下载最新版。 1、安装 复制代码 代码如下: wget htt...

php采集时被封ip的解决方法

在网上找了一些资料都没有找到,功夫不负有心人啊,在找的时侯有一个人提到了用搜索引擎爬虫蜘蛛的USERAGENT。虽然只提到一点点我还是想到了,列出我的解决方法, 1.使用Snoopy或c...