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程序设计有所帮助。

相关文章

提示Trying to clone an uncloneable object of class Imagic的解决

使用网上流传的一个程序实现pdf截图为png,需要使用Imagic扩展。在windows下安装完后提示: Fatal error: Trying to clone an unclonea...

Laravel 5.3 学习笔记之 错误&日志

1、简介 Laravel默认已经为我们配置好了错误和异常处理,我们在App\Exceptions\Handler类中触发异常并将响应返回给用户。本教程我们将深入探讨这个类。 此外,La...

Youku 视频绝对地址获取的方法详解

前一阵子为了研究 KnLiveCommentary 而进行了一系列的关于视频站点的研究。由于KnLiveCommentary需要能够获取充足的视频源进行测试,所以我们选取了 Youku(...

php使用function_exists判断函数可用的方法

本文实例讲述了php使用function_exists判断函数可用的方法。分享给大家供大家参考。具体如下: 本文所述的函数用来建立一张 gif 格式图形,参数 im 为使用 imagec...

探讨:parse url解析URL,返回其组成部分

parse_url(PHP 4, PHP 5)parse_url — 解析 URL,返回其组成部分说明array parse_url ( string $url )本函数解析一个 URL...