KXStudio Website https://kx.studio/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

zip.php 5.9KB

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. # From phpMyAdmin, modifications are purely aesthetic. (GPL v2 license)
  3. /**
  4. * Zip file creation class.
  5. * Makes zip files.
  6. *
  7. * Based on:
  8. *
  9. * http:#www.zend.com/codex.php?id=535&single=1
  10. * By Eric Mueller <eric@themepark.com>
  11. *
  12. * http:#www.zend.com/codex.php?id=470&single=1
  13. * by Denis125 <webmaster@atlant.ru>
  14. *
  15. * a patch from Peter Listiak <mlady@users.sourceforge.net> for last modified
  16. * date and time of the compressed file
  17. *
  18. * Official ZIP file format: http:#www.pkware.com/appnote.txt
  19. */
  20. class ZipFile {
  21. # Array to store compressed data
  22. var $datasec = array();
  23. # Central directory
  24. var $ctrl_dir = array();
  25. # End of central directory record
  26. var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
  27. # Last offset position
  28. var $old_offset = 0;
  29. /**
  30. * Function: unix2DosTime
  31. * Converts an Unix timestamp to a four byte DOS date and time format (date
  32. * in high two bytes, time in low two bytes allowing magnitude comparison).
  33. *
  34. * Parameters:
  35. * $unixtime - the current Unix timestamp
  36. *
  37. * Returns:
  38. * The current date in a four byte DOS format.
  39. */
  40. function unix2DosTime($unixtime = 0) {
  41. $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
  42. if ($timearray['year'] < 1980) {
  43. $timearray['year'] = 1980;
  44. $timearray['mon'] = 1;
  45. $timearray['mday'] = 1;
  46. $timearray['hours'] = 0;
  47. $timearray['minutes'] = 0;
  48. $timearray['seconds'] = 0;
  49. }
  50. return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) |
  51. ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);
  52. }
  53. /**
  54. * Function: addFile
  55. * Adds "file" to archive
  56. *
  57. * Parameters:
  58. * $data - The file's contents.
  59. * $name - The name of the file. Can be a directory.
  60. * $time - The current timestamp.
  61. */
  62. function addFile($data, $name, $time = 0) {
  63. $name = str_replace('\\', '/', $name);
  64. $dtime = dechex($this->unix2DosTime($time));
  65. $hexdtime = '\x' . $dtime[6] . $dtime[7]
  66. . '\x' . $dtime[4] . $dtime[5]
  67. . '\x' . $dtime[2] . $dtime[3]
  68. . '\x' . $dtime[0] . $dtime[1];
  69. eval('$hexdtime = "' . $hexdtime . '";');
  70. $fr = "\x50\x4b\x03\x04";
  71. $fr .= "\x14\x00"; # ver needed to extract
  72. $fr .= "\x00\x00"; # gen purpose bit flag
  73. $fr .= "\x08\x00"; # compression method
  74. $fr .= $hexdtime; # last mod time and date
  75. # "local file header" segment
  76. $unc_len = strlen($data);
  77. $crc = crc32($data);
  78. $zdata = gzcompress($data);
  79. $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); # fix crc bug
  80. $c_len = strlen($zdata);
  81. $fr .= pack('V', $crc); # crc32
  82. $fr .= pack('V', $c_len); # compressed filesize
  83. $fr .= pack('V', $unc_len); # uncompressed filesize
  84. $fr .= pack('v', strlen($name)); # length of filename
  85. $fr .= pack('v', 0); # extra field length
  86. $fr .= $name;
  87. # "file data" segment
  88. $fr .= $zdata;
  89. # add this entry to array
  90. $this -> datasec[] = $fr;
  91. # now add to central directory record
  92. $cdrec = "\x50\x4b\x01\x02";
  93. $cdrec.= "\x00\x00"; # version made by
  94. $cdrec.= "\x14\x00"; # version needed to extract
  95. $cdrec.= "\x00\x00"; # gen purpose bit flag
  96. $cdrec.= "\x08\x00"; # compression method
  97. $cdrec.= $hexdtime; # last mod time & date
  98. $cdrec.= pack('V', $crc); # crc32
  99. $cdrec.= pack('V', $c_len); # compressed filesize
  100. $cdrec.= pack('V', $unc_len); # uncompressed filesize
  101. $cdrec.= pack('v', strlen($name)); # length of filename
  102. $cdrec.= pack('v', 0); # extra field length
  103. $cdrec.= pack('v', 0); # file comment length
  104. $cdrec.= pack('v', 0); # disk number start
  105. $cdrec.= pack('v', 0); # internal file attributes
  106. $cdrec.= pack('V', 32); # external file attributes - 'archive' bit set
  107. $cdrec .= pack('V', $this -> old_offset); # relative offset of local header
  108. $this -> old_offset += strlen($fr);
  109. $cdrec .= $name;
  110. # optional extra field, file comment goes here
  111. # save to central directory
  112. $this -> ctrl_dir[] = $cdrec;
  113. }
  114. /**
  115. * Function: file
  116. * Dumps out file
  117. *
  118. * Returns:
  119. * the zipped file
  120. */
  121. function file() {
  122. $data = implode('', $this -> datasec);
  123. $ctrldir = implode('', $this -> ctrl_dir);
  124. return
  125. $data .
  126. $ctrldir .
  127. $this -> eof_ctrl_dir .
  128. pack('v', sizeof($this -> ctrl_dir)) . # total # of entries "on this disk"
  129. pack('v', sizeof($this -> ctrl_dir)) . # total # of entries overall
  130. pack('V', strlen($ctrldir)) . # size of central dir
  131. pack('V', strlen($data)) . # offset to start of central dir
  132. "\x00\x00"; # .zip file comment length
  133. }
  134. }