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实现的事件机制实例分析

本文实例讲述了基于PHP实现的事件机制。分享给大家供大家参考。具体分析如下: 内置了事件机制的语言不多,php也没有提供这样的功能。事件(Event)说简单了就是一个Observer模式...

php使用curl实现ftp文件下载功能

本文实例为大家分享了php实现ftp文件下载功能,供大家参考,具体内容如下 不知道为什么用正常的ftp_get函数下载文件速度特别慢,但是用ftp的客户端下载很快,所以换了curl的下载...

Swoole实现异步投递task任务案例详解

Swoole实现异步投递task任务案例详解

本文实例讲述了Swoole实现异步投递task任务案例。分享给大家供大家参考,具体如下: 【使用场景】       Swolle的t...

PHP 获取ip地址代码汇总

代码一: function getip() { static $ip = ''; $ip = $_SERVER['REMOTE_ADDR']; if(isset($_SE...

PHP基于curl post实现发送url及相关中文乱码问题解决方法

本文实例讲述了PHP基于curl post实现发送url及相关中文乱码问题解决方法。分享给大家供大家参考,具体如下: 这个问题困扰我几天了,发送的指定网址的url参数,中文总是乱码,指定...