1<?php 2 3namespace splitbrain\PHPArchive; 4 5/** 6 * Class Zip 7 * 8 * Creates or extracts Zip archives 9 * 10 * for specs see http://www.pkware.com/appnote 11 * 12 * @author Andreas Gohr <andi@splitbrain.org> 13 * @package splitbrain\PHPArchive 14 * @license MIT 15 */ 16class Zip extends Archive 17{ 18 19 protected $file = ''; 20 protected $fh; 21 protected $memory = ''; 22 protected $closed = true; 23 protected $writeaccess = false; 24 protected $ctrl_dir; 25 protected $complevel = 9; 26 27 /** 28 * Set the compression level. 29 * 30 * Compression Type is ignored for ZIP 31 * 32 * You can call this function before adding each file to set differen compression levels 33 * for each file. 34 * 35 * @param int $level Compression level (0 to 9) 36 * @param int $type Type of compression to use ignored for ZIP 37 * @return mixed 38 */ 39 public function setCompression($level = 9, $type = Archive::COMPRESS_AUTO) 40 { 41 $this->complevel = $level; 42 } 43 44 /** 45 * Open an existing ZIP file for reading 46 * 47 * @param string $file 48 * @throws ArchiveIOException 49 */ 50 public function open($file) 51 { 52 $this->file = $file; 53 $this->fh = @fopen($this->file, 'rb'); 54 if (!$this->fh) { 55 throw new ArchiveIOException('Could not open file for reading: '.$this->file); 56 } 57 $this->closed = false; 58 } 59 60 /** 61 * Read the contents of a ZIP archive 62 * 63 * This function lists the files stored in the archive, and returns an indexed array of FileInfo objects 64 * 65 * The archive is closed afer reading the contents, for API compatibility with TAR files 66 * Reopen the file with open() again if you want to do additional operations 67 * 68 * @throws ArchiveIOException 69 * @return FileInfo[] 70 */ 71 public function contents() 72 { 73 if ($this->closed || !$this->file) { 74 throw new ArchiveIOException('Can not read from a closed archive'); 75 } 76 77 $result = array(); 78 79 $centd = $this->readCentralDir(); 80 81 @rewind($this->fh); 82 @fseek($this->fh, $centd['offset']); 83 84 for ($i = 0; $i < $centd['entries']; $i++) { 85 $result[] = $this->header2fileinfo($this->readCentralFileHeader()); 86 } 87 88 $this->close(); 89 return $result; 90 } 91 92 /** 93 * Extract an existing ZIP archive 94 * 95 * The $strip parameter allows you to strip a certain number of path components from the filenames 96 * found in the tar file, similar to the --strip-components feature of GNU tar. This is triggered when 97 * an integer is passed as $strip. 98 * Alternatively a fixed string prefix may be passed in $strip. If the filename matches this prefix, 99 * the prefix will be stripped. It is recommended to give prefixes with a trailing slash. 100 * 101 * By default this will extract all files found in the archive. You can restrict the output using the $include 102 * and $exclude parameter. Both expect a full regular expression (including delimiters and modifiers). If 103 * $include is set only files that match this expression will be extracted. Files that match the $exclude 104 * expression will never be extracted. Both parameters can be used in combination. Expressions are matched against 105 * stripped filenames as described above. 106 * 107 * @param string $outdir the target directory for extracting 108 * @param int|string $strip either the number of path components or a fixed prefix to strip 109 * @param string $exclude a regular expression of files to exclude 110 * @param string $include a regular expression of files to include 111 * @throws ArchiveIOException 112 * @return FileInfo[] 113 */ 114 function extract($outdir, $strip = '', $exclude = '', $include = '') 115 { 116 if ($this->closed || !$this->file) { 117 throw new ArchiveIOException('Can not read from a closed archive'); 118 } 119 120 $outdir = rtrim($outdir, '/'); 121 @mkdir($outdir, 0777, true); 122 123 $extracted = array(); 124 125 $cdir = $this->readCentralDir(); 126 $pos_entry = $cdir['offset']; // begin of the central file directory 127 128 for ($i = 0; $i < $cdir['entries']; $i++) { 129 // read file header 130 @fseek($this->fh, $pos_entry); 131 $header = $this->readCentralFileHeader(); 132 $header['index'] = $i; 133 $pos_entry = ftell($this->fh); // position of the next file in central file directory 134 fseek($this->fh, $header['offset']); // seek to beginning of file header 135 $header = $this->readFileHeader($header); 136 $fileinfo = $this->header2fileinfo($header); 137 138 // apply strip rules 139 $fileinfo->strip($strip); 140 141 // skip unwanted files 142 if (!strlen($fileinfo->getPath()) || !$fileinfo->match($include, $exclude)) { 143 continue; 144 } 145 146 $extracted[] = $fileinfo; 147 148 // create output directory 149 $output = $outdir.'/'.$fileinfo->getPath(); 150 $directory = ($header['folder']) ? $output : dirname($output); 151 @mkdir($directory, 0777, true); 152 153 // nothing more to do for directories 154 if ($fileinfo->getIsdir()) { 155 continue; 156 } 157 158 // compressed files are written to temporary .gz file first 159 if ($header['compression'] == 0) { 160 $extractto = $output; 161 } else { 162 $extractto = $output.'.gz'; 163 } 164 165 // open file for writing 166 $fp = fopen($extractto, "wb"); 167 if (!$fp) { 168 throw new ArchiveIOException('Could not open file for writing: '.$extractto); 169 } 170 171 // prepend compression header 172 if ($header['compression'] != 0) { 173 $binary_data = pack( 174 'va1a1Va1a1', 175 0x8b1f, 176 chr($header['compression']), 177 chr(0x00), 178 time(), 179 chr(0x00), 180 chr(3) 181 ); 182 fwrite($fp, $binary_data, 10); 183 } 184 185 // read the file and store it on disk 186 $size = $header['compressed_size']; 187 while ($size != 0) { 188 $read_size = ($size < 2048 ? $size : 2048); 189 $buffer = fread($this->fh, $read_size); 190 $binary_data = pack('a'.$read_size, $buffer); 191 fwrite($fp, $binary_data, $read_size); 192 $size -= $read_size; 193 } 194 195 // finalize compressed file 196 if ($header['compression'] != 0) { 197 $binary_data = pack('VV', $header['crc'], $header['size']); 198 fwrite($fp, $binary_data, 8); 199 } 200 201 // close file 202 fclose($fp); 203 204 // unpack compressed file 205 if ($header['compression'] != 0) { 206 $gzp = @gzopen($extractto, 'rb'); 207 if (!$gzp) { 208 @unlink($extractto); 209 throw new ArchiveIOException('Failed file extracting. gzip support missing?'); 210 } 211 $fp = @fopen($output, 'wb'); 212 if (!$fp) { 213 throw new ArchiveIOException('Could not open file for writing: '.$extractto); 214 } 215 216 $size = $header['size']; 217 while ($size != 0) { 218 $read_size = ($size < 2048 ? $size : 2048); 219 $buffer = gzread($gzp, $read_size); 220 $binary_data = pack('a'.$read_size, $buffer); 221 @fwrite($fp, $binary_data, $read_size); 222 $size -= $read_size; 223 } 224 fclose($fp); 225 gzclose($gzp); 226 } 227 228 touch($output, $fileinfo->getMtime()); 229 //FIXME what about permissions? 230 } 231 232 $this->close(); 233 return $extracted; 234 } 235 236 /** 237 * Create a new ZIP file 238 * 239 * If $file is empty, the zip file will be created in memory 240 * 241 * @param string $file 242 * @throws ArchiveIOException 243 */ 244 public function create($file = '') 245 { 246 $this->file = $file; 247 $this->memory = ''; 248 $this->fh = 0; 249 250 if ($this->file) { 251 $this->fh = @fopen($this->file, 'wb'); 252 253 if (!$this->fh) { 254 throw new ArchiveIOException('Could not open file for writing: '.$this->file); 255 } 256 } 257 $this->writeaccess = true; 258 $this->closed = false; 259 $this->ctrl_dir = array(); 260 } 261 262 /** 263 * Add a file to the current ZIP archive using an existing file in the filesystem 264 * 265 * @param string $file path to the original file 266 * @param string|FileInfo $fileinfo either the name to us in archive (string) or a FileInfo oject with all meta data, empty to take from original 267 * @throws ArchiveIOException 268 */ 269 270 /** 271 * Add a file to the current archive using an existing file in the filesystem 272 * 273 * @param string $file path to the original file 274 * @param string|FileInfo $fileinfo either the name to us in archive (string) or a FileInfo oject with all meta data, empty to take from original 275 * @throws ArchiveIOException 276 */ 277 public function addFile($file, $fileinfo = '') 278 { 279 if (is_string($fileinfo)) { 280 $fileinfo = FileInfo::fromPath($file, $fileinfo); 281 } 282 283 if ($this->closed) { 284 throw new ArchiveIOException('Archive has been closed, files can no longer be added'); 285 } 286 287 $data = @file_get_contents($file); 288 if ($data === false) { 289 throw new ArchiveIOException('Could not open file for reading: '.$file); 290 } 291 292 // FIXME could we stream writing compressed data? gzwrite on a fopen handle? 293 $this->addData($fileinfo, $data); 294 } 295 296 /** 297 * Add a file to the current TAR archive using the given $data as content 298 * 299 * @param string|FileInfo $fileinfo either the name to us in archive (string) or a FileInfo oject with all meta data 300 * @param string $data binary content of the file to add 301 * @throws ArchiveIOException 302 */ 303 public function addData($fileinfo, $data) 304 { 305 if (is_string($fileinfo)) { 306 $fileinfo = new FileInfo($fileinfo); 307 } 308 309 if ($this->closed) { 310 throw new ArchiveIOException('Archive has been closed, files can no longer be added'); 311 } 312 313 // prepare info and compress data 314 $size = strlen($data); 315 $crc = crc32($data); 316 if ($this->complevel) { 317 $data = gzcompress($data, $this->complevel); 318 $data = substr($data, 2, -4); // strip compression headers 319 } 320 $csize = strlen($data); 321 $offset = $this->dataOffset(); 322 $name = $fileinfo->getPath(); 323 $time = $fileinfo->getMtime(); 324 325 // write local file header 326 $this->writebytes($this->makeLocalFileHeader( 327 $time, 328 $crc, 329 $size, 330 $csize, 331 $name, 332 (bool) $this->complevel 333 )); 334 335 // we store no encryption header 336 337 // write data 338 $this->writebytes($data); 339 340 // we store no data descriptor 341 342 // add info to central file directory 343 $this->ctrl_dir[] = $this->makeCentralFileRecord( 344 $offset, 345 $time, 346 $crc, 347 $size, 348 $csize, 349 $name, 350 (bool) $this->complevel 351 ); 352 } 353 354 /** 355 * Add the closing footer to the archive if in write mode, close all file handles 356 * 357 * After a call to this function no more data can be added to the archive, for 358 * read access no reading is allowed anymore 359 */ 360 public function close() 361 { 362 if ($this->closed) { 363 return; 364 } // we did this already 365 366 if ($this->writeaccess) { 367 // write central directory 368 $offset = $this->dataOffset(); 369 $ctrldir = join('', $this->ctrl_dir); 370 $this->writebytes($ctrldir); 371 372 // write end of central directory record 373 $this->writebytes("\x50\x4b\x05\x06"); // end of central dir signature 374 $this->writebytes(pack('v', 0)); // number of this disk 375 $this->writebytes(pack('v', 0)); // number of the disk with the start of the central directory 376 $this->writebytes(pack('v', 377 count($this->ctrl_dir))); // total number of entries in the central directory on this disk 378 $this->writebytes(pack('v', count($this->ctrl_dir))); // total number of entries in the central directory 379 $this->writebytes(pack('V', strlen($ctrldir))); // size of the central directory 380 $this->writebytes(pack('V', 381 $offset)); // offset of start of central directory with respect to the starting disk number 382 $this->writebytes(pack('v', 0)); // .ZIP file comment length 383 384 $this->ctrl_dir = array(); 385 } 386 387 // close file handles 388 if ($this->file) { 389 fclose($this->fh); 390 $this->file = ''; 391 $this->fh = 0; 392 } 393 394 $this->writeaccess = false; 395 $this->closed = true; 396 } 397 398 /** 399 * Returns the created in-memory archive data 400 * 401 * This implicitly calls close() on the Archive 402 */ 403 public function getArchive() 404 { 405 $this->close(); 406 407 return $this->memory; 408 } 409 410 /** 411 * Save the created in-memory archive data 412 * 413 * Note: It's more memory effective to specify the filename in the create() function and 414 * let the library work on the new file directly. 415 * 416 * @param $file 417 * @throws ArchiveIOException 418 */ 419 public function save($file) 420 { 421 if (!file_put_contents($file, $this->getArchive())) { 422 throw new ArchiveIOException('Could not write to file: '.$file); 423 } 424 } 425 426 /** 427 * Read the central directory 428 * 429 * This key-value list contains general information about the ZIP file 430 * 431 * @return array 432 */ 433 protected function readCentralDir() 434 { 435 $size = filesize($this->file); 436 if ($size < 277) { 437 $maximum_size = $size; 438 } else { 439 $maximum_size = 277; 440 } 441 442 @fseek($this->fh, $size - $maximum_size); 443 $pos = ftell($this->fh); 444 $bytes = 0x00000000; 445 446 while ($pos < $size) { 447 $byte = @fread($this->fh, 1); 448 $bytes = (($bytes << 8) & 0xFFFFFFFF) | ord($byte); 449 if ($bytes == 0x504b0506) { 450 break; 451 } 452 $pos++; 453 } 454 455 $data = unpack( 456 'vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', 457 fread($this->fh, 18) 458 ); 459 460 if ($data['comment_size'] != 0) { 461 $centd['comment'] = fread($this->fh, $data['comment_size']); 462 } else { 463 $centd['comment'] = ''; 464 } 465 $centd['entries'] = $data['entries']; 466 $centd['disk_entries'] = $data['disk_entries']; 467 $centd['offset'] = $data['offset']; 468 $centd['disk_start'] = $data['disk_start']; 469 $centd['size'] = $data['size']; 470 $centd['disk'] = $data['disk']; 471 return $centd; 472 } 473 474 /** 475 * Read the next central file header 476 * 477 * Assumes the current file pointer is pointing at the right position 478 * 479 * @return array 480 */ 481 protected function readCentralFileHeader() 482 { 483 $binary_data = fread($this->fh, 46); 484 $header = unpack( 485 'vchkid/vid/vversion/vversion_extracted/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/Voffset', 486 $binary_data 487 ); 488 489 if ($header['filename_len'] != 0) { 490 $header['filename'] = fread($this->fh, $header['filename_len']); 491 } else { 492 $header['filename'] = ''; 493 } 494 495 if ($header['extra_len'] != 0) { 496 $header['extra'] = fread($this->fh, $header['extra_len']); 497 } else { 498 $header['extra'] = ''; 499 } 500 501 if ($header['comment_len'] != 0) { 502 $header['comment'] = fread($this->fh, $header['comment_len']); 503 } else { 504 $header['comment'] = ''; 505 } 506 507 $header['mtime'] = $this->makeUnixTime($header['mdate'], $header['mtime']); 508 $header['stored_filename'] = $header['filename']; 509 $header['status'] = 'ok'; 510 if (substr($header['filename'], -1) == '/') { 511 $header['external'] = 0x41FF0010; 512 } 513 $header['folder'] = ($header['external'] == 0x41FF0010 || $header['external'] == 16) ? 1 : 0; 514 515 return $header; 516 } 517 518 /** 519 * Reads the local file header 520 * 521 * This header precedes each individual file inside the zip file. Assumes the current file pointer is pointing at 522 * the right position already. Enhances the given central header with the data found at the local header. 523 * 524 * @param array $header the central file header read previously (see above) 525 * @return array 526 */ 527 protected function readFileHeader($header) 528 { 529 $binary_data = fread($this->fh, 30); 530 $data = unpack( 531 'vchk/vid/vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', 532 $binary_data 533 ); 534 535 $header['filename'] = fread($this->fh, $data['filename_len']); 536 if ($data['extra_len'] != 0) { 537 $header['extra'] = fread($this->fh, $data['extra_len']); 538 } else { 539 $header['extra'] = ''; 540 } 541 542 $header['compression'] = $data['compression']; 543 foreach (array( 544 'size', 545 'compressed_size', 546 'crc' 547 ) as $hd) { // On ODT files, these headers are 0. Keep the previous value. 548 if ($data[$hd] != 0) { 549 $header[$hd] = $data[$hd]; 550 } 551 } 552 $header['flag'] = $data['flag']; 553 $header['mtime'] = $this->makeUnixTime($data['mdate'], $data['mtime']); 554 555 $header['stored_filename'] = $header['filename']; 556 $header['status'] = "ok"; 557 $header['folder'] = ($header['external'] == 0x41FF0010 || $header['external'] == 16) ? 1 : 0; 558 return $header; 559 } 560 561 /** 562 * Create fileinfo object from header data 563 * 564 * @param $header 565 * @return FileInfo 566 */ 567 protected function header2fileinfo($header) 568 { 569 $fileinfo = new FileInfo(); 570 $fileinfo->setPath($header['filename']); 571 $fileinfo->setSize($header['size']); 572 $fileinfo->setCompressedSize($header['compressed_size']); 573 $fileinfo->setMtime($header['mtime']); 574 $fileinfo->setComment($header['comment']); 575 $fileinfo->setIsdir($header['external'] == 0x41FF0010 || $header['external'] == 16); 576 return $fileinfo; 577 } 578 579 /** 580 * Write to the open filepointer or memory 581 * 582 * @param string $data 583 * @throws ArchiveIOException 584 * @return int number of bytes written 585 */ 586 protected function writebytes($data) 587 { 588 if (!$this->file) { 589 $this->memory .= $data; 590 $written = strlen($data); 591 } else { 592 $written = @fwrite($this->fh, $data); 593 } 594 if ($written === false) { 595 throw new ArchiveIOException('Failed to write to archive stream'); 596 } 597 return $written; 598 } 599 600 /** 601 * Current data pointer position 602 * 603 * @fixme might need a -1 604 * @return int 605 */ 606 protected function dataOffset() 607 { 608 if ($this->file) { 609 return ftell($this->fh); 610 } else { 611 return strlen($this->memory); 612 } 613 } 614 615 /** 616 * Create a DOS timestamp from a UNIX timestamp 617 * 618 * DOS timestamps start at 1980-01-01, earlier UNIX stamps will be set to this date 619 * 620 * @param $time 621 * @return int 622 */ 623 protected function makeDosTime($time) 624 { 625 $timearray = getdate($time); 626 if ($timearray['year'] < 1980) { 627 $timearray['year'] = 1980; 628 $timearray['mon'] = 1; 629 $timearray['mday'] = 1; 630 $timearray['hours'] = 0; 631 $timearray['minutes'] = 0; 632 $timearray['seconds'] = 0; 633 } 634 return (($timearray['year'] - 1980) << 25) | 635 ($timearray['mon'] << 21) | 636 ($timearray['mday'] << 16) | 637 ($timearray['hours'] << 11) | 638 ($timearray['minutes'] << 5) | 639 ($timearray['seconds'] >> 1); 640 } 641 642 /** 643 * Create a UNIX timestamp from a DOS timestamp 644 * 645 * @param $mdate 646 * @param $mtime 647 * @return int 648 */ 649 protected function makeUnixTime($mdate = null, $mtime = null) 650 { 651 if ($mdate && $mtime) { 652 $year = (($mdate & 0xFE00) >> 9) + 1980; 653 $month = ($mdate & 0x01E0) >> 5; 654 $day = $mdate & 0x001F; 655 656 $hour = ($mtime & 0xF800) >> 11; 657 $minute = ($mtime & 0x07E0) >> 5; 658 $seconde = ($mtime & 0x001F) << 1; 659 660 $mtime = mktime($hour, $minute, $seconde, $month, $day, $year); 661 } else { 662 $mtime = time(); 663 } 664 665 return $mtime; 666 } 667 668 /** 669 * Returns a local file header for the given data 670 * 671 * @param int $offset location of the local header 672 * @param int $ts unix timestamp 673 * @param int $crc CRC32 checksum of the uncompressed data 674 * @param int $len length of the uncompressed data 675 * @param int $clen length of the compressed data 676 * @param string $name file name 677 * @param boolean|null $comp if compression is used, if null it's determined from $len != $clen 678 * @return string 679 */ 680 protected function makeCentralFileRecord($offset, $ts, $crc, $len, $clen, $name, $comp = null) 681 { 682 if(is_null($comp)) $comp = $len != $clen; 683 $comp = $comp ? 8 : 0; 684 $dtime = dechex($this->makeDosTime($ts)); 685 686 $header = "\x50\x4b\x01\x02"; // central file header signature 687 $header .= pack('v', 14); // version made by - VFAT 688 $header .= pack('v', 20); // version needed to extract - 2.0 689 $header .= pack('v', 0); // general purpose flag - no flags set 690 $header .= pack('v', $comp); // compression method - deflate|none 691 $header .= pack( 692 'H*', 693 $dtime[6] . $dtime[7] . 694 $dtime[4] . $dtime[5] . 695 $dtime[2] . $dtime[3] . 696 $dtime[0] . $dtime[1] 697 ); // last mod file time and date 698 $header .= pack('V', $crc); // crc-32 699 $header .= pack('V', $clen); // compressed size 700 $header .= pack('V', $len); // uncompressed size 701 $header .= pack('v', strlen($name)); // file name length 702 $header .= pack('v', 0); // extra field length 703 $header .= pack('v', 0); // file comment length 704 $header .= pack('v', 0); // disk number start 705 $header .= pack('v', 0); // internal file attributes 706 $header .= pack('V', 0); // external file attributes @todo was 0x32!? 707 $header .= pack('V', $offset); // relative offset of local header 708 $header .= $name; // file name 709 710 return $header; 711 } 712 713 /** 714 * Returns a local file header for the given data 715 * 716 * @param int $ts unix timestamp 717 * @param int $crc CRC32 checksum of the uncompressed data 718 * @param int $len length of the uncompressed data 719 * @param int $clen length of the compressed data 720 * @param string $name file name 721 * @param boolean|null $comp if compression is used, if null it's determined from $len != $clen 722 * @return string 723 */ 724 protected function makeLocalFileHeader($ts, $crc, $len, $clen, $name, $comp = null) 725 { 726 if(is_null($comp)) $comp = $len != $clen; 727 $comp = $comp ? 8 : 0; 728 $dtime = dechex($this->makeDosTime($ts)); 729 730 $header = "\x50\x4b\x03\x04"; // local file header signature 731 $header .= pack('v', 20); // version needed to extract - 2.0 732 $header .= pack('v', 0); // general purpose flag - no flags set 733 $header .= pack('v', $comp); // compression method - deflate|none 734 $header .= pack( 735 'H*', 736 $dtime[6] . $dtime[7] . 737 $dtime[4] . $dtime[5] . 738 $dtime[2] . $dtime[3] . 739 $dtime[0] . $dtime[1] 740 ); // last mod file time and date 741 $header .= pack('V', $crc); // crc-32 742 $header .= pack('V', $clen); // compressed size 743 $header .= pack('V', $len); // uncompressed size 744 $header .= pack('v', strlen($name)); // file name length 745 $header .= pack('v', 0); // extra field length 746 $header .= $name; 747 return $header; 748 } 749} 750