php 生成zip文件的类的解决办法

内容摘要
这篇文章主要为大家详细介绍了php 生成zip文件的类的简单示例,具有一定的参考价值,可以用来参考一下。

对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码如下:

<?php
/*
文章正文

这篇文章主要为大家详细介绍了php 生成zip文件的类的简单示例,具有一定的参考价值,可以用来参考一下。

对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码如下:

<?php
/**
 * 生成zip文件类
 *
 * @param 
 * @arrange (www.idcnote.com)
 **/
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教程(www.idcnote.com) ***/ 
?>

注:关于php 生成zip文件的类的简单示例的内容就先介绍到这里,更多相关文章的可以留意

代码注释

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!