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.