php zip file comparison class, compare the contents of two zip files, return new, delete, and the same file list. Only one layer is supported for the time being.
requirement: upload a zip file with many picture files inside the zip. A series of time-consuming processing of picture files is required. When the user updates the zip file again. Determine if the files in the zip are consistent and handle only the different files. This saves resources and time, so you need to write a class that compares the files in the zip.
ZipCompare.class.php
<?php/** zip Compare class compares the contents of two zip files, returns new, deleted, and the same file list, temporarily only supports single layer * date:2014-05-18* author:fdipzone* ver:1 .0** func:* Public Compare compare zip file contents * Private GetInfo get a list of files within Zip * Private Parse Analysis two zip file contents * Private check check if zip file is correct * private Check_handler Check server for installation Unzip*/class zipcompare{//class start/** compare Z IP file contents, list different parts * @param string $zipfile 1 zip file 1 * @param string $zipfile 2 zip file 2 * @return Array */PU Blic function Compare ($zipfile 1, $zipfile 2) {//Check if there is an installation unzip if (! $this->check_handler ()) {thro W New Exception (' Unzip not install '); }//Check the zip file if (! $this->check ($zipfile 1) | |! $this->check ($zipfile 2)) {throw new Exception ( ' ZipFile not exists or error '); }//Get a list of files within the zip $zipinfo 1 = $this->getinfo ($zipfile 1); $zipinfo 2 = $this->getinfo ($zipfile 2); Analysis of two zip file contents, return the same and different file list return $this->parSE ($zipinfo 1, $zipinfo 2); /** get the list of files in Zip * @param String $zipfile zip file * @return The list of files within the Array Zip */Private function get Info ($zipfile) {//unzip-v fields $fields = Array (' Length ', ' Method ', ' Size ', ' cmpr ', ' Date ', ' time ', ' CRC-32 ', ' N Ame '); Zip verbose $verbose = shell_exec (sprintf ("Unzip-v%s | Sed ' \ $d ' | 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 above with a $tmp = Array_slice (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 Validation}} return $zipinfO }/** parse two zip file contents * @param string $zipinfo 1 * @param string $zipinfo 2 * @return Array */Private FUNCT Ion Parse ($zipinfo 1, $zipinfo 2) {$result = Array (' add ' = = Array (),//new ' del ' = > Array (),//missing ' match ' = = Array ()//Match); if ($zipinfo 1 && $zipinfo 2) {//in ZIP1 but not in zip2 file $result [' add '] = Array_values (Array_diff (ARRA Y_keys ($zipinfo 1), Array_keys ($zipinfo 2)); Files in Zip2 but not in zip1 $result [' del '] = array_values (Array_diff (Array_keys ($zipinfo 2), Array_keys ($zipinfo 1))); Also in Zip1 and zip2 file $match _file = array_values (Array_diff (Array_keys ($zipinfo 1), $result [' Add ']); Check that the file contents of the same file name match for ($i =0, $len =count ($match _file), $i < $len, $i + +) {if ($zipinfo 1[$m atch_file[$i]]== $zipinfo 2[$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 that the zip file is correct * @param String $zipfile zip file * @return Boolean */Private Function Check ($zipfile) { File exists and can decompress return file_exists ($zipfile) && shell_exec (sprintf (' unzip-v%s | wc-l ', $zipfile) >1; /** Check to see if the server has unzip * @return Boolean */Private Function Check_handler () {return strstr (shell_ex EC (' Unzip-v '), ' version ')! = '; }}//Class end?>
Demo
<?phprequire "ZipCompare.class.php"; $obj = new Zipcompare (); $result = $obj->compare (' test1.zip ', ' test2.zip '); Print_r ($result);? >
Post-Execution output:
Array ([ add] = array ( [0] = 9.jpg ) [del] = = array ( [0] = 5.jpg [1] =& Gt 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 [ten] = 2.jpg [one] = 3.jpg [] = 4.jpg ))
This article explains about the php zip file content comparison class, more relevant content please focus on PHP Chinese web.
Related recommendations:
How to get/set User access Page language class via PHP
Calculates the relative path method between two files via PHP