使用 PHP 解压缩较大的文件 [英] Unzipping larger files with PHP

查看:43
本文介绍了使用 PHP 解压缩较大的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 PHP 解压一个 14MB 的档案,代码如下:

I'm trying to unzip a 14MB archive with PHP with code like this:

    $zip = zip_open("c:\kosmas.zip");
    while ($zip_entry = zip_read($zip)) {
    $fp = fopen("c:/unzip/import.xml", "w");
    if (zip_entry_open($zip, $zip_entry, "r")) {
     $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
     fwrite($fp,"$buf");
     zip_entry_close($zip_entry);
     fclose($fp);
     break;
    }
   zip_close($zip);
  }

它在我的本地主机上失败,内存限制为 128MB,经典的 blablabla 字节的允许内存大小已用尽".在服务器上,我有 16MB 的限制,有没有更好的方法来做到这一点,以便我能适应这个限制?我不明白为什么这必须分配超过 128MB 的内存.提前致谢.

It fails on my localhost with 128MB memory limit with the classic "Allowed memory size of blablabla bytes exhausted". On the server, I've got 16MB limit, is there a better way to do this so that I could fit into this limit? I don't see why this has to allocate more than 128MB of memory. Thanks in advance.

解决方案:我开始读取 10Kb 块中的文件,问题解决了峰值内存使用量 1.5MB.

Solution: I started reading the files in 10Kb chunks, problem solved with peak memory usage arnoud 1.5MB.

        $filename = 'c:\kosmas.zip';
        $archive = zip_open($filename);
        while($entry = zip_read($archive)){
            $size = zip_entry_filesize($entry);
            $name = zip_entry_name($entry);
            $unzipped = fopen('c:/unzip/'.$name,'wb');
            while($size > 0){
                $chunkSize = ($size > 10240) ? 10240 : $size;
                $size -= $chunkSize;
                $chunk = zip_entry_read($entry, $chunkSize);
                if($chunk !== false) fwrite($unzipped, $chunk);
            }

            fclose($unzipped);
        }

推荐答案

为什么一次读取整个文件?

Why do you read the whole file at once?

 $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
 fwrite($fp,"$buf");

尝试读取其中的一小部分并将它们写入文件.

Try to read small chunks of it and writing them to a file.

这篇关于使用 PHP 解压缩较大的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆