2 /* vim: set expandtab sw=4 ts=4 sts=4: */
8 if (! defined('PHPMYADMIN')) {
13 * Zip file creation class.
18 * @see Official ZIP file format: http://www.pkware.com/support/zip-app-note
23 * Whether to echo zip as it's built or return as string from -> file
25 * @var boolean $doWrite
30 * Array to store compressed data
34 var $datasec = array();
39 * @var array $ctrl_dir
41 var $ctrl_dir = array();
44 * End of central directory record
46 * @var string $eof_ctrl_dir
48 var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
51 * Last offset position
53 * @var integer $old_offset
59 * Sets member variable this -> doWrite to true
60 * - Should be called immediately after class instantiation
61 * - If set to true, then ZIP archive are echo'ed to STDOUT as each
62 * file is added via this -> addfile(), and central directories are
63 * echoed to STDOUT on final call to this -> file(). Also,
64 * this -> file() returns an empty string so it is safe to issue a
65 * "echo $zipfile;" command
73 $this -> doWrite
= true;
74 } // end of the 'setDoWrite()' method
77 * Converts an Unix timestamp to a four byte DOS date and time format (date
78 * in high two bytes, time in low two bytes allowing magnitude comparison).
80 * @param integer $unixtime the current Unix timestamp
82 * @return integer the current date in a four byte DOS format
86 function unix2DosTime($unixtime = 0)
88 $timearray = ($unixtime == 0) ?
getdate() : getdate($unixtime);
90 if ($timearray['year'] < 1980) {
91 $timearray['year'] = 1980;
92 $timearray['mon'] = 1;
93 $timearray['mday'] = 1;
94 $timearray['hours'] = 0;
95 $timearray['minutes'] = 0;
96 $timearray['seconds'] = 0;
99 return (($timearray['year'] - 1980) << 25)
100 |
($timearray['mon'] << 21)
101 |
($timearray['mday'] << 16)
102 |
($timearray['hours'] << 11)
103 |
($timearray['minutes'] << 5)
104 |
($timearray['seconds'] >> 1);
105 } // end of the 'unix2DosTime()' method
109 * Adds "file" to archive
111 * @param string $data file contents
112 * @param string $name name of the file in the archive (may contains the path)
113 * @param integer $time the current timestamp
119 function addFile($data, $name, $time = 0)
121 $name = str_replace('\\', '/', $name);
123 $hexdtime = pack('V', $this->unix2DosTime($time));
125 $fr = "\x50\x4b\x03\x04";
126 $fr .= "\x14\x00"; // ver needed to extract
127 $fr .= "\x00\x00"; // gen purpose bit flag
128 $fr .= "\x08\x00"; // compression method
129 $fr .= $hexdtime; // last mod time and date
131 // "local file header" segment
132 $unc_len = strlen($data);
134 $zdata = gzcompress($data);
135 $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
136 $c_len = strlen($zdata);
137 $fr .= pack('V', $crc); // crc32
138 $fr .= pack('V', $c_len); // compressed filesize
139 $fr .= pack('V', $unc_len); // uncompressed filesize
140 $fr .= pack('v', strlen($name)); // length of filename
141 $fr .= pack('v', 0); // extra field length
144 // "file data" segment
147 // echo this entry on the fly, ...
148 if ($this -> doWrite
) {
150 } else { // ... OR add this entry to array
151 $this -> datasec
[] = $fr;
154 // now add to central directory record
155 $cdrec = "\x50\x4b\x01\x02";
156 $cdrec .= "\x00\x00"; // version made by
157 $cdrec .= "\x14\x00"; // version needed to extract
158 $cdrec .= "\x00\x00"; // gen purpose bit flag
159 $cdrec .= "\x08\x00"; // compression method
160 $cdrec .= $hexdtime; // last mod time & date
161 $cdrec .= pack('V', $crc); // crc32
162 $cdrec .= pack('V', $c_len); // compressed filesize
163 $cdrec .= pack('V', $unc_len); // uncompressed filesize
164 $cdrec .= pack('v', strlen($name)); // length of filename
165 $cdrec .= pack('v', 0); // extra field length
166 $cdrec .= pack('v', 0); // file comment length
167 $cdrec .= pack('v', 0); // disk number start
168 $cdrec .= pack('v', 0); // internal file attributes
169 $cdrec .= pack('V', 32); // external file attributes
170 // - 'archive' bit set
172 $cdrec .= pack('V', $this -> old_offset
); // relative offset of local header
173 $this -> old_offset +
= strlen($fr);
177 // optional extra field, file comment goes here
178 // save to central directory
179 $this -> ctrl_dir
[] = $cdrec;
180 } // end of the 'addFile()' method
184 * Echo central dir if ->doWrite==true, else build string to return
186 * @return string if ->doWrite {empty string} else the ZIP file contents
192 $ctrldir = implode('', $this -> ctrl_dir
);
194 $this -> eof_ctrl_dir
.
195 pack('v', sizeof($this -> ctrl_dir
)) . //total #of entries "on this disk"
196 pack('v', sizeof($this -> ctrl_dir
)) . //total #of entries overall
197 pack('V', strlen($ctrldir)) . //size of central dir
198 pack('V', $this -> old_offset
) . //offset to start of central dir
199 "\x00\x00"; //.zip file comment length
201 if ($this -> doWrite
) { // Send central directory & end ctrl dir to STDOUT
203 return ""; // Return empty string
204 } else { // Return entire ZIP archive as string
205 $data = implode('', $this -> datasec
);
206 return $data . $header;
208 } // end of the 'file()' method
210 } // end of the 'ZipFile' class