Php 文件\u获取与gzip文件等效的内容
与使用Php 文件\u获取与gzip文件等效的内容,php,file,gzip,Php,File,Gzip,与使用gzwrite函数读取文本文件的全部内容的file\u get\u contents函数等效的函数是什么。。还是你的意思 编辑: 如果你不想有一个手柄,使用 我不确定它将如何处理gz文件头。我根据手册中的注释编写了一个我正在寻找的函数: /** * @param string $path to gzipped file * @return string */ public function gz_get_contents($path) { // gzread needs th
gzwrite
函数读取文本文件的全部内容的file\u get\u contents
函数等效的函数是什么。。还是你的意思
编辑:
如果你不想有一个手柄,使用
我不确定它将如何处理gz文件头。我根据手册中的注释编写了一个我正在寻找的函数:
/**
* @param string $path to gzipped file
* @return string
*/
public function gz_get_contents($path)
{
// gzread needs the uncompressed file size as a second argument
// this might be done by reading the last bytes of the file
$handle = fopen($path, "rb");
fseek($handle, -4, SEEK_END);
$buf = fread($handle, 4);
$unpacked = unpack("V", $buf);
$uncompressedSize = end($unpacked);
fclose($handle);
// read the gzipped content, specifying the exact length
$handle = gzopen($path, "rb");
$contents = gzread($handle, $uncompressedSize);
gzclose($handle);
return $contents;
}
我尝试了@Sfisioza的答案,但我遇到了一些问题。它还会读取文件两次,一次是非压缩,然后再次是压缩。以下是一个简明版本:
public function gz_get_contents($path){
$file = @gzopen($path, 'rb', false);
if($file) {
$data = '';
while (!gzeof($file)) {
$data .= gzread($file, 1024);
}
gzclose($file);
}
return $data;
}
这是一种更容易使用的方法
谢谢,但是file\u get\u contents将文件路径而不是句柄作为参数。gzread使用资源,而不是路径,因此这不是等效的。
readgzfile
输出到stdout,而不是字符串。但是使用gzread对我来说很有效。例如,$zd=gzopen($fname,“r”)$gz=gzread($zd,100*1024*1024);gzclose(兹德)代码>(我将最大文件大小设置为100MB;如果这还不够,请进行调整。)这对我不起作用(PHP5.5.9,Mint17);我得到一个0字节的文件。
public function gz_get_contents($path){
$file = @gzopen($path, 'rb', false);
if($file) {
$data = '';
while (!gzeof($file)) {
$data .= gzread($file, 1024);
}
gzclose($file);
}
return $data;
}
file_get_contents('compress.zlib://'.$file);