PHP解压ZIP的一个类

网络整理 - 07-26
   

  //以下是类及函数定义

  class Zip //ZIP压缩类

  {

  var $datasec, $ctrl_dir = array();

  var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";

  var $old_offset = 0; var $dirs = Array(".");

  function get_List($zip_name)

  {

  $zip = @fopen($zip_name, 'rb');

  if(!$zip) return(0);

  $centd = $this->ReadCentralDir($zip,$zip_name);

  @rewind($zip);

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

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

  {

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

  $header['index'] = $i;$info['filename'] = $header['filename'];

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

  $info['size'] = $header['size'];$info['compressed_size']=$header['compressed_size'];

  $info['crc'] = strtoupper(dechex( $header['crc'] ));

  $info['mtime'] = $header['mtime']; $info['comment'] = $header['comment'];

  $info['folder'] = ($header['external']==0x41FF0010||$header['external']==16)?1:0;

  $info['index'] = $header['index'];$info['status'] = $header['status'];

  $ret[]=$info; unset($header);

  }

  return $ret;

  }

  function Add($files,$compact)

  {

  if(!is_array($files[0])) $files=Array($files);

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

  $fn = $files[$i];

  if(!in_Array(dirname($fn[0]),$this->dirs))

  $this->add_Dir(dirname($fn[0]));

  if(basename($fn[0]))

  $ret[basename($fn[0])]=$this->add_File($fn[1],$fn[0],$compact);

  }

  return $ret;

  }

  function get_file()

  {

  $data = implode('', $this -> datasec);

  $ctrldir = implode('', $this -> ctrl_dir);

  return $data . $ctrldir . $this -> eof_ctrl_dir .

  pack('v', sizeof($this -> ctrl_dir)).pack('v', sizeof($this -> ctrl_dir)).

  pack('V', strlen($ctrldir)) . pack('V', strlen($data)) . "\x00\x00";

  }

  function add_dir($name)

  {

  $name = str_replace("\\", "/", $name);

  $fr = "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00\x00\x00\x00\x00";

  $fr .= pack("V",0).pack("V",0).pack("V",0).pack("v", strlen($name) );

  $fr .= pack("v", 0 ).$name.pack("V", 0).pack("V", 0).pack("V", 0);

  $this -> datasec[] = $fr;

  $new_offset = strlen(implode("", $this->datasec));

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

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

  $cdrec .= pack("v", 0 ).pack("v", 0 ).pack("v", 0 ).pack("v", 0 );

  $ext = "\xff\xff\xff\xff";

  $cdrec .= pack("V", 16 ).pack("V", $this -> old_offset ).$name;

  $this -> ctrl_dir[] = $cdrec;

  $this -> old_offset = $new_offset;

  $this -> dirs[] = $name;

  }

  function add_File($data, $name, $compact = 1)

  {

  $name = str_replace('\\', '/', $name);

  $dtime = dechex($this->DosTime());

  $hexdtime = '\x' . $dtime[6] . $dtime[7].'\x'.$dtime[4] . $dtime[5]

  . '\x' . $dtime[2] . $dtime[3].'\x'.$dtime[0].$dtime[1];

  eval('$hexdtime = "' . $hexdtime . '";');

  if($compact)

  $fr = "\x50\x4b\x03\x04\x14\x00\x00\x00\x08\x00".$hexdtime;

  else $fr = "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00".$hexdtime;

  $unc_len = strlen($data); $crc = crc32($data);

  if($compact){

  $zdata = gzcompress($data); $c_len = strlen($zdata);

  $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2);

  }else{

  $zdata = $data;

  }

  $c_len=strlen($zdata);

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

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

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

  $this -> datasec[] = $fr;

  $new_offset = strlen(implode('', $this->datasec));