Generate Zip Compressed files from PHP, support file and archive path lookup

Source: Internet
Author: User
Tags ziparchive
/** 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

  • Related Article

    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.