File_get_contents equivalent for gzipped files

What is equivalent to the file_get_contents function, which reads the entire text of a text file written with the gzwrite function?

+4
source share
5 answers

It's easier with streamlined

 file_get_contents('compress.zlib://'.$file); 

fooobar.com/questions/767396 / ...

+5
source

Obviously this will be gzread .. or do you mean file_put_contents ?

Edit: If you do not want to have a descriptor, use readgzfile .

+2
source

I wrote the function I was looking for based on the comments in the manual:

 /** * @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; } 
+1
source

I tried @Sfisioza's answer, but I had problems with it. It also reads the file twice, once and does not compress, and then compresses again. Here is the compressed version:

 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; } 
+1
source
 file_get_contents("php://filter/zlib.inflate/resource=/path/to/file.gz"); 

I'm not sure how it will handle the gz file header.

0
source

Source: https://habr.com/ru/post/1496767/


All Articles