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