Real zip file Operation Class (PHP) _php tutorial

Source: Internet
Author: User
Tags fread rewind unpack
/********************
Author unknown
Finishing: Longbill (www.longbill.cn; longbill.cn@gmail.com)
*********************/

Class 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. Eof_ctrl_dir, $this.
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", +). 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 '] < 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) * *, $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) * *;
$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 < 277) $maximum _size = $size;
else $maximum _size=277;

@fseek ($zip, $size-$maximum _size);
$pos = Ftell ($zip); $bytes = 0x00000000;

while ($pos < $size)
{
$byte = @fread ($zip, 1); $bytes = ($bytes << 8) | Ord ($byte);
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 < 2048? $size: 2048);
$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 < 1024x768 $size: 1024);
$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 < 2048? $size: 2048);
$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;
}
}
?>

Package file Download

http://www.bkjia.com/PHPjc/318463.html www.bkjia.com true http://www.bkjia.com/PHPjc/318463.html techarticle /******************** Author Unknown Collation: Longbill (www.longbill.cn;longbill.cn@gmail.com) *********************/Classzip {var$datasec, $ctrl _dir=array (); var$eof_ctrl_dir= "\x50\x ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.