dtvfxzq3802
2009-08-04 19:53
浏览 106
已采纳

使用PHP中的gzip解压缩大文件

I'm using a simple unzip function (as seen below) for my files so I don't have to unzip files manually before they are processed further.

function uncompress($srcName, $dstName) {
    $string = implode("", gzfile($srcName));
    $fp = fopen($dstName, "w");
    fwrite($fp, $string, strlen($string));
    fclose($fp);
} 

The problem is that if the gzip file is large (e.g. 50mb) the unzipping takes a large amount of ram to process.

The question: can I parse a gzipped file in chunks and still get the correct result? Or is there a better other way to handle the issue of extracting large gzip files (even if it takes a few seconds more)?

图片转代码服务由CSDN问答提供 功能建议

我正在为我的文件使用一个简单的解压缩函数(如下所示),所以我不需要解压缩 文件在进一步处理之前手动完成。

  function uncompress($ srcName,$ dstName){
 $ string = implode(“”,gzfile($ srcName)); \  n $ fp = fopen($ dstName,“w”); 
 fwrite($ fp,$ string,strlen($ string)); 
 fclose($ fp); 
} 
   
 
 

问题是如果gzip文件很大(例如50mb),解压缩会占用大量的ram。

问题:我可以 解析一个gzip压缩的文件,仍然得到正确的结果? 或者是否有更好的其他方法来处理提取大型gzip文件的问题(即使它需要几秒钟)?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • doutaoer3148 2009-08-04 20:16
    已采纳

    gzfile() is a convenience method that calls gzopen, gzread, and gzclose.

    So, yes, you can manually do the gzopen and gzread the file in chunks.

    This will uncompress the file in 4kB chunks:

    function uncompress($srcName, $dstName) {
        $sfp = gzopen($srcName, "rb");
        $fp = fopen($dstName, "w");
    
        while (!gzeof($sfp)) {
            $string = gzread($sfp, 4096);
            fwrite($fp, $string, strlen($string));
        }
        gzclose($sfp);
        fclose($fp);
    }
    
    点赞 打赏 评论
  • douzao5487 2009-08-04 20:04

    try with

    function uncompress($srcName, $dstName) {
        $fp = fopen($dstName, "w");
        fwrite($fp, implode("", gzfile($srcName)));
        fclose($fp);
    }
    

    $length parameter is optional.

    点赞 打赏 评论
  • duanlu0075 2009-08-04 20:04

    If you are on a Linux host, have the required privilegies to run commands, and the gzip command is installed, you could try calling it with something like shell_exec

    SOmething a bit like this, I guess, would do :

    shell_exec('gzip -d your_file.gz');
    

    This way, the file wouldn't be unzip by PHP.


    As a sidenote :

    • Take care where the command is run from (ot use a swith to tell "decompress to that directory")
    • You might want to take a look at escapeshellarg too ;-)
    点赞 打赏 评论
  • dongtaotao19830418 2016-11-15 21:00

    As maliayas mentioned, it may lead to a bug. I experienced an unexpected fall out of the while loop, but the gz file has been decompressed successfully. The whole code looks like this and works better for me:

    function gzDecompressFile($srcName, $dstName) { 
        $error = false; 
    
        if( $file = gzopen($srcName, 'rb') ) { // open gz file
    
            $out_file = fopen($dstName, 'wb'); // open destination file
    
            while (($string = gzread($file, 4096)) != '') { // read 4kb at a time   
                if( !fwrite($out_file, $string) ) { // check if writing was successful
                     $error = true;
                }   
            }
    
            // close files
            fclose($out_file);
            gzclose($file);     
    
        } else {
            $error = true;
        }
    
        if ($error)
            return false; 
        else
            return true; 
    } 
    
    点赞 打赏 评论

相关推荐 更多相似问题