15

我正在使用 CodeIgniter,但我不知道如何解压缩文件!

4

5 回答 5

48

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 。它不处理焦油。

于 2010-07-20T18:45:02.827 回答
9

gzopen 的工作量太大了。这更直观:

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

当服务器也吐出 gzip 压缩数据时,可以在 http 页面上工作。

于 2019-11-21T13:09:36.517 回答
5

如果您有权访问 system():

system("gunzip file.sql.gz");
于 2013-11-24T20:00:34.257 回答
3

使用Zlib 压缩扩展实现的功能。

这个片段展示了如何使用扩展提供的一些功能:

// 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);
于 2010-07-20T18:36:23.670 回答
2

下载解压缩库并 包含或autoloadunzip

$this->load->library('unzip');
于 2010-07-20T18:32:44.167 回答