PHP压缩文件夹(ZIP)
1 <?php
2
3 /**
4 * 添加压缩单/多个文件至压缩包
5 *
6 * @param $zipFile 压缩包名
7 * @param $files array() 文件数组
8 * @param $override 是否覆盖
9 */
10 function compressFilesToZip($zipFile,$files,$override = false) {
11
12 if (empty($zipFile) || empty($files) || gettype($files) != "array" && count($files)==0) {
13 throw new exception("parameters lose !");
14 }
15
16 if(!$override && is_file($zipFile)) {
17 unlink($zipFile);
18 }
19
20 $zip = new ZipArchive;
21 $res = $zip->open($zipFile, ZipArchive::CREATE);
22 if ($res === TRUE) {
23 foreach($files as $file) {
24 if (is_file($file)) {
25 $zip->addFile($file,basename($file));
26 }
27 }
28 $zip->close();
29 } else {
30 throw new exception("create zip failed !");
31 }
32 }
33
34 //compressFiles("abcd.zip",array("xxx.jpg","2-10.txt","E:/xampp/htdocs/rr/admin.php"));
35
36
37 /**
38 * 单文件或文件夹压缩
39 */
40 function Zip($source, $destination = null)
41 {
42 echo $source."<br/>";
43 if (!extension_loaded('zip') || !file_exists($source)) {
44 return false;
45 }
46
47 if (empty($destination)) {
48 $destination = basename($source).".zip";
49 }
50
51 $zip = new ZipArchive();
52 if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
53 return false;
54 }
55
56 $source = str_replace('\\', '/', realpath($source));
57
58 if (is_dir($source) === true)
59 {
60 $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
61
62 foreach ($files as $file)
63 {
64 $file = str_replace('\\', '/', $file);
65
66 // Ignore "." and ".." folders
67 if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )
68 continue;
69
70 $file = realpath($file);
71
72 $file = str_replace("\\","/",$file);
73
74 if (is_dir($file) === true)
75 {
76 $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
77 }
78 else if (is_file($file) === true)
79 {
80 $contextFilePath = substr(str_replace($source, '', $file),1);
81 $zip->addFromString($contextFilePath, file_get_contents($file));
82 }
83 }
84 }
85 else if (is_file($source) === true)
86 {
87 $zip->addFromString(basename($source), file_get_contents($source));
88 }
89 return $zip->close();
90 }
91
92 //切记,目录之间一定要用“/”反斜杠!!!
93 echo Zip("E:/xampp/htdocs/sl","sl2012-09-07.zip");
94
95 ?>
2
3 /**
4 * 添加压缩单/多个文件至压缩包
5 *
6 * @param $zipFile 压缩包名
7 * @param $files array() 文件数组
8 * @param $override 是否覆盖
9 */
10 function compressFilesToZip($zipFile,$files,$override = false) {
11
12 if (empty($zipFile) || empty($files) || gettype($files) != "array" && count($files)==0) {
13 throw new exception("parameters lose !");
14 }
15
16 if(!$override && is_file($zipFile)) {
17 unlink($zipFile);
18 }
19
20 $zip = new ZipArchive;
21 $res = $zip->open($zipFile, ZipArchive::CREATE);
22 if ($res === TRUE) {
23 foreach($files as $file) {
24 if (is_file($file)) {
25 $zip->addFile($file,basename($file));
26 }
27 }
28 $zip->close();
29 } else {
30 throw new exception("create zip failed !");
31 }
32 }
33
34 //compressFiles("abcd.zip",array("xxx.jpg","2-10.txt","E:/xampp/htdocs/rr/admin.php"));
35
36
37 /**
38 * 单文件或文件夹压缩
39 */
40 function Zip($source, $destination = null)
41 {
42 echo $source."<br/>";
43 if (!extension_loaded('zip') || !file_exists($source)) {
44 return false;
45 }
46
47 if (empty($destination)) {
48 $destination = basename($source).".zip";
49 }
50
51 $zip = new ZipArchive();
52 if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
53 return false;
54 }
55
56 $source = str_replace('\\', '/', realpath($source));
57
58 if (is_dir($source) === true)
59 {
60 $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
61
62 foreach ($files as $file)
63 {
64 $file = str_replace('\\', '/', $file);
65
66 // Ignore "." and ".." folders
67 if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )
68 continue;
69
70 $file = realpath($file);
71
72 $file = str_replace("\\","/",$file);
73
74 if (is_dir($file) === true)
75 {
76 $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
77 }
78 else if (is_file($file) === true)
79 {
80 $contextFilePath = substr(str_replace($source, '', $file),1);
81 $zip->addFromString($contextFilePath, file_get_contents($file));
82 }
83 }
84 }
85 else if (is_file($source) === true)
86 {
87 $zip->addFromString(basename($source), file_get_contents($source));
88 }
89 return $zip->close();
90 }
91
92 //切记,目录之间一定要用“/”反斜杠!!!
93 echo Zip("E:/xampp/htdocs/sl","sl2012-09-07.zip");
94
95 ?>