Phpyield first experience, recursively traverse folders and compress

Source: Internet
Author: User
Tags ziparchive
Php traverses a folder and compresses it to zipprivatefunctionzip ($ path, $ zipFile) {$ zipnewZipArchive (); $ zip-& amp; gt; open ($ zipFile, ZipArchive: CREATE ); create an empty zip file... php traverses a folder and compresses it to zip

Php code

Private function zip ($ path, $ zipFile) {$ zip = new ZipArchive (); $ zip-> open ($ zipFile, ZipArchive: CREATE ); // create an empty zip file $ this-> addFileToZip ($ path, $ zip);} private function addFileToZip ($ path, ZipArchive $ zip, $ root = '') {if (! Is_dir ($ path) {return false;} if (! $ Root) {$ root = $ path;} if (strpos ($ path, $ root )! = 0) {$ root = $ path;} $ handler = opendir ($ path); // specify $ path to open the current folder. While ($ filename = readdir ($ handler ))! = False) {if ($ filename! = "." & $ Filename! = ".. ") {// The folder name is '. 'and '.. ', do not operate on them if (is_dir ($ path. "/". $ filename) {// recursive $ this-> addFileToZip ($ path. "/". $ filename, $ zip, $ root);} else {// add the file to the zip object $ filenameWithPath = $ path. "/". $ filename; $ localFileName = substr ($ filenameWithPath, strlen ($ root); $ zip-> addFile ($ filenameWithPath, $ localFileName );}}} @ closedir ($ handler );}

Use yield to refactor the code

Php code

Private function zipFolder ($ folder, $ zipFile) {$ zip = new ZipArchive (); $ zip-> open ($ zipFile, ZipArchive: CREATE ); // create an empty zip file foreach ($ this-> yieldFile ($ folder) as $ file) {$ localFileName = substr ($ file, strlen ($ folder )); $ zip-> addFile ($ file, $ localFileName) ;}} private function yieldFile ($ path) {if (is_dir ($ path )) {$ handler = opendir ($ path); while ($ filename = readdir ($ handler ))! = False) {if ($ filename! = "." & $ Filename! = ".. ") {// The folder name is '. 'and '.. ', do not operate on them if (is_dir ($ path. "/". $ filename) {// recursive foreach ($ this-> yieldFile ($ path. "/". $ filename) as $ file) {yield $ file ;}} else {// add the file to the zip object $ file = $ path. "/". $ filename; yield $ file ;}} closedir ($ handler );}}

Code Execution

Php code

  public function anyZip(){        for($i=0;$i<6;$i++) {            $start = microtime(true);            $toPath = 'D:/unzip';            $this->zipFolder($toPath, 'd:/zip/123.zip');            $end = microtime(true);            echo '|zipFolder-delay:' . ($end - $start);            $start = microtime(true);            $toPath = 'D:/unzip';            $this->zip($toPath, 'd:/zip/124.zip');            $end = microtime(true);            echo '|zip-delay:' . ($end - $start) . '
'; } }

| ZipFolder-delay: 1.6427090167999 | zip-delay: 1.6077039241791
| ZipFolder-delay: 1.6132049560547 | zip-delay: 1.6287071704865
| ZipFolder-delay: 1.6342070102692 | zip-delay: 1.6152048110962
| ZipFolder-delay: 1.6917150020599 | zip-delay: 1.6022040843964
| ZipFolder-delay: 1.6297070980072 | zip-delay: 1.7262189388275
| ZipFolder-delay: 1.5997030735016 | zip-delay: 1.5892019271851

The time difference between yield recursion and normal recursive execution is not big. The main advantage is to split data acquisition and data processing, making it easier to understand and reuse.

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.