• [织梦吧]唯一域名:www.dedecms8.com,织梦DedeCMS学习平台.

当前位置: > 编程与数据库 > php编程 >

PHP解压ZIP的一个类(2)

来源: www.dedecms8.com 编辑:织梦吧 时间:2012-02-07点击:

  if($compact)

  $cdrec = "\x50\x4b\x01\x02\x00\x00\x14\x00\x00\x00\x08\x00";

  else $cdrec = "\x50\x4b\x01\x02\x14\x00\x0a\x00\x00\x00\x00\x00";

  $cdrec .= $hexdtime.pack('V', $crc).pack('V', $c_len).pack('V', $unc_len);

  $cdrec .= pack('v', strlen($name) ).pack('v', 0 ).pack('v', 0 );

  $cdrec .= pack('v', 0 ).pack('v', 0 ).pack('V', 32 );

  $cdrec .= pack('V', $this -> old_offset );

  $this -> old_offset = $new_offset;

  $cdrec .= $name;

  $this -> ctrl_dir[] = $cdrec;

  return true;

  }

  function DosTime() {

  $timearray = getdate();

  if ($timearray['year'] < 1980) {

  $timearray['year'] = 1980; $timearray['mon'] = 1;

  $timearray['mday'] = 1; $timearray['hours'] = 0;

  $timearray['minutes'] = 0; $timearray['seconds'] = 0;

  }

  return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) | ($timearray['hours'] << 11) |

  ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);

  }

  function Extract ( $zn, $to, $index = Array(-1) )

  {

  $ok = 0; $zip = @fopen($zn,'rb');

  if(!$zip) return(-1);

  $cdir = $this->ReadCentralDir($zip,$zn);

  $pos_entry = $cdir['offset'];

  if(!is_array($index)){ $index = array($index); }

  for($i=0; $index[$i];$i++){

  if(intval($index[$i])!=$index[$i]||$index[$i]>$cdir['entries'])

  return(-1);

  }

  for ($i=0; $i<$cdir['entries']; $i++)

  {

  @fseek($zip, $pos_entry);

  $header = $this->ReadCentralFileHeaders($zip);

  $header['index'] = $i; $pos_entry = ftell($zip);

  @rewind($zip); fseek($zip, $header['offset']);

  if(in_array("-1",$index)||in_array($i,$index))

  $stat[$header['filename']]=$this->ExtractFile($header, $to, $zip);

  }

  fclose($zip);

  return $stat;

  }

  function ReadFileHeader($zip)

  {

  $binary_data = fread($zip, 30);

  $data = unpack('vchk/vid/vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $binary_data);

  $header['filename'] = fread($zip, $data['filename_len']);

  if ($data['extra_len'] != 0) {

  $header['extra'] = fread($zip, $data['extra_len']);

  } else { $header['extra'] = ''; }

  $header['compression'] = $data['compression'];$header['size'] = $data['size'];

  $header['compressed_size'] = $data['compressed_size'];

  $header['crc'] = $data['crc']; $header['flag'] = $data['flag'];

  $header['mdate'] = $data['mdate'];$header['mtime'] = $data['mtime'];

  if ($header['mdate'] && $header['mtime']){

  $hour=($header['mtime']&0xF800)>>11;$minute=($header['mtime']&0x07E0)>>5;

  $seconde=($header['mtime']&0x001F)*2;$year=(($header['mdate']&0xFE00)>>9)+1980;

  $month=($header['mdate']&0x01E0)>>5;$day=$header['mdate']&0x001F;

  $header['mtime'] = mktime($hour, $minute, $seconde, $month, $day, $year);

  }else{$header['mtime'] = time();}

  $header['stored_filename'] = $header['filename'];

  $header['status'] = "ok";

  return $header;

  }

  function ReadCentralFileHeaders($zip){

  $binary_data = fread($zip, 46);

  $header = unpack('vchkid/vid/vversion/vversion_extracted/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/Voffset', $binary_data);

  if ($header['filename_len'] != 0)

  $header['filename'] = fread($zip,$header['filename_len']);

  else $header['filename'] = '';

  if ($header['extra_len'] != 0)

  $header['extra'] = fread($zip, $header['extra_len']);

  else $header['extra'] = '';

  if ($header['comment_len'] != 0)

  $header['comment'] = fread($zip, $header['comment_len']);

  else $header['comment'] = '';

  if ($header['mdate'] && $header['mtime'])

标签: php zip 解压

About D8

  • ©2014 织梦吧(d8) DedeCMS学习交流平台
  • 唯一网址 www.DedeCMS8.com 网站地图
  • 联系我们 1170734538@qq.com ,  QQ