PHP Zip file content comparison class,
A PHP implementation of the ZIP file content comparison class and its usage, you can compare the contents of two zip files, return the new, deleted, and the same file list.
PHP implementation of the ZIP file content comparison class. is a very useful PHP class file.
The php zip file comparison class is primarily implemented to compare the contents of two zip files, returning additions, deletions, 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.
The ZipCompare.class.php class file is as follows:
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 * Www.jbxue . com* Func: * Public Compare Compare zip file contents * Private GetInfo Get zip inside file list * Private Parse Analysis two zip file contents * Private Check Check zip file Correct * Private Check_handler check if the server is installed unzip*/ classzipcompare{//class Start/** Compare zip file contents, list different parts * @param string $zipfile 1 zip file 1 * @param string $zipfile 2 zip file 2 * @return Array*/ Public functionCompare$zipfile 1,$zipfile 2){ //Check if there is an installation unzipif(!$this-Check_handler ()) { Throw New Exception(' Unzip not install '); } //Check the zip fileif(!$this->check ($zipfile 1) || !$this->check ($zipfile 2)){ Throw New Exception(' ZipFile not exists or error '); } //get a list of files within a 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 listsreturn $this->parse ($zipinfo 1,$zipinfo 2); } /** Get a list of files in Zip * @param String $zipfile zip file * @return The list of files in the Array zip*/ Private functionGetInfo ($zipfile){ //unzip-v Fields$fields=Array(' Length ', ' Method ', ' Size ', ' cmpr ', ' Date ', ' time ', ' CRC-32 ', ' Name '); //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 an overhead cell with a$tmp=Array_slice(Explode(' ',$rowdata), 1);//Get rid of the first space$file=Array_combine($fields,$tmp); $zipinfo[$file[' Name ']] =$file[' Length ']. ' _'.$file[' CRC-32 '];//file name, length, CRC32, for verifying} } return $zipinfo; } /** Analysis of two zip file contents * @param string $zipinfo 1 * @param string $zipinfo 2 * @return Array*/ Private functionParse$zipinfo 1,$zipinfo 2){ $result=Array( ' Add ' =Array(),//New' del ' + =Array(),//missing' Match ' =Array()//Match); if($zipinfo 1&&$zipinfo 2){ //files in Zip1 but not in Zip2$result[' add '] =array_values(Array_diff(Array_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 files$match _file=array_values(Array_diff(Array_keys($zipinfo 1),$result[' Add '])); //Check if the file contents of the same file name match for($i=0,$len=Count($match _file);$i<$len;$i++){ if($zipinfo 1[$match _file[$i]]==$zipinfo 2[$match _file[$i]]){//MatchArray_push($result[' Match '],$match _file[$i]); }Else{//Not match, change to addArray_push($result[' Add '],$match _file[$i]); } } } return $result; } /** Check that the zip file is correct * @param String $zipfile zip file * @return Boolean*/ Private functionCheck$zipfile){ //file exists and can be decompressedreturn file_exists($zipfile) &&shell_exec(sprintf(' unzip-v%s | wc-l ',$zipfile)) >1; } /** Check the server for installation unzip * @return Boolean*/ Private functionCheck_handler () {return strstr(shell_exec(' Unzip-v '), ' version ')! = '; } } //class End?>
The Demo sample program is as follows:
require "ZipCompare.class.php"; $obj New $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] = 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] =
)
)
http://www.bkjia.com/PHPjc/884177.html www.bkjia.com true http://www.bkjia.com/PHPjc/884177.html techarticle php zip file Content comparison class, a PHP implementation of the ZIP file content comparison class and its usage, you can compare the contents of two zip files, return new, deleted, and the same file list. P ...