如何用PHP解压缩.gz文件?

时间:2010-07-20 18:30:14

标签: php codeigniter unzip

我正在使用CodeIgniter,我无法弄清楚如何解压缩文件!

6 个答案:

答案 0 :(得分:41)

PHP本身有许多处理gzip文件的功能。

如果你想创建一个新的未压缩文件,它就是这样的。

注意:这不会检查目标文件是否首先存在,不会删除输入文件,也不会执行任何错误检查。在生产代码中使用它之前,你真的应该修复它们。

// This input should be from somewhere else, hard-coded in this example
$file_name = 'file.txt.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);

注意:这仅适用于gzip 。它不涉及焦油。

答案 1 :(得分:3)

使用Zlib Compression扩展程序实现的功能。

此代码段显示了如何使用扩展程序中提供的一些功能:

// open file for reading
$zp = gzopen($filename, "r");

// read 3 char
echo gzread($zp, 3);

// output until end of the file and close it.
gzpassthru($zp);
gzclose($zp);

答案 2 :(得分:3)

如果您有权访问system():

system("gunzip file.sql.gz");

答案 3 :(得分:2)

Download the Unzip library 并包含或autoload unzip

$this->load->library('unzip');

答案 4 :(得分:0)

gzopen太多了。这更直观:

$zipped = file_get_contents("foo.gz");
$unzipped = gzdecode($zipped);

当服务器还吐出压缩后的数据时,它可以在http页面上工作。

答案 5 :(得分:-1)

使用extractTo function from PharData

hasproperty(X, wet) :- hasproperty(X, was_in_rain). hasproperty(X, wet) :- hasproperty(X, washed). 是您要解压缩的.gz存档文件,而$source是要将其解压缩到的目录。

$destDir
相关问题