search
Homephp教程php手册PHP解压ZIP的一个类

 

  //以下是类及函数定义

  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

  {

  $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));

  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']

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

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

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

  }

  return (($timearray['year'] - 1980)

  ($timearray['minutes'] > 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

  {

  @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'])

  {

  $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';

  if (substr($header['filename'], -1) == '/')

  $header['external'] = 0x41FF0010;

  return $header;

  }

  function ReadCentralDir($zip,$zip_name)

  {

  $size = filesize($zip_name);

  if ($size

  else $maximum_size=277;

  @fseek($zip, $size-$maximum_size);

  $pos = ftell($zip); $bytes = 0x00000000;

  while ($pos

  {

  $byte = @fread($zip, 1); $bytes=($bytes

  if ($bytes == 0x504b0506){ $pos++; break; } $pos++;

  }

  $data=unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size',fread($zip,18));

  if ($data['comment_size'] != 0)

  $centd['comment'] = fread($zip, $data['comment_size']);

  else $centd['comment'] = ''; $centd['entries'] = $data['entries'];

  $centd['disk_entries'] = $data['disk_entries'];

  $centd['offset'] = $data['offset'];$centd['disk_start'] = $data['disk_start'];

  $centd['size'] = $data['size']; $centd['disk'] = $data['disk'];

  return $centd;

  }

  function ExtractFile($header,$to,$zip)

  {

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

  if(substr($to,-1)!="/") $to.="/";

  if(!@is_dir($to)) @mkdir($to,0777);

  $pth = explode("/",dirname($header['filename']));

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

  if(!$pth[$i]) continue;$pthss.=$pth[$i]."/";

  if(!is_dir($to.$pthss)) @mkdir($to.$pthss,0777);

  }

  if (!($header['external']==0x41FF0010)&&!($header['external']==16))

  {

  if ($header['compression']==0)

  {

  $fp = @fopen($to.$header['filename'], 'wb');

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

  $size = $header['compressed_size'];

  while ($size != 0)

  {

  $read_size = ($size

  $buffer = fread($zip, $read_size);

  $binary_data = pack('a'.$read_size, $buffer);

  @fwrite($fp, $binary_data, $read_size);

  $size -= $read_size;

  }

  fclose($fp);

  touch($to.$header['filename'], $header['mtime']);

  }else{

  $fp = @fopen($to.$header['filename'].'.gz','wb');

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

  $binary_data = pack('va1a1Va1a1', 0x8b1f, Chr($header['compression']),

  Chr(0x00), time(), Chr(0x00), Chr(3));

  fwrite($fp, $binary_data, 10);

  $size = $header['compressed_size'];

  while ($size != 0)

  {

  $read_size = ($size

  $buffer = fread($zip, $read_size);

  $binary_data = pack('a'.$read_size, $buffer);

  @fwrite($fp, $binary_data, $read_size);

  $size -= $read_size;

  }

  $binary_data = pack('VV', $header['crc'], $header['size']);

  fwrite($fp, $binary_data,8); fclose($fp);

  $gzp = @gzopen($to.$header['filename'].'.gz','rb') or die("Cette archive est compress閑");

  if(!$gzp) return(-2);

  $fp = @fopen($to.$header['filename'],'wb');

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

  $size = $header['size'];

  while ($size != 0)

  {

  $read_size = ($size

  $buffer = gzread($gzp, $read_size);

  $binary_data = pack('a'.$read_size, $buffer);

  @fwrite($fp, $binary_data, $read_size);

  $size -= $read_size;

  }

  fclose($fp); gzclose($gzp);

  touch($to.$header['filename'], $header['mtime']);

  @unlink($to.$header['filename'].'.gz');

  }}

  return true;

  }

  } //ZIP压缩类end



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
使用java的File.length()函数获取文件的大小使用java的File.length()函数获取文件的大小Jul 24, 2023 am 08:36 AM

使用Java的File.length()函数获取文件的大小文件大小是在处理文件操作时很常见的一个需求,Java提供了一个很方便的方法来获取文件的大小,即使用File类的length()方法。本文将介绍如何使用该方法来获取文件的大小,并给出相应的代码示例。首先,我们需要创建一个File对象来表示我们想要获取大小的文件。以下是创建File对象的方法:Filef

SpringBoot怎么通过Feign调用传递Header中参数SpringBoot怎么通过Feign调用传递Header中参数May 16, 2023 pm 08:38 PM

【SpringBoot】通过Feign调用传递Header中参数如何通过Feign传递Header参数问题描述我们在SpringCloud中使用Feign请求另一个服务的Api接口时,有将Header中参数传递下去的需求,如果不做特殊处理,就会将Header中的参数丢失。解决方案方案一:通过@RequestHeader(name="headerName")来传递例如:Feign定义如下@FeignClient(name="service-name")pub

linux的header是什么意思linux的header是什么意思Jul 18, 2023 pm 03:34 PM

linux的header是指在文件或数据流中的开头部分,用于包含关于内容的元数据,通过正确地编写和使用Header文件,开发者能够更好地利用系统资源,提高代码的可读性和可维护性。

如何使用PHP header()方法来调整网页如何使用PHP header()方法来调整网页Mar 28, 2023 pm 01:54 PM

PHP是一种功能强大的编程语言,可以用来创建动态网站和Web应用程序。其中一个最强大的功能之一是PHP的header()方法。在本文中,我们将探讨如何使用PHP的header()方法来调整网页。

php header头怎么实现跳转php header头怎么实现跳转Dec 02, 2022 am 09:14 AM

php header实现跳转的方法:1、使用“Header("Location:$url");”语法实现跳转;2、使用if判断式实现跳转,其跳转语句如“if($_COOKIE["u_type"]){ header('location:register.php'); } else{ setcookie('u_type','1','86400*360');”。

html5标签head和header有什么区别html5标签head和header有什么区别Jan 17, 2022 am 11:10 AM

区别:1、head标签用于定义文档头部,它是所有头部元素的容器,而header标签用于定义文档的页眉(介绍信息);2、浏览器都支持head标签,而旧版本浏览器均不支持header标签,需要IE9+以上浏览器才支持header标签。

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code<form name="myform"

Nginx如何通过header中的标识进行分发Nginx如何通过header中的标识进行分发May 11, 2023 pm 04:01 PM

Nginx可以根据请求头中自定义的标识将请求分发到不同的服务器。具体来说,可以使用map指令将请求头中的自定义标识映射为不同的后端服务器地址,然后使用proxy_pass指令将请求转发到对应的后端服务器。以下是一个示例配置文件:http{map$http_my_header$backend{defaultbackend1.example.com;value1backend2.example.com;value2backend3.example.com;}upstreambackend1{serv

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!