chineerat
chineerat

Reputation: 97

PHP: Uncompressing gzcompressed file with gzuncompress and fopen

I am trying to uncompress a binary file and then uncompress it to test whether the compresssion is working. However the 'uncompressed' file has the same data as the 'compressed' file. As though the uncompression never happened. I have listed code below. thanks in advance:

//compressing  
//read file  
$filename = 'tocompress/tocompress'.$number_input.'.bin';  
$contents=fread($fp,filesize($filename));  
fclose($fp);  
//compress file  
$compressing = gzcompress($contents , '0');  
//write to file  
$fp = fopen('compressed/compressed'.$number_input.'.bin', 'wb');  
 fwrite($fp, $compressing);  
 fclose($fp);  

//uncompressing  
//read file  
$uncompfilename='compressed/compressed'.$number_input.'.bin';  
$fp=fopen($uncompfilename,'rb');  
$uncompresscontents=fread($fp,filesize($uncompfilename));  
fclose($fp);  
//uncompress file  
$uncompressing = gzuncompress($uncompresscontents);  
//write to file  
$fp = fopen('uncompressed/uncompressed'.$number_input.'.bin', 'wb');  
 fwrite($fp, $uncompresscontents);  
fclose($fp);

Upvotes: 0

Views: 2340

Answers (1)

Adam Hopkinson
Adam Hopkinson

Reputation: 28795

gzcompress takes an optional second argument which sets compression level, from 0-9. You're setting it to 0 (no compression) and you should be using an int, not a string:

You have gzcompress($contents, '0');

You want gzcompress($contents, 9);

From php.net/gzcompress:

The level of compression. Can be given as 0 for no compression up to 9 for maximum compression.

Upvotes: 4

Related Questions