gzopen

簡介

gzopen—Opengz-file

說明

resourcegzopen(string$filename,string$mode[,int$use_include_path=0])
Opensagzip(.gz)fileforreadingorwriting.
gzopen()canbeusedtoreadafilewhichisnotingzipformat;inthiscasegzread()willdirectlyreadfromthefilewithoutdecompression.

參數

filename
Thefilename.
mode
Asinfopen()(rborwb)butcanalsoincludeacompressionlevel(wb9)orastrategy:fforfiltereddataasinwb6f,hforHuffmanonlycompressionasinwb1h(SeethedescriptionofdeflateInit2inzlib.hformoreinformationaboutthestrategyparameter.)
use_include_path
Youcansetthisoptionalparameterto1,ifyouwanttosearchforthefileintheinclude_pathtoo.

返回值

Returnsafilepointertothefileopened,afterthat,everythingyoureadfromthisfiledescriptorwillbetransparentlydecompressedandwhatyouwritegetscompressed.
Iftheopenfails,thefunctionreturnsFALSE.

範例

<?php

$fp=gzopen("/tmp/file.gz","r");
?>
參見
gzclose()-Closeanopengz-filepointer
gzopen("php://output","wb")doesn'tworkonawebserver,nordoesfopen("compress.zlib://php://output","wb").
Hereisasnippettogzipafileandoutputitonthefly,withoutusingatemporaryfile,withoutreadingthefileintomemory,andwithoutreadingthefilemorethanonce:
<?php
$fin=fopen($file,"rb");
if($fin!==FALSE){
$fout=fopen("php://output","wb");
if($fout!==FALSE){
//writegzipheader
fwrite($fout,"x1Fx8Bx08x08".pack("V",filemtime($file))."xFF",10);
//writetheoriginalfilename
$oname=str_replace("","",basename($file));
fwrite($fout,$oname."",1+strlen($oname));
//addthedeflatefilterusingdefaultcompressionlevel
$fltr=stream_filter_append($fout,"zlib.deflate",STREAM_FILTER_WRITE,-1);
//setuptheCRC32hashingcontext
$hctx=hash_init("crc32b");
//turnoffthetimelimit
if(!ini_get("safe_mode"))set_time_limit(0);
$con=TRUE;
$fsize=0;
while(($con!==FALSE)&&!feof($fin)){
//deflateworksbestwithbuffers>32K
$con=fread($fin,64*1024);
if($con!==FALSE){
hash_update($hctx,$con);
$clen=strlen($con);
$fsize+=$clen;
fwrite($fout,$con,$clen);
}
}
//removethedeflatefilter
stream_filter_remove($fltr);
//writetheCRC32value
//hash_finalisastring,notaninteger
$crc=hash_final($hctx,TRUE);
//needtoreversethehash_finalstringsoit'slittleendian
fwrite($fout,$crc[3].$crc[2].$crc[1].$crc[0],4);
//writetheoriginaluncompressedfilesize
fwrite($fout,pack("V",$fsize),4);
fclose($fout);
}
fclose($fin);
}
?>

相關詞條

熱門詞條

聯絡我們