我正在使用CodeIgniter,但是我不知道如何解压文件!
我正在使用CodeIgniter,但是我不知道如何解压文件!
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,不涉及tar。
gzopen太麻烦了。这个更加直观:
$zipped = file_get_contents("foo.gz");
$unzipped = gzdecode($zipped);
当服务器输出压缩数据时,可以在http页面上使用。
gzopen
并使用缓冲区才是正确的方法。 - mbomb007system("gunzip file.sql.gz");
使用由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);
下载Unzip库,并且包含或者自动加载
unzip
库。
$this->load->library('unzip');