PHP GZ壓縮與解壓
阿新 • • 發佈:2017-10-23
code fwrite name bsp time style write fclose log
1 /*將字符串添加至GZ文件*/ 2 function gz_str($str,$gz_name){ 3 $fp = gzopen ($gz_name, ‘w9‘); 4 gzwrite ($fp, $str); 5 gzclose($fp); 6 } 7 /*將文件添加至GZ文件*/ 8 function gz_file($file,$gz_name){ 9 $fp = gzopen ($gz_name, ‘w9‘); 10 gzwrite ($fp, file_get_contents($file)); 11 gzclose($fp); 12 } 13 /*讀取GZ文件*/ 14 function read_gz($gz_file){ 15 $buffer_size = 4096; // read 4kb at a time 16 $file = gzopen($gz_file, ‘rb‘); 17 $str=‘‘; 18 while(!gzeof($file)) { 19 $str.=gzread($file, $buffer_size); 20 } 21 gzclose($file); 22 return $str; 23 } 24 /*解壓GZ文件*/ 25 function unzip_gz($gz_file){ 26 $buffer_size = 4096; // read 4kb at a time 27 $out_file_name = str_replace(‘.gz‘, ‘‘, $gz_file); 28 $file = gzopen($gz_file, ‘rb‘); 29 $out_file = fopen($out_file_name, ‘wb‘); 30 $str=‘‘; 31 while(!gzeof($file)) { 32 fwrite($out_file, gzread($file, $buffer_size)); 33 } 34 fclose($out_file); 35 gzclose($file); 36 }
PHP GZ壓縮與解壓