zip.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. class zip_lib
  3. {
  4. var $datasec = array();
  5. var $ctrl_dir = array();
  6. var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
  7. var $old_offset = 0;
  8. function unix2DosTime($unixtime = 0) {
  9. $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
  10. if ($timearray['year'] < 1980) {
  11. $timearray['year'] = 1980;
  12. $timearray['mon'] = 1;
  13. $timearray['mday'] = 1;
  14. $timearray['hours'] = 0;
  15. $timearray['minutes'] = 0;
  16. $timearray['seconds'] = 0;
  17. } // end if
  18. return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) |
  19. ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);
  20. } // end of the 'unix2DosTime()' method
  21. function addFile($data, $name, $time = 0)
  22. {
  23. $name = str_replace('\\', '/', $name);
  24. $dtime = dechex($this->unix2DosTime($time));
  25. $hexdtime = '\x' . $dtime[6] . $dtime[7]
  26. . '\x' . $dtime[4] . $dtime[5]
  27. . '\x' . $dtime[2] . $dtime[3]
  28. . '\x' . $dtime[0] . $dtime[1];
  29. eval('$hexdtime = "' . $hexdtime . '";');
  30. $fr = "\x50\x4b\x03\x04";
  31. $fr .= "\x14\x00"; // ver needed to extract
  32. $fr .= "\x00\x00"; // gen purpose bit flag
  33. $fr .= "\x08\x00"; // compression method
  34. $fr .= $hexdtime; // last mod time and date
  35. // "local file header" segment
  36. $unc_len = strlen($data);
  37. $crc = crc32($data);
  38. $zdata = gzcompress($data);
  39. $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
  40. $c_len = strlen($zdata);
  41. $fr .= pack('V', $crc); // crc32
  42. $fr .= pack('V', $c_len); // compressed filesize
  43. $fr .= pack('V', $unc_len); // uncompressed filesize
  44. $fr .= pack('v', strlen($name)); // length of filename
  45. $fr .= pack('v', 0); // extra field length
  46. $fr .= $name;
  47. // "file data" segment
  48. $fr .= $zdata;
  49. // "data descriptor" segment (optional but necessary if archive is not
  50. // served as file)
  51. $fr .= pack('V', $crc); // crc32
  52. $fr .= pack('V', $c_len); // compressed filesize
  53. $fr .= pack('V', $unc_len); // uncompressed filesize
  54. // add this entry to array
  55. $this -> datasec[] = $fr;
  56. // now add to central directory record
  57. $cdrec = "\x50\x4b\x01\x02";
  58. $cdrec .= "\x00\x00"; // version made by
  59. $cdrec .= "\x14\x00"; // version needed to extract
  60. $cdrec .= "\x00\x00"; // gen purpose bit flag
  61. $cdrec .= "\x08\x00"; // compression method
  62. $cdrec .= $hexdtime; // last mod time & date
  63. $cdrec .= pack('V', $crc); // crc32
  64. $cdrec .= pack('V', $c_len); // compressed filesize
  65. $cdrec .= pack('V', $unc_len); // uncompressed filesize
  66. $cdrec .= pack('v', strlen($name) ); // length of filename
  67. $cdrec .= pack('v', 0 ); // extra field length
  68. $cdrec .= pack('v', 0 ); // file comment length
  69. $cdrec .= pack('v', 0 ); // disk number start
  70. $cdrec .= pack('v', 0 ); // internal file attributes
  71. $cdrec .= pack('V', 32 ); // external file attributes - 'archive' bit set
  72. $cdrec .= pack('V', $this -> old_offset ); // relative offset of local header
  73. $this -> old_offset += strlen($fr);
  74. $cdrec .= $name;
  75. // optional extra field, file comment goes here
  76. // save to central directory
  77. $this -> ctrl_dir[] = $cdrec;
  78. } // end of the 'addFile()' method
  79. /**
  80. * Dumps out file
  81. *
  82. * @return string the zipped file
  83. *
  84. * @access public
  85. */
  86. function file()
  87. {
  88. $data = implode('', $this -> datasec);
  89. $ctrldir = implode('', $this -> ctrl_dir);
  90. return
  91. $data .
  92. $ctrldir .
  93. $this -> eof_ctrl_dir .
  94. pack('v', sizeof($this -> ctrl_dir)) . // total # of entries "on this disk"
  95. pack('v', sizeof($this -> ctrl_dir)) . // total # of entries overall
  96. pack('V', strlen($ctrldir)) . // size of central dir
  97. pack('V', strlen($data)) . // offset to start of central dir
  98. "\x00\x00"; // .zip file comment length
  99. } // end of the 'file()' method
  100. function addFiles($files)
  101. {
  102. foreach($files as $file)
  103. {
  104. if (is_file($file)) //directory check
  105. {
  106. $data = implode("",file($file));
  107. $this->addFile($data,$file);
  108. }
  109. }
  110. }
  111. function output($file)
  112. {
  113. $fp=fopen($file,"w");
  114. fwrite($fp,$this->file());
  115. fclose($fp);
  116. }
  117. }
  118. ?>