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