What is suitable buffer size for uncompressing large gzip files in gzopen using php?

两盒软妹~` 提交于 2019-12-07 22:42:54

问题


function uncompress($srcName, $dstName) {
    $sfp = gzopen($srcName, "rb");
    $dstName = str_replace('.gz', '', $dstName);
    $fp = fopen($dstName, "w");

        fseek($FileOpen, -4, SEEK_END);
        $buf = fread($FileOpen, 4);
        $GZFileSize = end(unpack("V", $buf));

    while ($string = gzread($sfp, $GZFileSize)) {
        fwrite($fp, $string, strlen($string));
    }
    gzclose($sfp);
    fclose($fp);
}

I use this code for uncompressing but It does not work and I get following error:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, webmaster@example.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.


回答1:


This should help you see the error messages. Either it will displayed on the screen or will be printed into the txt file, although the directory must be writable by php.

<?php //top of script
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', 'errors.txt');



回答2:


This function looks sketchy. It seems you're reimplementing stream_copy_to_stream() in userland. Forget about buffers and simply use the native stuff.

function uncompress($srcName, $dstName)
{
    $src = gzopen($srcName, 'rb');
    $dst = fopen($dstName, 'wb');

    stream_copy_to_stream($src, $dst);

    gzclose($src);
    fclose($dst);
}

Come to think of it, you could probably even use copy()

function uncompress($srcName, $dstName)
{
    copy('compress.zlib://' . $srcName, $dstName);
}



回答3:


I changed my function to this and my problem was solved :

function uncompress($srcName, $dstName) {
        $sfp = gzopen($srcName, "rb");
        $dstName = str_replace('.gz', '', $dstName);
        $fp = fopen($dstName, "w");


            $GZFileSize = filesize($srcName);


        while ($string = gzread($sfp, $GZFileSize)) {
            fwrite($fp, $string, strlen($string));
        }
        gzclose($sfp);
        fclose($fp);
    }


来源:https://stackoverflow.com/questions/11267311/what-is-suitable-buffer-size-for-uncompressing-large-gzip-files-in-gzopen-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!