/** new Creatzip ($_dir,$_zipname); *@ _dir is a compressed folder name, you can use the path, example ' a ' or ' a/test.txt ' or ' test.txt ' *@ _zipname is the compressed package name, can use the path, example ' A/test.zip ' or ' test.zip ' **/class creatzip {private $_dir; Private $_zipdir; Private $_zipname; Public function __construct ($dir, $zipName) {$this->_dir = $dir; $this->_zipdir = basename ($dir); $this->_zipname = $zipName; $this->mkdirs (); $this->creat (); }//Detect and generate the directory private Function mkdirs () {if (!is_dir (dirname ($this->_zipname))) {$arr = explode ('/', dirname ($this->_zipname)); $arrs = "; foreach ($arr as $value) {if (!is_dir ($arrs. $value)) {if (!! mkdir ($arrs. $value)) {$arrs. = $value. ' /'; }}}}}//Generate Zip Compressed package private Function creat () {$zip = new ziparchive; if (Is_dir ($this->_dir)) {$this->reaDdir ($this->_dir, $files); if ($zip->open ($this->_zipname,ziparchive::create)) {foreach ($files as $value) { Preg_match ('///('. $this->_zipdir. ') /.*)/', $value, $match); if (Is_dir ($value)) {@ $zip->addemptydir ($value, $match [1]); } else {@ $zip->addfile ($value, $match [1]); }} $zip->close (); }} else {if ($zip->open ($this->_zipname,ziparchive::create)) {$zip->addfile ($ This->_dir, basename ($this->_dir)); }}}//Read folder All files Private function ReadDir ($dir,& $arr) {if ($dirs = Opendir ($dir)) { while ($file =readdir ($dirs)) = False) {if ($file = = '. ' | | $file = = ' ... ') continue; $files = $dir. ' /'. $file; if (Is_dir ($files) && $this->iSempty ($files)) {$this->readdir ($files, $arr); } else {$arr [] = $files; }}} closedir ($dirs); }//Determine if the folder is empty private function IsEmpty ($dir) {if ($_dirs = Opendir ($dir)) {while (($file =readdir ($dirs)) ! = False) {if ($file! = '. ' && $file! = ') {Closedir ($dirs); return true; Break }} closedir ($dirs); return false; } }}
The above describes the PHP generated Zip compressed file method, supporting file and compression package path lookup, including read the contents of the file, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!
Related articles:
How to use PHP to generate zip compressed files with detailed code
PHP implementation of online decompression zip archive
Simple PHP method for creating Zip compressed files