摘要:本文主要向大家介绍了PHP语言 zip文件内容比较类,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。
本文主要向大家介绍了PHP语言 zip文件内容比较类,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。
php zip 文件比较类,比较两个zip文件的内容,返回新增,删除,及相同的文件列表。暂时只支持单层。
需求:上传一个zip文件,zip内有很多图片文件。需要对图片文件进行一系列很耗时的处理。当用户再更新zip文件时。判断zip内文件是否一致,只处理不同的文件。这样可以节省资源与时间,因此需要编写一个能够比较zip内文件的类。
ZipCompare.class.php
[php] view plain copy
1. <?php
2. /** Zip Compare class 比较两个zip文件的内容,返回新增,删除,及相同的文件列表,暂时只支持单层
3. * Date: 2014-05-18
4. * Author: fdipzone
5. * Ver: 1.0
6. *
7. * Func:
8. * public compare 比较zip文件内容
9. * private getInfo 获取zip内文件列表
10. * private parse 分析两个zip的文件内容
11. * private check 检查zip文件是否正确
12. * private check_handler 检查服务器是否有安装unzip
13. */
14.
15. class ZipCompare{ // class start
16.
17. /** 比较zip文件内容,列出不相同的部分
18. * @param String $zipfile1 zip文件1
19. * @param String $zipfile2 zip文件2
20. * @return Array
21. */
22. public function compare($zipfile1, $zipfile2){
23.
24. // 检查是否有安装unzip
25. if(!$this->check_handler()){
26. throw new Exception('unzip not install');
27. }
28.
29. // 检查zip文件
30. if(!$this->check($zipfile1) || !$this->check($zipfile2)){
31. throw new Exception('zipfile not exists or error');
32. }
33.
34. // 获取zip内文件列表
35. $zipinfo1 = $this->getInfo($zipfile1);
36. $zipinfo2 = $this->getInfo($zipfile2);
37.
38. // 分析两个zip的文件内容,返回相同及不同的文件列表
39. return $this->parse($zipinfo1, $zipinfo2);
40.
41. }
42.
43.
44. /** 获取zip内文件列表
45. * @param String $zipfile zip文件
46. * @return Array zip内文件列表
47. */
48. private function getInfo($zipfile){
49.
50. // unzip -v fields
51. $fields = array('Length','Method','Size','Cmpr','Date','Time','CRC-32','Name');
52.
53. // zip verbose
54. $verbose = shell_exec(sprintf("unzip -v %s | sed '\$d' | sed '\$d' | sed -n '4,\$p'", $zipfile));
55.
56. // zip info
57. $zipinfo = array();
58.
59. $filelist = explode("\n", $verbose);
60.
61. if($filelist){
62. foreach($filelist as $rowdata){
63. if($rowdata==''){
64. continue;
65. }
66. $rowdata = preg_replace('/[ ]{2,}/', ' ', $rowdata); // 将两个或以上空格替换为一个
67. $tmp = array_slice(explode(' ', $rowdata), 1); // 去掉第一个空格
68.
69. $file = array_combine($fields, $tmp);
70.
71. $zipinfo[$file['Name']] = $file['Length'].'_'.$file['CRC-32']; // 文件名,长度,CRC32,用于校验
72. }
73. }
74.
75. return $zipinfo;
76.
77. }
78.
79.
80. /** 分析两个zip文件内容
81. * @param String $zipinfo1
82. * @param String $zipinfo2
83. * @return Array
84. */
85. private function parse($zipinfo1, $zipinfo2){
86.
87. $result = array(
88. 'add' => array(), // 新增
89. 'del' => array(), // 缺少
90. 'match' => array() // 匹配
91. );
92.
93. if($zipinfo1 && $zipinfo2){
94.
95. // 在zip1但不在zip2的文件
96. $result['add'] = array_values(array_diff(array_keys($zipinfo1), array_keys($zipinfo2)));
97.
98. // 在zip2但不在zip1的文件
99. $result['del'] = array_values(array_diff(array_keys($zipinfo2), array_keys($zipinfo1)));
100.
101. // 同时在zip1与zip2的文件
102. $match_file = array_values(array_diff(array_keys($zipinfo1), $result['add']));
103.
104. // 检查相同文件名的文件内容是否匹配
105. for($i=0,$len=count($match_file); $i<$len; $i++){
106.
107. if($zipinfo1[$match_file[$i]]==$zipinfo2[$match_file[$i]]){ // match
108. array_push($result['match'], $match_file[$i]);
109. }else{ // not match, change to add
110. array_push($result['add'], $match_file[$i]);
111. }
112.
113. }
114.
115. }
116.
117. return $result;
118.
119. }
120.
121.
122. /** 检查zip文件是否正确
123. * @param String $zipfile zip文件
124. * @return boolean
125. */
126. private function check($zipfile){
127. // 文件存在且能解压
128. return file_exists($zipfile) && shell_exec(sprintf('unzip -v %s | wc -l', $zipfile))>1;
129. }
130.
131.
132. /** 检查服务器是否有安装unzip
133. * @return boolean
134. */
135. private function check_handler(){
136. return strstr(shell_exec('unzip -v'), 'version')!='';
137. }
138.
139. } // class end
140.
141. ?>
demo
[php] view plain copy
1. <?php
2.
3. require "ZipCompare.class.php";
4.
5. $obj = new ZipCompare();
6. $result = $obj->compare('test1.zip','test2.zip');
7.
8. print_r($result);
9.
10. ?>
执行后输出:
[plain] view plain copy
1. Array
2. (
3. [add] => Array
4. (
5. [0] => 9.jpg
6. )
7.
8. [del] => Array
9. (
10. [0] => 5.jpg
11. [1] => 6.jpg
12. [2] => 7.jpg
13. [3] => 8.jpg
14. )
15.
16. [match] => Array
17. (
18. [0] => 1.jpg
19. [1] => 10.jpg
20. [2] => 11.jpg
21. [3] => 12.jpg
22. [4] => 13.jpg
23. [5] => 14.jpg
24. [6] => 15.jpg
25. [7] => 16.jpg
26. [8] => 17.jpg
27. [9] => 18.jpg
28. [10] => 2.jpg
29. [11] => 3.jpg
30. [12] => 4.jpg
31. )
32.
33. )
本文由职坐标整理并发布,了解更多内容,请关注职坐标编程语言PHP频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号