1、使用PHP執行文件解壓縮zip文件,前提條件,一定要確定服務器開啟了zip拓展
2、封裝的方法如下:
實例代碼
1 <?php 2 /** 3 * 壓縮文件 4 * @param array $files 待壓縮文件 array('d:/test/1.txt','d:/test/2.jpg');【文件地址為絕對路徑】 5 * @param string $filePath 輸出文件路徑 【絕對文件地址】 如 d:/test/new.zip 6 * @return string|bool 7 */ 8 function zip($files, $filePath) { 9 //檢查參數 10 if (empty($files) || empty($filePath)) { 11 return false; 12 } 13 14 //壓縮文件 15 $zip = new ZipArchive(); 16 $zip->open($filePath, ZipArchive::CREATE); 17 foreach ($files as $key => $file) { 18 //檢查文件是否存在 19 if (!file_exists($file)) { 20 return false; 21 } 22 $zip->addFile($file, basename($file)); 23 } 24 $zip->close(); 25 26 return true; 27 } 28 29 /** 30 * zip解壓方法 31 * @param string $filePath 壓縮包所在地址 【絕對文件地址】d:/test/123.zip 32 * @param string $path 解壓路徑 【絕對文件目錄路徑】d:/test 33 * @return bool 34 */ 35 function unzip($filePath, $path) { 36 if (empty($path) || empty($filePath)) { 37 return false; 38 } 39 40 $zip = new ZipArchive(); 41 42 if ($zip->open($filePath) === true) { 43 $zip->extractTo($path); 44 $zip->close(); 45 return true; 46 } else { 47 return false; 48 } 49 } 50 ?>