This article describes the PHP implementation of the ZIP file content comparison class. is a very useful PHP class file. Share to everyone for your reference. The specific analysis is as follows:
The php zip file comparison class is primarily implemented to compare the contents of two zip files, returning new, deleted, 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.
The ZipCompare.class.php class files are as follows:
<?php/** zip Compare class compares the contents of two zip files, returns new, deleted, and the same file list, temporarily supports only single layer * date:2014-05-18 * author:fdipzone * Ver: 1.0 * * Func: * Public Compare comparison zip file content * Private getInfo get zip file list * Private Parse Analysis two zip file contents * Private check check zip file is correct * Private Check_handler check if the server is installed Unzip/class zipcompare{//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 */PU Blic function Compare ($zipfile 1, $zipfile 2) {//Check for installation unzip if (! $this->check_handler ()) {throw new
Exception (' Unzip not install '); //Check ZIP file if (! $this->check ($zipfile 1) | |! $this->check ($zipfile 2)) {throw new Exception (' Zipfi
Le not exists or error ');
//Get file list in Zip $zipinfo 1 = $this->getinfo ($zipfile 1);
$zipinfo 2 = $this->getinfo ($zipfile 2); Analyzes two zip file contents, returns the same and different file list return $this->parse ($zipinfo 1, $zipinfo2); /** Get zip file list * @param String $zipfile zip file * @return file list in Array Zip * * Private Function GetInfo ($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 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 $zipinFo2 * @return Array/Private Function parse ($zipinfo 1, $zipinfo 2) {$result = Array (' Add ' =>
Array (),//Add ' del ' => Array (),//missing ' match ' => Array ()//Match); if ($zipinfo 1 && $zipinfo 2) {//in ZIP1 but not zip2 file $result [' add '] = Array_values (Array_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 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 is installed Unzip * @return Boolean/Private Function Check_handler () {return strstr shell_exec
(' Unzip-v '), ' version ')!= ';
}//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< C28/>[6] => 15.jpg
[7] => 16.jpg
[8] => 17.jpg
[9] => 18.jpg
[ten] => 2.jpg
[11] = > 3.jpg
[[] => 4.jpg
)
]
Full instance code click here to download the site.
I hope this article will help you with the learning of PHP programming.