在内存中下载并解压 zip 存档
我想下载一个 zip 存档并使用 PHP 将其解压缩到内存中。
这就是我今天所拥有的(对我来说文件处理太多了:)):
// download the data file from the real page
copy("http://www.curriculummagic.com/AdvancedBalloons.kmz", "./data/zip.kmz");
// unzip it
$zip = new ZipArchive;
$res = $zip->open('./data/zip.kmz');
if ($res === TRUE) {
$zip->extractTo('./data');
$zip->close();
}
// use the unzipped files...
I would like to download a zip archive and unzip it in memory using PHP.
This is what I have today (and it's just too much file-handling for me :) ):
// download the data file from the real page
copy("http://www.curriculummagic.com/AdvancedBalloons.kmz", "./data/zip.kmz");
// unzip it
$zip = new ZipArchive;
$res = $zip->open('./data/zip.kmz');
if ($res === TRUE) {
$zip->extractTo('./data');
$zip->close();
}
// use the unzipped files...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
警告:这不能在内存中完成 -
ZipArchive
无法使用“内存映射文件”。您可以使用
file_get_contents
文档,因为它支持zip://
流包装器Docs:您只能使用
zip://
或通过ZipArchive访问本地文件。为此,您可以首先将内容复制到临时文件并使用它:Warning: This cannot be done in memory —
ZipArchive
cannot work with "memory mapped files".You can obtain the data of a file inside a zip-file into a variable (memory) with
file_get_contents
Docs as it supports thezip://
Stream wrapper Docs:You can only access local files with
zip://
or via ZipArchive. For that you can first copy the contents to a temporary file and work with it:就像这样简单:
As easy as:
老话题但仍然相关,因为我问自己同样的问题,但没有找到答案。
我最终编写了这个函数,它返回一个数组,其中包含存档中包含的每个文件的名称以及该文件的解压缩内容:
Old subject but still relevant since I asked myself the same question, without finding an answer.
I ended up writing this function which returns an array containing the name of each file contained in the archive, as well as the decompressed contents of that file:
您可以获取 zip 内文件的流并将其提取到变量中:
You can get a stream to a file inside the zip and extract it into a variable:
如果您可以使用系统调用,最简单的方法应该如下所示(bzip2 情况)。您只需使用标准输出即可。
If you can use system calls, the simplest way should look like this (bzip2 case). You just use stdout.