Php method for generating Zip files, supporting file and compressed package path lookup

Source: Internet
Author: User
Tags ziparchive

/** New Creatzip ($_dir,$_zipname), *@ _dir is the compressed folder name, 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 ' **/classCreatzip {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 (); }    //Detecting and generating catalogs    Private functionmkdirs () {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 a ZIP archive package    Private functioncreat () {$zip=Newziparchive; 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 functionIsEmpty ($dir) {        if($_dirs=Opendir($dir)) {             while(($file=Readdir($dirs)) !=false) {                if($file! = '. ' &&$file! = ' ... ') {                    Closedir($dirs); return true;  Break; }            }            Closedir($dirs); return false; }    }}

Reprint Please specify source: http://www.ly89.cn/detailB/41.html

Php method for generating Zip files, supporting file and compressed package path lookup

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.