Home > Backend Development > PHP Tutorial > Generate zip compressed files through php, supporting file and compressed package path search

Generate zip compressed files through php, supporting file and compressed package path search

PHP中文网
Release: 2023-02-28 22:40:02
Original
1984 people have browsed it
/*
*   new creatZip($_dir,$_zipName);
*@ _dir是被压缩的文件夹名称,可使用路径,例 'a'或者'a/test.txt'或者'test.txt'
*@ _zipName是压缩后的压缩包名称,可使用路径,例 'a/test.zip'或者'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();
    }
    //检测并生成目录
    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.'/';
                    }
                }
            }
        }
    }
    //生成Zip压缩包
    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));
            }
        }
    }
    //读取文件夹所有文件
    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);
    }
    //判断文件夹是否为空
    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;
        }
    }
}
Copy after login

The above introduces the method of generating zip compressed files in PHP, which supports file and compressed package path search, including content on reading files. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Related articles:

How to use php to generate zip compressed files with detailed code

php realizes online decompression of zip compressed files

Simple php method to create zip compressed files

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template