gpt4 book ai didi

php - 在内存中下载并解压缩 zip 存档

转载 作者:可可西里 更新时间:2023-10-31 23:07:38 26 4
gpt4 key购买 nike

我想下载一个 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...

最佳答案

警告:这不能在内存中完成 — ZipArchive 不能使用“内存映射文件”

您可以使用file_get_contentsDocs 将zip 文件中的文件数据获取到变量(内存)中。因为它支持 zip:// Stream wrapper Docs :

$zipFile = './data/zip.kmz';     # path of zip-file
$fileInZip = 'test.txt'; # name the file to obtain

# read the file's data:
$path = sprintf('zip://%s#%s', $zipFile, $fileInZip);
$fileData = file_get_contents($path);

您只能使用 zip:// 或通过 ZipArchive 访问本地文件。为此,您可以先将内容复制到一个临时文件并使用它:

$zip = 'http://www.curriculummagic.com/AdvancedBalloons.kmz';
$file = 'doc.kml';

$ext = pathinfo($zip, PATHINFO_EXTENSION);
$temp = tempnam(sys_get_temp_dir(), $ext);
copy($zip, $temp);
$data = file_get_contents("zip://$temp#$file");
unlink($temp);

关于php - 在内存中下载并解压缩 zip 存档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7391969/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com