PHP:使用gzuncompress和fopen解压缩gzcompressed文件

时间:2011-07-29 13:10:35

标签: php zlib gz compression

我正在尝试解压缩二进制文件,然后解压缩以测试压缩是否正常工作。 但是,“未压缩”文件与“压缩”文件具有相同的数据。好像从未发生过压缩。我在下面列出了代码。 提前谢谢:

//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);

1 个答案:

答案 0 :(得分:4)

gzcompress采用可选的第二个参数设置压缩级别,从0到9。你将它设置为0(没有压缩),你应该使用int,而不是字符串:

您有gzcompress($contents, '0');

您想要gzcompress($contents, 9);

来自php.net/gzcompress

  

压缩程度。可以给出0表示无压缩,最高可达9   用于最大压缩。