Zip Processing---h_zip.php

Source: Internet
Author: User
1
  Get_list ($zip _name); Returns: Gets the zip file and folder List, parameters: $zip _name-zip file name; ';                                                    $H _zip-Extract ($ZN, $to);                            Back: Extract zip, Parameters: $zn-zip file name: $to-Extracted to $H _zip-addfiles ($H _file= ' Test.zip ', $H _flist); Back: Compressed zip, Parameters: $H _file-Compressed ZIP file name: $Hfile-file List (array (' 1.txt ', ' test.php '), Array (' 1.txt ', ' k/my.php ')), $H _ Zip_file = ' T/example.zip '; $H _zip=new h_zip ();    Print_r ($H _zip->extract ($H _zip_file, './', Array (-1)));                Unzip the Print_r ($H _zip->get_list ($H _zip_file));    Return list $H _zip, AddFiles ($H _file= ' test.zip ', Array (' 1.txt ', ' test.php '), Array (' 1.txt ', ' k/my.php ')); Compression *************************************///start class h_zip{$datasec, $ctrl _dir = AR Ray (); Public $eof _ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00"; _offset public $old = 0; Public $dirs = Array ("."); Public $H _temnm = ";      Public Function __construct () {33//When an object is instantiated, this method of the object is first called "Return"; 35} 36 Public Function __destruct () {37//When the deletion of an object or object operation terminates, call the method. Nction __get ($key) {41//when attempting to read a property that does not exist, it is called ' ['. $key. '] Variable not find '; The Public Function __set ($key, $val) {45//when attempting to write a value to a property that does not exist is called "['. $key. ] Variable not find '; __call Public Function ($key, $args) {49//when attempting to invoke a method that does not exist for an object, call the method "['. $ke Y. '] Function not find ';       N/A public function __tostring () {53//When printing an object is called the-------------H_DESCE (); 55 __clone Public Function () {57//When the object is cloned, it is called "clone"; Nction H_desce () {61//Return Description $this-H_desce.' Class name: H_zip-zip processing; '; $this h_desce. = ' function: get_list ($zip _name), returns: Gets the zip file and folder List, parameters: $zip _name-zip file name; '; $this-H_desce. = ' function: Extract ($ZN, $to), returning: Extracting zip, Parameters: $zn-zip file name: $to-unzip to; '; $this-H_desce. = ' function: AddFiles ($H _file= "Test.zip", $H _flist), return: Compressed zip, Parameters: $H _file-Compressed ZIP file name: $Hfile-List of files (arra Y (Array ("1.txt", "test.php"), Array ("1.txt", "k/my.php")); H_DESCE, $this-A; The Get_list ($zip _name) {$zip = @fopen ($zip _name, ' RB '); P) return (0); $this $centd = Readcentraldir ($zip, $zip _name); @rewind ($zip); @fseek ($zip, $centd [' offset ']); ($i =0; $i < $centd [' entries ']; $i + +) {$header = $this, readcentralfileheaders ($zip); 7 7 $header [' index '] = $i; $info [' filename '] = $header [' filename ']; $info [' stored_filename '] = $header [' Stored_filename ']; $info [' size '] = $header[' size ']; $info [' compressed_size '] = $header [' compressed_size ']; Bayi $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); The return $ret;         ($files, $compact) {!is_array ($files [0]) $files = Array ($files); 93 for ($i =0; $files [$i]; $i + +) {94 $FN = $files [$i];!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); 98 return $ret; }100 Public Function gEt_file () {101 $data = implode (", $this-datasec); 102 $ctrldir = Implode (", $this-Ctrl_dir); 10 3 return $data. $ctrldir. $this, Eof_ctrl_dir.pack (' V ', sizeof ($this-Ctrl_dir)). Pack (' V ', sizeof ($this-& Gt Ctrl_dir). Pack (' V ', strlen ($ctrldir)). Pack (' V ', strlen ($data)). " \x00\x00 "; 104}105 Public Function Add_dir ($name) {106 $name = str_replace (" \ \ ","/", $name); 107 $f r = "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00\x00\x00\x00\x00"; 108 $fr. = Pack ("V", 0). Pack ("V", 0). Pack ("V", 0). pack  ("V", strlen ($name)); 109 $fr. = Pack ("V", 0). $name. Pack ("V", 0). Pack ("V", 0). Pack ("V", 0); Datasec[] = $FR; 111 $new _offset = strlen (Implode ("", $this->datasec)) $cdrec = "\x50\x4b\x01\x02\x 00\x00\x0a\x00\x00\x00\x00\x00\x00\x00\x00\x00 "; 113 $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). $n ame;117 $this-ctrl_dir[] = $cdrec, 118 $this, Old_offset = $new _offset;119 $this D Irs[] = $name;}121 public Function Add_file ($data, $name, $compact = 1) {122 $name = str_replace (' \ \ '), '/', $name); 123 $dtime = Dechex ($this->dostime ()); 124 $hexdtime = ' \x '. $dtime [6]. $dtime [7]. ' \x '. $dtime [4]. $dtime [5]. ' \x '. $dtime [2]. $dtime [3]. ' \x '. $dtime [0]. $dtime [1];125 eval (' $hexdtime = "'. $hexdtime. '; '); 126 if ($compact) $fr = "\x50\x4b\x03\x04\x14\x00\x00\x00\x08\x00". $hexdtime; 127 Else $fr = "\x50\x4b\x03\x 04\x0a\x00\x00\x00\x00\x00 ". $hexdtime, $unc _len = strlen ($data), 129 $CRC = CRC32 ($data); $compact) {131 $zdata = gzcompress ($data), $c _len = strlen ($zdata); 133 $zdata = Subs TR (substr ($zdata, 0, strlen ($zdata)-4), 2); 134}else $zdata = $data; 135 $c _len=strlen ($zdata); 136 $fr. = Pack (' V ', $CRC). Pack (' V ', $c _len). Pack (' V ', $unc _len), 137 $fr. = Pack (' V ', strlen ($name)). Pack (' V ', 0). $name. $zdata; 138 $fr. = P        Ack (' V ', $CRC). Pack (' V ', $c _len). Pack (' V ', $unc _len), 139 $this-datasec[] = $FR; $new _offset = strlen (Implode (", $this->datasec)) 141 if ($compact) $cdrec =" \x50\x4b\x01\x02\x00\x00\x14\x00\x00\x00\x08 \x00 "; 142 Else $cdrec =" \x50\x4b\x01\x02\x14\x00\x0a\x00\x00\x00\x00\x00 "; 143 $cdrec. = $hexdtime. Pack (' V ', $CRC). Pack (' V ', $c _len). Pack (' V ', $unc _len); 144 $cdrec. = Pack (' V ', strlen ($name)). Pack (' V ', 0). Pack (' V ', 0) ; 145 $cdrec. = Pack (' V ', 0). Pack (' V ', 0), 146 $cdrec. = Pack (' V ', $this-Old_offset) ; 147 $this-Old_offset = $new _offset;148 $cdrec. = $name; 149 $this, ctrl_dir[] = $cdrec; return true;151}152 Public Function Dostime () {153 $timearray = getdate (), 154 if ($timearray [' Year '] < 1980) {155 $timearray [' year '] = 1980;156 $timearray [' mon '] = 1;157 $timearray [' mday '] = 1;15 8 $timearray [' hours '] = 0;159 $timearray [' minutes '] = 0;160 $timearray [' seconds '] = 0; 161}162 Return (($timearray [' Year ']-1980) << 25) | ($timearray [' mon '] << 21) | ($timearray [' Mday '] << 16) | ($timearray [' hours '] << 11) | ($timearray [' minutes '] << 5) | ($timearray [' seconds '] >> 1); 163}164 Public Function Extract ($ZN, $to, $index = Array ( -1)) {165 $ok = 0;166 $zip = @fopen ($ZN, ' RB '), 167 if (! $zip) return ( -1), 168 $cdir = $this->readcentraldir ($zip , $ZN), 169 $pos _entry = $cdir [' Offset '];170 if (!is_array ($index)) $index = Array ($index); 171 for ($i = 0; $index [$i]; $i + +) {172 if (intval ($index [$i])! = $index [$i]| |             $index [$i]> $cdir [' entries ']) return ( -1) 173}174 for ($i =0; $i < $cdir [' entries ']; $i + +) {175 @fseek ($zip, $pos _entry); 176 $header = $this->readcentralfileheaders ($zip); 177 $header [' Inde X '] = $i; $pos _entry = Ftell ($zip), 178 @rewind ($zip) 179 fseek ($zip, $header [' offset ']); N_array ("1", $index) | | In_array ($i, $index)) $stat [$header [' filename ']]= $this->extractfile ($header, $to, $zip); 181}182 Fclose ( $zip); 183 return $stat; 184}185 public Function Readfileheader ($zip) {186 $binary _data = fread ($zip , 187 $data = unpack (' Vchk/vid/vversion/vflag/vcompression/vmtime/vmdate/vcrc/vcompressed_size/vsize/vfilename _len/vextra_len ', $binary _data); 188 $header [' filename '] = fread ($zip, $data [' Filename_len ']); 189 if ($data [ ' Extra_len ']! = 0) {$header [' extra '] = Fread ($zip, $data [' Extra_len ']); 191}eLSE $header [' extra '] = '; 192 $header [' compression '] = $data [' compression '];193 $header [' size '] = $data [' s         Ize '];194 $header [' compressed_size '] = $data [' compressed_size '];195 $header [' CRC '] = $data [' CRC '];196 $header [' flag '] = $data [' flag '];197 $header [' mdate '] = $data [' mdate '];198 $header [' mtime '] = $data [' MT IME '];199 if ($header [' mdate '] && $header [' mtime ']) {$hour = ($header [' Mtime ']&0xf800) > >11;201 $minute = ($header [' Mtime ']&0x07e0) >>5;202 $seconde = ($header [' Mtime ']&0x001 F) *2;203 $year = (($header [' mdate ']&0xfe00) >>9) +1980;204 $month = ($header [' Mdate ']&0x01 E0) >>5;205 $day = $header [' mdate ']&0x001f;206 $header [' mtime '] = Mktime ($hour, $minute, $s Econde, $month, $day, $year) 207}else $header [' mtime '] = time (), 208 $header [' stored_filename '] = $header [         ' filename '];209$header [' status '] = "OK";}212 return $header; 211 Public Function Readcentralfileheaders ($zip) {213 $binary _data = Fread ($zip, $) 214 $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); 215 if ($header [' Filename_len ']! = 0) $header [' filename '] = fread ($zip, $header [' Filename_le n ']); 216 else $header [' filename '] = '; 217 if ($header [' Extra_len ']! = 0) $header [' extra '] = Fread ($zip, $h eader[' Extra_len '); 218 else $header [' extra '] = '; 219 if ($header [' Comment_len ']! = 0) $header [' Comment '] = Fread ($zip, $header [' Comment_len ']); $header [' comment '] = '; 221 if ($header [' mdate '] && $header [' mtime ']) {222 $hour = ($header [' Mtime '] & 0xf800) >> 11;223 $minute = ($header ['   Mtime '] & 0x07e0) >> 5;224          $seconde = ($header [' Mtime '] & 0x001f) *2;225 $year = (($header [' Mdate '] & 0xfe00) >> 9  ) + 1980;226 $month = ($header [' Mdate '] & 0x01e0) >> 5;227 $day = $header [' Mdate '] & 0x001f;228 $header [' mtime '] = Mktime ($hour, $minute, $seconde, $month, $day, $year); 229}else $header         [' mtime '] = time (), $header [' stored_filename '] = $header [' filename '];231 $header [' status '] = ' OK '; 232 if (substr ($header [' filename '],-1) = = '/') $header [' external '] = 0x41ff0010;233 return $header; 234}23 5 Public Function Readcentraldir ($zip, $zip _name) {236 $size = filesize ($zip _name); 237 if ($size < 277 ) $maximum _size = $size; 238 else $maximum _size=277;239 @fseek ($zip, $size-$maximum _size); $pos = Ftell ($zip); $bytes = 0x00000000;241 while ($pos < $size) {242 $byte = @fread ($zip, 1); $bytes = ($bytes << 8 ) | ORD ($BYTE); 243 if ($bytes = = 0x504b0506) {$pos + +; break;} 244 $pos ++;245}246 $data =unpack (' Vdisk/vdisk_start/vdisk_entries/ventries/vsize/voffset/vcomm Ent_size ', Fread ($zip,)) 247 if ($data [' comment_size ']! = 0) $centd [' comment '] = fread ($zip, $data [' Comment_size ' ]); 248 else $centd [' comment '] = '; 249 $centd [' entries '] = $data [' Entries '];250 $centd [' Disk_entri Es '] = $data [' disk_entries '];251 $centd [' offset '] = $data [' offset '];252 $centd [' disk_start '] = $data [' Disk _start '];253 $centd [' size '] = $data [' Size '];254 $centd [' disk '] = $data [' Disk '];255 return $CENTD; 2         }257 Public Function Extractfile ($header, $to, $zip) {258 $header = $this->readfileheader ($zip); 259 if (substr ($to, -1)! = "/") $to. = "/"; 260 if (! @is_dir ($to)) @mkdir ($to, 0777); 261 $pth = Explode ("/", Dirnam            E ($header [' filename ']); 262 for ($i =0;isset ($pth [$i]); $i + +) {263 if (! $pth [$i]) continue;264 if (!is_dir ($to. $pth [$i])) @mkdir ($to. $pth [$i],0777) 265}266 if (! ( $header [' External ']==0x41ff0010) &&! ($header [' External ']==16)]                 {267 if ($header [' compression ']==0) {268 $fp = @fopen ($to. $header [' filename '], ' WB '); 269 if (! $fp) return ( -1), $size = $header [' Compressed_size '];271 while ($size! = 0) {272 $read _size = ($size < 2048? $size: 2048); 273 $buffer = Fread ($zip, $read _size) 274 $binary _data = Pack (' a '. $read _size, $buffer); 275 @fwrite ($fp, $binary _d                 ATA, $read _size), 276 $size-= $read _size;277}278 fclose ($fp); 279 Touch ($to. $header [' filename '], $header [' mtime ']), 280}else{281 $fp = @fopen ($to. $h eader[' filename '].    GZ ', ' WB '); 282 if (! $fp) return (-1); 283             $binary _data = Pack (' va1a1va1a1 ', 0x8b1f, Chr ($header [' compression ']), Chr (0x00), Time (), Chr (0x00), Chr (3)); 2                 Fwrite ($fp, $binary _data, ten) 285 $size = $header [' Compressed_size '];286  while ($size! = 0) {287 $read _size = ($size < 1024x768 $size: 1024x768); 288 $buffer = Fread ($zip, $read _size); 289 $binary _data = Pack (' a '. $read _size, $buffer); 290 @f                 Write ($fp, $binary _data, $read _size), 291 $size-= $read _size;292}293 $binary _data = Pack (' VV ', $header [' CRC '], $header [' size ']); 294 fwrite ($fp, $binary _data,8); Fclose ($FP); 295 $gzp = @gzopen ($to. $header [' filename ']. GZ ', ' RB ') or die ("Cette archive est compress?");                 296 if (! $gzp) return ( -2), 297 $fp = @fopen ($to. $header [' filename '], ' WB '); 298              if (! $fp) return (-1); 299   $size = $header [' Size '];300 while ($size! = 0) {301 $read _size = ($size < 2048?) $size: 2048); 302 $buffer = Gzread ($gzp, $read _size); 303 $binary _data = Pack (' a '. $ Read_size, $buffer); 304 @fwrite ($FP, $binary _data, $read _size); 305 $size-= $read _ size;306}307 fclose ($FP); Gzclose ($GZP); 308 Touch ($to. $header [' filename '], $header [' mtime ']), 309 @unlink ($to. $header [' filename ']. GZ '); 310}311}312 return true;313}314 public function addfiles ($H _file= ", $H _flist) { 315//Compressed file $H _flist = Array (' Old.txt ', ' new.txt '), $H _file = $H _file = = '? ' Temp_ '. Date (' Ymdhis '). Zip ': $H _file;317 if (!is_array ($H _flist)) return false;318 else{319 foreach ($H _flist as $H _key = = $H _val) {320//loop join 321 $H _OLDFL = '; 322 if (Is_array ($H _val)) {323 $H _OLDFL = $H _val[0];324 $H _NEWFL                 = $H _val[1];325}else $H _OLDFL = $H _NEWFL = $H _val;326 if ($H _OLDFL = = ") continue;327 $H _filesize = @filesize ($H _OLDFL), 328 $H _fp = @fopen ($H _OLDFL, ' RB ');                 329 $H _file_cont = Array ($H _newfl, @fread ($H _fp, $H _filesize)), @fclose ($H _fp); 331    $this, Add ($H _file_cont,1); Add Files 332}333 $this, h_temnm = $H _file;334 if (@fputs (@fopen ($H _file, "WB"), $this Get_file ())) return true;335 else return false;336}337 return true;338}339}//end Class340?>
  • 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.