如何用PHP解压.gz文件?
我正在使用CodeIgniter,但我不知道如何解压缩文件 并包括或如何用PHP解压.gz文件?,php,codeigniter,unzip,Php,Codeigniter,Unzip,我正在使用CodeIgniter,但我不知道如何解压缩文件 并包括或自动加载解压库 $this->load->library('unzip'); 使用扩展实现的函数 此代码段显示如何使用扩展提供的某些函数: // open file for reading $zp = gzopen($filename, "r"); // read 3 char echo gzread($zp, 3); // output until end of the file and close it.
自动加载解压库
$this->load->library('unzip');
使用扩展实现的函数
此代码段显示如何使用扩展提供的某些函数:
// 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);
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。如果您有权访问system():
使用
$source
是要解压缩的.gz存档文件,而$destDir
是要将其解压缩到的目录
$phar = new PharData($source);
$phar->extractTo($destDir,null,true); // extract all files, overwrites existing files
工作太多了。这更直观:
$zipped = file_get_contents("foo.gz");
$unzipped = gzdecode($zipped);
当服务器吐出Gzip数据时,也可以在http页面上工作。一个.gz文件与一个.zip文件不同,即使通常一个能够解压缩.zip文件的实用程序也能够解压缩.gz文件。应该可以工作,但大多数时候由于安全原因,system()将被禁用链接已失效。。让我找到链接,如果我可以和更新这个我用了这个代码,它的工作完善。只是询问如何使其与gz文件内的文件夹一起工作,以及如何显示提取文件的输出文件夹。@RealMan gzip只支持单个文件。对于多个文件,您需要一个.tar.gz。我不确定PHP是否内置了对tar的支持。
$zipped = file_get_contents("foo.gz");
$unzipped = gzdecode($zipped);