这篇文章主要介绍了php生成zip文件类,实例分析了php操作zip文件的技巧,非常具有实用价值,需要的朋友可以参考下
本文实例讲述了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生成zip文件类实例详解的详细内容。
