时时商务社区
标题:
php生成zip文件类实例
[打印本页]
作者:
wx_pylelnL4
时间:
2018-2-14 05:47
本文实例讲述了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在不解压缩包的情况下显示压缩包中的图片
欢迎光临 时时商务社区 (http://bbs.4435.cn/)
Powered by Discuz! X3.2