找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1920|回复: 0
打印 上一主题 下一主题

php生成zip文件类实例

[复制链接]

2536

主题

2536

帖子

7532

积分

论坛元老

Rank: 8Rank: 8

积分
7532
跳转到指定楼层
楼主
发表于 2018-2-14 05:47:09 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            本文实例讲述了php生成zip文件类。分享给大家供大家参考。具体如下:
"",
   "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实现zip压缩文件解压缩代码分享(简单易懂)
  • php实现的zip文件内容比较类
  • php在线解压ZIP文件的方法
  • php操作(删除,提取,增加)zip文件方法详解
  • php实现zip文件解压操作
  • php打包网站并在线压缩为zip
  • php简单创建zip压缩文件的方法
  • php打包压缩文件之ZipArchive方法用法分析
  • php将文件夹打包成zip文件的简单实现方法
  • PHP读取zip文件的方法示例
  • php操作zip在不解压缩包的情况下显示压缩包中的图片
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端