加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 16:29:32 所属栏目:PHP教程 来源:网络整理
导读:我想下载一个zip存档,并使用 PHP将其解压缩到内存中. 这就是我今天所拥有的(而且对我来说文件处理太多:)): // download the data file from the real pagecopy("http://www.curriculummagic.com/AdvancedBalloons.kmz","./data/zip.kmz");// unzip it$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...
警告:这不能在内存中完成 – 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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读