Zip file content comparison class implemented by php,
This example describes the zip file content comparison class implemented by php. Is a very practical PHP class file. Share it with you for your reference. The specific analysis is as follows:
This php zip file comparison class compares the content of two zip files and returns the newly added, deleted, and same file list. Currently, only one layer is supported.
Requirement: Upload a zip file with many image files in it. 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.
The ZipCompare. class. php class file is as follows:
<? Php/** Zip Compare class compares the content of two zip files and returns the add, delete, and same file list. Currently, only the single-layer * Date: * Author: fdipzone * Ver: 1.0 ** Func: * public compare compares the zip file content * private getInfo obtains the zip file list * private parse analyzes the content of the two zip files * private check checks whether the zip file is correct * private check_handler checks whether the server is installed unzip */class ZipCompare {// class start/** compares the content of the zip file, list different parts * @ param String $ zipfile1 zip file 1 * @ param String $ zipfile2 zip file 2 * @ Return Array */public function compare ($ zipfile1, $ zipfile2) {// check whether unzip if (! $ This-> 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?>
The demo sample program is as follows:
<?php require "ZipCompare.class.php"; $obj = new ZipCompare(); $result = $obj->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 ) )
Click here to download the complete instance code.
I hope this article will help you learn PHP programming.