Use php to generate zip files and support file and compressed package path searching

Source: Internet
Author: User
Tags ziparchive
This article describes how to generate zip files in php. it supports file and compressed package path search. For more information about PHP tutorials, see.
/** New creatZip ($ _ dir, $ _ zipName); * @ _ dir is the name of the compressed folder. you can use the path, for example, 'a' or 'a/test.txt'{'test.txt '* @ _ zipName is the name of the compressed package. The path can be used, for example, '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 ();} // detects and generates the directory private functio N 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 the Zip 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-> addFi Le ($ value, $ match [1]) ;}$ zip-> close ();}} else {if ($ zip-> open ($ this-> _ zipName, ZipArchive: CREATE) {$ zip-> addFile ($ this-> _ dir, basename ($ this-> _ dir) ;}}// read all files in the folder. 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);} // determines whether 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 how to generate zip compressed files in php, and supports file and compressed package path searching, including reading file content. For more information, see PHP Chinese network (www.php1.cn )!

Related articles:

Use php to generate zip compressed files with detailed code

Php implements online decompression of zip files

Simple php method for creating zip files

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.