PHP implementation of the ZIP file content comparison class

Source: Internet
Author: User
Tags comparison explode php class php programming sprintf zip zipinfo

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 compare zip file contents

* Private GetInfo get a list of files in zip

* Private parse analysis of 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 zip file contents, list 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 to see if there is an 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 (' ZipFile not exists or error ');

}

Get a list of files in zip

$zipinfo 1 = $this->getinfo ($zipfile 1);

$zipinfo 2 = $this->getinfo ($zipfile 2);

Analyze the contents of two zip files to return the same and different list of files

return $this->parse ($zipinfo 1, $zipinfo 2);

}

/** get list of files in zip

* @param String $zipfile zip file

* @return List of files in the 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 empty cells with one

$tmp = Array_slice (Explode (', $rowdata), 1); Remove the first space

$file = Array_combine ($fields, $tmp);

$zipinfo [$file [' Name ']] = $file [' Length ']. ' _ '. $file [' CRC-32 ']; FileName, length, CRC32, for checksums

}

}

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) {

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)));

At the same time in Zip1 and ZIP2 documents

$match _file = array_values (Array_diff (Array_keys ($zipinfo 1), $result [' Add ']);

Check to see 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]]) {//Match

Array_push ($result [' Match '], $match _file[$i]);

}else{//Not match, change to add

Array_push ($result [' Add '], $match _file[$i]);

}

}

}

return $result;

}

/** Check zip file is correct

* @param String $zipfile zip file

* @return Boolean

*/

Private function Check ($zipfile) {

File exists and can be decompressed

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

[6] => 15.jpg

[7] => 16.jpg

[8] => 17.jpg

[9] => 18.jpg

[Ten] => 2.jpg

[One] => 3.jpg

[of] => 4.jpg

)

)

I hope this article will help you with the learning of PHP programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.