php2015中文文档,PHP - Manual: gzwrite (官方文档)

gz compression is often used with tar archives. Building tar archives is quite easy. Here is a code snipplet which can be used for building tar archives before compressing them to tar.gz.

// Adds file header to the tar file, it is used before adding file content.

// f: file resource (provided by eg. fopen)

// phisfn: path to file

// archfn: path to file in archive, directory names must be followed by '/'

//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\functionTarAddHeader($f,$phisfn,$archfn)

{$info=stat($phisfn);$ouid=sprintf("%6s ",decoct($info[4]));$ogid=sprintf("%6s ",decoct($info[5]));$omode=sprintf("%6s ",decoct(fileperms($phisfn)));$omtime=sprintf("%11s",decoct(filemtime($phisfn)));

if (@is_dir($phisfn))

{$type="5";$osize=sprintf("%11s ",decoct(0));

}

else

{$type='';$osize=sprintf("%11s ",decoct(filesize($phisfn)));clearstatcache();

}$dmajor='';$dminor='';$gname='';$linkname='';$magic='';$prefix='';$uname='';$version='';$chunkbeforeCS=pack("a100a8a8a8a12A12",$archfn,$omode,$ouid,$ogid,$osize,$omtime);$chunkafterCS=pack("a1a100a6a2a32a32a8a8a155a12",$type,$linkname,$magic,$version,$uname,$gname,$dmajor,$dminor,$prefix,'');$checksum=0;

for ($i=0;$i<148;$i++)$checksum+=ord(substr($chunkbeforeCS,$i,1));

for ($i=148;$i<156;$i++)$checksum+=ord(' ');

for ($i=156,$j=0;$i<512;$i++,$j++)$checksum+=ord(substr($chunkafterCS,$j,1));fwrite($f,$chunkbeforeCS,148);$checksum=sprintf("%6s ",decoct($checksum));$bdchecksum=pack("a8",$checksum);fwrite($f,$bdchecksum,8);fwrite($f,$chunkafterCS,356);

returntrue;

}//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

// Writes file content to the tar file must be called after a TarAddHeader

// f:file resource provided by fopen

// phisfn: path to file

//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/functionTarWriteContents($f,$phisfn)

{

if (@is_dir($phisfn))

{

return;

}

else

{$size=filesize($phisfn);$padding=$size%512?512-$size%512:0;$f2=fopen($phisfn,"rb");

while (!feof($f2))fwrite($f,fread($f2,1024*1024));$pstr=sprintf("a%d",$padding);fwrite($f,pack($pstr,''));

}

}//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

// Adds 1024 byte footer at the end of the tar file

// f: file resource

//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/functionTarAddFooter($f)

{fwrite($f,pack('a1024',''));

}?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值