2 /* vim: set expandtab sw=4 ts=4 sts=4: */
10 * Zip file creation class.
15 * http://www.zend.com/codex.php?id=535&single=1
16 * By Eric Mueller <eric@themepark.com>
18 * http://www.zend.com/codex.php?id=470&single=1
19 * by Denis125 <webmaster@atlant.ru>
21 * a patch from Peter Listiak <mlady@users.sourceforge.net> for last modified
22 * date and time of the compressed file
24 * Official ZIP file format: http://www.pkware.com/appnote.txt
32 * Array to store compressed data
36 var $datasec = array();
41 * @var array $ctrl_dir
43 var $ctrl_dir = array();
46 * End of central directory record
48 * @var string $eof_ctrl_dir
50 var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
53 * Last offset position
55 * @var integer $old_offset
61 * Converts an Unix timestamp to a four byte DOS date and time format (date
62 * in high two bytes, time in low two bytes allowing magnitude comparison).
64 * @param integer the current Unix timestamp
66 * @return integer the current date in a four byte DOS format
70 function unix2DosTime($unixtime = 0) {
71 $timearray = ($unixtime == 0) ?
getdate() : getdate($unixtime);
73 if ($timearray['year'] < 1980) {
74 $timearray['year'] = 1980;
75 $timearray['mon'] = 1;
76 $timearray['mday'] = 1;
77 $timearray['hours'] = 0;
78 $timearray['minutes'] = 0;
79 $timearray['seconds'] = 0;
82 return (($timearray['year'] - 1980) << 25) |
($timearray['mon'] << 21) |
($timearray['mday'] << 16) |
83 ($timearray['hours'] << 11) |
($timearray['minutes'] << 5) |
($timearray['seconds'] >> 1);
84 } // end of the 'unix2DosTime()' method
88 * Adds "file" to archive
90 * @param string file contents
91 * @param string name of the file in the archive (may contains the path)
92 * @param integer the current timestamp
96 function addFile($data, $name, $time = 0)
98 $name = str_replace('\\', '/', $name);
100 $dtime = dechex($this->unix2DosTime($time));
101 $hexdtime = '\x' . $dtime[6] . $dtime[7]
102 . '\x' . $dtime[4] . $dtime[5]
103 . '\x' . $dtime[2] . $dtime[3]
104 . '\x' . $dtime[0] . $dtime[1];
105 eval('$hexdtime = "' . $hexdtime . '";');
107 $fr = "\x50\x4b\x03\x04";
108 $fr .= "\x14\x00"; // ver needed to extract
109 $fr .= "\x00\x00"; // gen purpose bit flag
110 $fr .= "\x08\x00"; // compression method
111 $fr .= $hexdtime; // last mod time and date
113 // "local file header" segment
114 $unc_len = strlen($data);
116 $zdata = gzcompress($data);
117 $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
118 $c_len = strlen($zdata);
119 $fr .= pack('V', $crc); // crc32
120 $fr .= pack('V', $c_len); // compressed filesize
121 $fr .= pack('V', $unc_len); // uncompressed filesize
122 $fr .= pack('v', strlen($name)); // length of filename
123 $fr .= pack('v', 0); // extra field length
126 // "file data" segment
129 // "data descriptor" segment (optional but necessary if archive is not
131 // nijel(2004-10-19): this seems not to be needed at all and causes
132 // problems in some cases (bug #1037737)
133 //$fr .= pack('V', $crc); // crc32
134 //$fr .= pack('V', $c_len); // compressed filesize
135 //$fr .= pack('V', $unc_len); // uncompressed filesize
137 // add this entry to array
138 $this -> datasec
[] = $fr;
140 // now add to central directory record
141 $cdrec = "\x50\x4b\x01\x02";
142 $cdrec .= "\x00\x00"; // version made by
143 $cdrec .= "\x14\x00"; // version needed to extract
144 $cdrec .= "\x00\x00"; // gen purpose bit flag
145 $cdrec .= "\x08\x00"; // compression method
146 $cdrec .= $hexdtime; // last mod time & date
147 $cdrec .= pack('V', $crc); // crc32
148 $cdrec .= pack('V', $c_len); // compressed filesize
149 $cdrec .= pack('V', $unc_len); // uncompressed filesize
150 $cdrec .= pack('v', strlen($name)); // length of filename
151 $cdrec .= pack('v', 0); // extra field length
152 $cdrec .= pack('v', 0); // file comment length
153 $cdrec .= pack('v', 0); // disk number start
154 $cdrec .= pack('v', 0); // internal file attributes
155 $cdrec .= pack('V', 32); // external file attributes - 'archive' bit set
157 $cdrec .= pack('V', $this -> old_offset
); // relative offset of local header
158 $this -> old_offset +
= strlen($fr);
162 // optional extra field, file comment goes here
163 // save to central directory
164 $this -> ctrl_dir
[] = $cdrec;
165 } // end of the 'addFile()' method
171 * @return string the zipped file
177 $data = implode('', $this -> datasec
);
178 $ctrldir = implode('', $this -> ctrl_dir
);
183 $this -> eof_ctrl_dir
.
184 pack('v', sizeof($this -> ctrl_dir
)) . // total # of entries "on this disk"
185 pack('v', sizeof($this -> ctrl_dir
)) . // total # of entries overall
186 pack('V', strlen($ctrldir)) . // size of central dir
187 pack('V', strlen($data)) . // offset to start of central dir
188 "\x00\x00"; // .zip file comment length
189 } // end of the 'file()' method
191 } // end of the 'zipfile' class