如何使用php提取或解压缩gzip文件?

时间:2012-06-29 16:57:01

标签: php gzip compression

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

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

我试过这段代码,但这不起作用,我得到了:

  

内部服务器错误
  服务器遇到内部错误或配置错误,无法完成您的请求。请联系服务器管理员webmaster@domain.com并告知他们错误发生的时间,以及可能导致错误的任何操作。服务器错误日志中可能提供了有关此错误的更多信息   此外,尝试使用ErrorDocument处理请求时遇到404 Not Found错误。

1 个答案:

答案 0 :(得分:63)

尝试找到here

//This input should be from somewhere else, hard-coded in this example
$file_name = '2013-07-16.dump.gz';

// Raising this value may increase performance
$buffer_size = 4096; // read 4kb at a time
$out_file_name = str_replace('.gz', '', $file_name); 

// Open our files (in binary mode)
$file = gzopen($file_name, 'rb');
$out_file = fopen($out_file_name, 'wb'); 

// Keep repeating until the end of the input file
while (!gzeof($file)) {
    // Read buffer-size bytes
    // Both fwrite and gzread and binary-safe
    fwrite($out_file, gzread($file, $buffer_size));
}

// Files are done, close files
fclose($out_file);
gzclose($file);