PHPzip file comparison class, compares the content of two zip files, returns the add, delete, and the same file list. Currently, only one layer is supported. Php zip file comparison class, compares the content of two zip files, returns the add, delete, and the same file list. Currently, only one layer is supported.
Requirements:Upload a zip file, which contains many image files. You need to process a series of time-consuming image files. When the user updates the zip file again. Determine whether the files in the zip file are consistent and only process different files. This saves resources and time, so you need to write a class that can compare zip files.
ZipCompare. class. php
Check_handler () {throw new Exception ('Unzip not install');} // Check the zip file if (! $ This-> check ($ zipfile1) |! $ This-> check ($ zipfile2) {throw new Exception ('zipfile not exists or error ');} // Get the zip file list $ zipinfo1 = $ this-> getInfo ($ zipfile1); $ zipinfo2 = $ this-> getInfo ($ zipfile2 ); // analyze the content of two zip files and return the same and different file lists. return $ this-> parse ($ zipinfo1, $ zipinfo2 );} /** get the zip file list * @ param String $ zipfile zip file * @ return Array zip file list */private function getInfo ($ zipfile) {// unzip-v fields $ fields = array ('length ',' Method ', 'size', 'cmpps', 'date', 'time', 'crc-32', 'name '); // zip verbose $ verbose = shell_exec (sprintf ("unzip-v % s | sed '\ $ d' | sed-n' 4, \ $ p '", $ zipfile); // zip info $ zipinfo = array (); $ filelist = explode (" \ n ", $ verbose ); if ($ filelist) {foreach ($ filelist as $ rowdata) {if ($ rowdata = '') {continue ;} $ rowdata = preg_replace ('/[] {2,}/', '', $ rowdata); // replace two or more spaces with one $ tmp = array_slic E (explode ('', $ rowdata), 1); // remove the first space $ file = array_combine ($ fields, $ tmp ); $ zipinfo [$ file ['name'] = $ file ['length']. '_'. $ file ['crc-32']; // file name, length, CRC32, for verification} return $ zipinfo ;} /** analyze the content of two zip files * @ param String $ zipinfo1 * @ param String $ zipinfo2 * @ return Array */private function parse ($ zipinfo1, $ zipinfo2) {$ result = array ('add' => array (), // add 'Del '=> array (), // 'match' => array is missing () // Match); if ($ zipinfo1 & $ zipinfo2) {// in zip1 but not zip2, $ result ['add'] = array_values (array_diff (array_keys ($ zipinfo1), array_keys ($ zipinfo2 ))); // In The zip2 but not zip1 file $ result ['Del '] = array_values (array_diff (array_keys ($ zipinfo2), array_keys ($ zipinfo1 ))); // The zip1 and zip2 files at the same time $ match_file = array_values (array_diff (array_keys ($ zipinfo1), $ result ['add']); // check whether the content of the file with the same file name matches for ($ I = 0, $ len = count ($ match _ File); $ I <$ len; $ I ++) {if ($ zipinfo1 [$ match_file [$ I] ==$ zipinfo2 [$ match_file [$ I]) {// match array_push ($ result ['match'], $ match_file [$ I]);} else {// not match, change to add array_push ($ result ['add'], $ match_file [$ I]);} return $ result;}/** check whether the zip file is correct * @ param String $ zipfile zip file * @ return boolean */private function check ($ zipfile) {// The file exists and can be decompressed return file_exists ($ zipfile) & shell_exec (Sprintf ('Unzip-v % s | wc-L', $ zipfile)> 1 ;} /** check whether unzip * @ return boolean */private function check_handler () {return strstr (shell_exec ('Unzip-v'), 'version') is installed on the server ')! = '';}} // Class end?>
Demo
compare('test1.zip','test2.zip');print_r($result);?>
Output after execution:
Array( [add] => Array ( [0] => 9.jpg ) [del] => Array ( [0] => 5.jpg [1] => 6.jpg [2] => 7.jpg [3] => 8.jpg ) [match] => Array ( [0] => 1.jpg [1] => 10.jpg [2] => 11.jpg [3] => 12.jpg [4] => 13.jpg [5] => 14.jpg [6] => 15.jpg [7] => 16.jpg [8] => 17.jpg [9] => 18.jpg [10] => 2.jpg [11] => 3.jpg [12] => 4.jpg ))
The above is the content of the php zip file comparison class. For more information, see The PHP Chinese website (www.php1.cn )!