PHP:如何解压缩 ZIP 存档
解压缩 ZIP 存档
今天,当您在 Google 中搜索如何用 PHP 解压缩 ZIP 存档时,您会在第一位找到 PECL 库的提取功能。 但是不通过第三方库就可以解压这个标准格式吗? 无论 ! 我向您推荐的就是这个解决方案。 这是出于多种原因:
- 没有额外的库。
- 功能已经集成。
- PHP 4 >= 4.1.0 或 PHP 5 >= 5.1.0 的所有平台上的通用功能
- 得益于适用于 Linux 用户的 apt-get 或适用于其他平台的各种教程,易于安装。
- 限制代码冗余,因为必须说,PECL API 可能更容易理解我们所说的“低级别”代码是一样的,无论我们通过原生 PHP 还是通过 PECL ZipArchive
脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
功能 拉开拉链($path_zip, $tmp_folder = “/tmp/”)
{
$所有文件 = 排列();
$zip = zip_open($path_zip);
if ($zip) {
$stream = zip_read($zip);
而 ($流) {
var_dump($zip_name);
$zip_name = zip_entry_name($stream);
$path_file_unzip = $tmp_folder.$zip_name;
$dir_folder = substr($path_file_unzip, 0, strrpos($path_file_unzip, “/”));
mkdir($dir_文件夹, 0777, true);
$fp = fopen($path_file_unzip, “w”);
array_push($all_files, $path_file_unzip);
if (zip_entry_open($zip, $stream, “r”)){
$buf = zip_entry_read($stream, zip_entry_filesize($stream));
fwrite($fp, “$buff”);
zip_entry_close($stream);
fclose($ fp);
}
$stream = zip_read($zip);
}
zip_close($zip);
}
回报 $所有文件;
}
|