You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用zip://包装器读取嵌套Zip归档文件?

读取嵌套ZIP归档中的文件(无需临时目录)

你的归档结构是 outer-archive.zip > inner-archive.zip > file.xml,使用双层zip://包装器的方式不可行——PHP不支持这种嵌套格式的解析,第二个#会被当作文件名的一部分处理。下面提供无需解压到临时目录的解决方案:

使用ZipArchive分步处理

通过内存流中转内层归档内容,全程在内存中完成读取操作:

$outerZip = new ZipArchive();
if ($outerZip->open('/path/to/outer-archive.zip') === true) {
    // 从外层归档读取内层ZIP的内容
    $innerZipContent = $outerZip->getFromName('inner-archive.zip');
    $outerZip->close();

    // 将内层ZIP内容写入内存临时流
    $tempStream = fopen('php://temp', 'r+');
    fwrite($tempStream, $innerZipContent);
    rewind($tempStream);

    $innerZip = new ZipArchive();
    if ($innerZip->open($tempStream) === true) {
        // 读取目标XML文件内容
        $xmlContent = $innerZip->getFromName('file.xml');
        $innerZip->close();
        fclose($tempStream);

        // 对获取到的XML内容进行后续操作
        var_dump($xmlContent);
    } else {
        fclose($tempStream);
        exit('无法打开内层归档文件');
    }
} else {
    exit('无法打开外层归档文件');
}

说明

  • php://temp 优先在内存中存储数据,当数据量超过内存限制时才会自动使用临时文件(可通过php://temp/maxmemory:NNN自定义内存上限);若希望强制全程在内存中处理,可替换为php://memory(需注意内存溢出风险)。
  • 该方案无需将任何归档文件解压到磁盘,完全通过内存流转完成读取。

内容的提问来源于stack exchange,提问作者BenMorel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 18:32:55