比較兩個 ZIP 壓縮檔內容差異,回傳新增、刪除、相同檔案清單,支援單層結構。
ZipCompare<?php
class ZipCompare{
public function compare($zipfile1, $zipfile2){
if(!$this->check_handler()) throw new Exception('unzip not install');
if(!$this->check($zipfile1) || !$this->check($zipfile2)) throw new Exception('zipfile not exists or error');
$zipinfo1 = $this->getInfo($zipfile1);
$zipinfo2 = $this->getInfo($zipfile2);
return $this->parse($zipinfo1, $zipinfo2);
}
private function getInfo($zipfile){
$fields = array('Length','Method','Size','Cmpr','Date','Time','CRC-32','Name');
exec("unzip -v ".escapeshellarg($zipfile), $output);
// 解析 $output 取得檔案清單...
}
private function parse($info1,$info2){
// 比較邏輯:以檔案名+CRC32 判斷同一檔案
// 回傳 array('added'=>[], 'removed'=>[], 'same'=>[])
}
private function check($zipfile){ return file_exists($zipfile) && is_readable($zipfile); }
private function check_handler(){ return @exec('which unzip') !== ''; }
}
?>
unzip 指令。