【问题标题】:How can I extract or uncompress gzip file using php? [duplicate]如何使用 php 提取或解压缩 gzip 文件? [复制]
【发布时间】:2012-07-01 05:29:29
【问题描述】:
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 错误。

【问题讨论】:

  • 我们需要的不仅仅是“它不起作用”。它有什么作用?您收到什么错误消息?
  • 您的代码看起来不错(它将数据分块为 4kb 块),因此它在 RAM 上应该很轻。您遇到了什么错误?
  • 我什么也得不到,我没有解压缩我的任何文件
  • 我想知道我必须为许多文件使用多少缓冲区

标签: php gzip compression


【解决方案1】:

试试这个发现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);

【讨论】:

  • 对我有很大帮助。工作正常。并且易于使用。
  • 很好的代码,一般。但我不会认为 str_replace(gz) 是理所当然的,因为有时输入文件是 /tmp/ 因为它来自 $_FILES 中的上传
猜你喜欢
  • 1970-01-01
  • 2017-02-01
  • 1970-01-01
  • 2016-11-19
  • 2011-06-14
  • 1970-01-01
  • 1970-01-01
  • 2012-05-13
  • 1970-01-01
相关资源
最近更新 更多