5 * ZIP file unpack classes. Contributed to the phpMyAdmin project.
8 * @package File-Formats-ZIP
10 * @filesource unzip.lib.php
13 * @author Holger Boskugel <vbwebprofi@gmx.de>
14 * @copyright Copyright © 2003, Holger Boskugel, Berlin, Germany
15 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
18 * 2003-12-02 - HB : Patched : naming bug : Time/Size of file
19 * Added : ZIP file comment
20 * Added : Check BZIP2 support of PHP
21 * 2003-11-29 - HB * Initial version
25 * Unzip class, which retrieves entries from ZIP files.
27 * Supports only the compression modes
34 * {@link http://www.pkware.com/products/enterprise/white_papers/appnote.html
35 * * Official ZIP file format}<BR>
36 * {@link http://msdn.microsoft.com/library/en-us/w98ddk/hh/w98ddk/storage_5l4m.asp
37 * * Microsoft DOS date/time format}
40 * @package File-Formats-ZIP
43 * @author Holger Boskugel <vbwebprofi@gmx.de>
44 * @uses SimpleUnzipEntry
45 * @example example.unzip.php Two examples
50 * Array to store file entries
61 * Array to store file entries
68 var $Entries = array();
71 * Name of the ZIP file
81 * Size of the ZIP file
91 * Time of the ZIP file (unix timestamp)
101 * Contructor of the class
103 * @param string File name
104 * @return SimpleUnzip Instanced class
106 * @uses SimpleUnzip::ReadFile() Opens file on new if specified
109 function SimpleUnzip($in_FileName = '') {
110 if($in_FileName !== '') {
111 SimpleUnzip
::ReadFile($in_FileName);
113 } // end of the 'SimpleUnzip' constructor
118 * @return integer Count of ZIP entries
124 return count($this->Entries
);
125 } // end of the 'Count()' method
128 * Gets data of the specified ZIP entry
130 * @param integer Index of the ZIP entry
131 * @return mixed Data for the ZIP entry
132 * @uses SimpleUnzipEntry::$Data
136 function GetData($in_Index) {
137 return $this->Entries
[$in_Index]->Data
;
138 } // end of the 'GetData()' method
141 * Gets an entry of the ZIP file
143 * @param integer Index of the ZIP entry
144 * @return SimpleUnzipEntry Entry of the ZIP file
149 function GetEntry($in_Index) {
150 return $this->Entries
[$in_Index];
151 } // end of the 'GetEntry()' method
154 * Gets error code for the specified ZIP entry
156 * @param integer Index of the ZIP entry
157 * @return integer Error code for the ZIP entry
158 * @uses SimpleUnzipEntry::$Error
162 function GetError($in_Index) {
163 return $this->Entries
[$in_Index]->Error
;
164 } // end of the 'GetError()' method
167 * Gets error message for the specified ZIP entry
169 * @param integer Index of the ZIP entry
170 * @return string Error message for the ZIP entry
171 * @uses SimpleUnzipEntry::$ErrorMsg
175 function GetErrorMsg($in_Index) {
176 return $this->Entries
[$in_Index]->ErrorMsg
;
177 } // end of the 'GetErrorMsg()' method
180 * Gets file name for the specified ZIP entry
182 * @param integer Index of the ZIP entry
183 * @return string File name for the ZIP entry
184 * @uses SimpleUnzipEntry::$Name
188 function GetName($in_Index) {
189 return $this->Entries
[$in_Index]->Name
;
190 } // end of the 'GetName()' method
193 * Gets path of the file for the specified ZIP entry
195 * @param integer Index of the ZIP entry
196 * @return string Path of the file for the ZIP entry
197 * @uses SimpleUnzipEntry::$Path
201 function GetPath($in_Index) {
202 return $this->Entries
[$in_Index]->Path
;
203 } // end of the 'GetPath()' method
206 * Gets file time for the specified ZIP entry
208 * @param integer Index of the ZIP entry
209 * @return integer File time for the ZIP entry (unix timestamp)
210 * @uses SimpleUnzipEntry::$Time
214 function GetTime($in_Index) {
215 return $this->Entries
[$in_Index]->Time
;
216 } // end of the 'GetTime()' method
219 * Reads ZIP file and extracts the entries
221 * @param string File name of the ZIP archive
222 * @return array ZIP entry list (see also class variable {@link $Entries $Entries})
223 * @uses SimpleUnzipEntry For the entries
227 function ReadFile($in_FileName) {
228 $this->Entries
= array();
230 // Get file parameters
231 $this->Name
= $in_FileName;
232 $this->Time
= filemtime($in_FileName);
233 $this->Size
= filesize($in_FileName);
236 $oF = fopen($in_FileName, 'rb');
237 $vZ = fread($oF, $this->Size
);
241 // Cut end of central directory
242 $aE = explode("\x50\x4b\x05\x06", $vZ);
244 // Easiest way, but not sure if format changes
245 //$this->Comment = substr($aE[1], 18);
248 $aP = unpack('x16/v1CL', $aE[1]);
249 $this->Comment
= substr($aE[1], 18, $aP['CL']);
251 // Translates end of line from other operating systems
252 $this->Comment
= strtr($this->Comment
, array("\r\n" => "\n",
256 // Cut the entries from the central directory
257 $aE = explode("\x50\x4b\x01\x02", $vZ);
258 // Explode to each part
259 $aE = explode("\x50\x4b\x03\x04", $aE[0]);
260 // Shift out spanning signature or empty entry
263 // Loop through the entries
264 foreach($aE as $vZ) {
268 // Retrieving local file header information
269 $aP = unpack('v1VN/v1GPF/v1CM/v1FT/v1FD/V1CRC/V1CS/V1UCS/v1FNL', $vZ);
270 // Check if data is encrypted
271 $bE = ($aP['GPF'] && 0x0001) ?
TRUE : FALSE;
274 // Special case : value block after the compressed data
275 if($aP['GPF'] & 0x0008) {
276 $aP1 = unpack('V1CRC/V1CS/V1UCS', substr($vZ, -12));
278 $aP['CRC'] = $aP1['CRC'];
279 $aP['CS'] = $aP1['CS'];
280 $aP['UCS'] = $aP1['UCS'];
282 $vZ = substr($vZ, 0, -12);
285 // Getting stored filename
286 $aI['N'] = substr($vZ, 26, $nF);
288 if(substr($aI['N'], -1) == '/') {
289 // is a directory entry - will be skipped
293 // Truncate full filename in path and filename
294 $aI['P'] = dirname($aI['N']);
295 $aI['P'] = $aI['P'] == '.' ?
'' : $aI['P'];
296 $aI['N'] = basename($aI['N']);
298 $vZ = substr($vZ, 26 +
$nF);
300 if(strlen($vZ) != $aP['CS']) {
302 $aI['EM'] = 'Compressed size is not equal with the value in header information.';
307 $aI['EM'] = 'File is encrypted, which is not supported from this class.';
312 // Here is nothing to do, the file ist flat.
316 $vZ = gzinflate($vZ);
321 if(! extension_loaded('bz2')) {
322 if(strtoupper(substr(PHP_OS
, 0, 3)) == 'WIN') {
330 if(extension_loaded('bz2')) {
332 $vZ = bzdecompress($vZ);
337 $aI['EM'] = "PHP BZIP2 extension not available.";
345 $aI['EM'] = "De-/Compression method {$aP['CM']} is not supported.";
353 $aI['EM'] = 'Decompression of data failed.';
356 if(strlen($vZ) != $aP['UCS']) {
358 $aI['EM'] = 'Uncompressed size is not equal with the value in header information.';
361 if(crc32($vZ) != $aP['CRC']) {
363 $aI['EM'] = 'CRC32 checksum is not equal with the value in header information.';
375 // DOS to UNIX timestamp
376 $aI['T'] = mktime(($aP['FT'] & 0xf800) >> 11,
377 ($aP['FT'] & 0x07e0) >> 5,
378 ($aP['FT'] & 0x001f) << 1,
379 ($aP['FD'] & 0x01e0) >> 5,
380 ($aP['FD'] & 0x001f),
381 (($aP['FD'] & 0xfe00) >> 9) +
1980);
383 $this->Entries
[] = &new SimpleUnzipEntry($aI);
384 } // end for each entries
386 return $this->Entries
;
387 } // end of the 'ReadFile()' method
388 } // end of the 'SimpleUnzip' class
391 * Entry of the ZIP file.
393 * @category phpPublic
394 * @package File-Formats-ZIP
397 * @author Holger Boskugel <vbwebprofi@gmx.de>
398 * @example example.unzip.php Two examples
400 class SimpleUnzipEntry
{
402 * Data of the file entry
406 * @see SimpleUnzipEntry()
412 * Error of the file entry
414 * - 0 : No error raised.<BR>
415 * - 1 : Compressed size is not equal with the value in header information.<BR>
416 * - 2 : Decompression of data failed.<BR>
417 * - 3 : Uncompressed size is not equal with the value in header information.<BR>
418 * - 4 : CRC32 checksum is not equal with the value in header information.<BR>
419 * - 5 : File is encrypted, which is not supported from this class.<BR>
420 * - 6 : De-/Compression method ... is not supported.<BR>
421 * - 7 : PHP BZIP2 extension not available.
425 * @see SimpleUnzipEntry()
431 * Error message of the file entry
435 * @see SimpleUnzipEntry()
441 * File name of the file entry
445 * @see SimpleUnzipEntry()
451 * File path of the file entry
455 * @see SimpleUnzipEntry()
461 * File time of the file entry (unix timestamp)
465 * @see SimpleUnzipEntry()
471 * Contructor of the class
473 * @param array Entry datas
474 * @return SimpleUnzipEntry Instanced class
478 function SimpleUnzipEntry($in_Entry) {
479 $this->Data
= $in_Entry['D'];
480 $this->Error
= $in_Entry['E'];
481 $this->ErrorMsg
= $in_Entry['EM'];
482 $this->Name
= $in_Entry['N'];
483 $this->Path
= $in_Entry['P'];
484 $this->Time
= $in_Entry['T'];
485 } // end of the 'SimpleUnzipEntry' constructor
486 } // end of the 'SimpleUnzipEntry' class