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 temporarily.
Requirements: Upload a zip file, there are many picture files in the zip. A series of time-consuming processing of picture files is required. When the user updates the zip file again. Determine if the file in the zip is consistent and only handle different files. This saves resources and time, so you need to write a class that compares 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 supports single layer * DATE:2014-05-18 * author:fdipzone * ver:1.0 * * Func: * Public Compare Compare zip file contents * Private getInfo Get zip file list * Private parse Analyze two zip file contents * Private Check check Zip file is correct * Private Check_handler check if the server is installed Unzip/class Zip compare{//class start/** compare the contents of the zip file, listing the different parts * @param string $zipfile 1 zip file 1 * @param string $zipfile 2 zip file 2 * @return Array */Public Function compare ($zipfile 1, $zipfile 2) {//check whether
There are installation unzip if (! $this->check_handler ()) {throw new Exception (' Unzip not install '); //Check the zip file if (! $this->check ($zipfile 1) | |! $this->check ($zipfile 2)) {thro
W New Exception (' ZipFile not exists or error ');
//Get file list in Zip $zipinfo 1 = $this->getinfo ($zipfile 1); $zipinfo 2 = $this->getinfo ($zipfile 2);
Analyze the contents of two zip files, return the same and different file list returns $this->parse ($zipinfo 1, $zipinfo 2);
/** Get zip file list * @param String $zipfile zip file * @return Array ZIP file list * * Private Function GetInfo ($zipfile) {//unzip-v fields $fields = Array (' Length ', ' method ', ' S
Ize ', ' cmpr ', ' Date ', ' time ', ' CRC-32 ', ' Name '; Zip verbose//View more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/$verbose = shell_exec (SPR intf ("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 = = ") {con
Tinue; } $rowdata = Preg_replace ('/[]{2,}/', ', ', $roWdata); Replace two or more of the 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 Verification}} return $zipinfo;
/** Analysis two zip file contents * @param string $zipinfo 1 * @param String $zipinfo 2 * @return Array * * Private Function 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 zip2 file $result [' add '] = array_values (ar
Ray_diff (Array_keys ($zipinfo 1), Array_keys ($zipinfo 2))); In Zip2 but not in zip1 file $result [' del '] = Array_valUEs (Array_diff (Array_keys ($zipinfo 2), Array_keys ($zipinfo 1)));
At the same time in zip1 with zip2 documents $match _file = Array_values (Array_diff (Array_keys ($zipinfo 1), $result [' Add ']);
Checks whether 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]]) {//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 Func tion 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 is installed Unzip * @return Boolean/Private Function Check_handler () {return strstr (shell_exec
(' Unzip-v '), ' version ')!= '; }//Class end?>
Demo
<?php
require "ZipCompare.class.php";
$obj = new Zipcompare ();
$result = $obj->compare (' test1.zip ', ' test2.zip ');
Print_r ($result);
? >
Output after execution: