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 ?>
