-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
ZipReader.php
562 lines (478 loc) · 14.3 KB
/
ZipReader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
<?php
/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/
namespace Contao;
/**
* Reads .zip files and unpacks their content
*
* Usage:
*
* $zip = new ZipReader('test.zip');
*
* while ($zip->next())
* {
* echo $zip->->file_name;
* }
*
* @property integer $number_of_this_disk The number of this disk
* @property integer $number_of_disk_with_cd The number of the disk with the start of the central directory
* @property integer $total_cd_entries_disk The total number of entries in the central directory on this disk
* @property integer $total_cd_entries The total number of entries in the central directory
* @property integer $size_of_cd The size of the central directory
* @property integer $offset_start_cd The offset of the start of the central directory with respect to the starting disk number
* @property integer $zipfile_comment_length The ZIP file comment length
* @property string $zipfile_comment The ZIP file comment
* @property integer $version_made_by The version made by
* @property integer $version_needed_to_extract The version needed to extract
* @property integer $general_purpose_bit_flag General purpose bit flag
* @property integer $compression_method The compression method
* @property integer $last_mod_file_time The last modification file time
* @property integer $last_mod_file_date The last modification file date
* @property integer $last_mod_file_unix The last modification file unix timestamp
* @property integer $crc-32 The CRC32 checksum
* @property integer $compressed_size The compressed size
* @property integer $uncompressed_size The uncompressed size
* @property integer $file_name_length The file name length
* @property integer $extra_field_length The extra field length
* @property integer $file_comment_length The file comment length
* @property integer $disk_number_start Disk number start
* @property integer $internal_file_attributes Internal file attributes
* @property integer $external_file_attributes External file attributes
* @property integer $offset_of_local_header The relative offset of local header
* @property string $file_name The file name
* @property string $file_basename The file basename
* @property string $file_dirname The file dirname
* @property string $extra_field The extra field
* @property string $file_comment The file comment
*
* @author Leo Feyer <https://github.com/leofeyer>
*/
class ZipReader
{
/**
* File signature
*/
const FILE_SIGNATURE = "\x50\x4b\x03\x04";
/**
* Central directory begin marker
*/
const CENTRAL_DIR_START = "\x50\x4b\x01\x02";
/**
* Central directory end marker
*/
const CENTRAL_DIR_END = "\x50\x4b\x05\x06";
/**
* File handle
* @var resource
*/
protected $resFile;
/**
* File name
* @var string
*/
protected $strFile;
/**
* Root dir
* @var string
*/
protected $strRootDir;
/**
* Current file index
* @var integer
*/
private $intIndex = -1;
/**
* Last file index
* @var integer
*/
private $intLast = 0;
/**
* Archive header
* @var array
*/
protected $arrHeader = array();
/**
* File list
* @var array
*/
protected $arrFiles = array();
/**
* Open the archive and return the file handle
*
* @param string $strFile The file path
*
* @throws \Exception If $strFile does not exist or cannot be opened
*/
public function __construct($strFile)
{
// Handle open_basedir restrictions
if ($strFile == '.')
{
$strFile = '';
}
$this->strFile = $strFile;
$this->strRootDir = System::getContainer()->getParameter('kernel.project_dir');
// Check if file exists
if (!file_exists($this->strRootDir . '/' . $strFile))
{
throw new \Exception("Could not find file $strFile");
}
$this->resFile = @fopen($this->strRootDir . '/' . $strFile, 'r');
// Could not open file
if (!\is_resource($this->resFile))
{
throw new \Exception("Could not open file $strFile");
}
$this->readCentralDirectory();
}
/**
* Close the file handle
*/
public function __destruct()
{
fclose($this->resFile);
}
/**
* Return a property of the archive header or the current file
*
* @param string $strKey The property name
*
* @return mixed|null The property value or null
*/
public function __get($strKey)
{
switch ($strKey)
{
// Header
case 'number_of_this_disk':
case 'number_of_disk_with_cd':
case 'total_cd_entries_disk':
case 'total_cd_entries':
case 'size_of_cd':
case 'offset_start_cd':
case 'zipfile_comment_length':
case 'zipfile_comment':
return $this->arrHeader[$strKey];
// Current file
default:
if ($this->intIndex < 0)
{
$this->first();
}
if (isset($this->arrFiles[$this->intIndex][$strKey]))
{
return $this->arrFiles[$this->intIndex][$strKey];
}
break;
}
return null;
}
/**
* Return a list of all files in the archive
*
* @return array The files array
*/
public function getFileList()
{
$arrFiles = array();
foreach ($this->arrFiles as $arrFile)
{
$arrFiles[] = $arrFile['file_name'];
}
return $arrFiles;
}
/**
* Set the internal pointer to a particular file
*
* @param string $strName The file name
*
* @return boolean True if the file was found
*/
public function getFile($strName)
{
foreach ($this->arrFiles as $k=>$v)
{
if ($strName == $v['file_name'])
{
$this->intIndex = $k;
return true;
}
}
return false;
}
/**
* Go to the first file of the archive
*
* @return ZipReader The object instance
*/
public function first()
{
$this->intIndex = 0;
return $this;
}
/**
* Go to the next file of the archive
*
* @return ZipReader|boolean The object instance or false if there is no next file
*/
public function next()
{
if ($this->intIndex >= $this->intLast)
{
return false;
}
++$this->intIndex;
return $this;
}
/**
* Go to the previous file of the archive
*
* @return ZipReader|boolean The object instance or false if there is no previous file
*/
public function prev()
{
if ($this->intIndex <= 0)
{
return false;
}
--$this->intIndex;
return $this;
}
/**
* Go to the last file of the archive
*
* @return ZipReader The object instance
*/
public function last()
{
$this->intIndex = $this->intLast;
return $this;
}
/**
* Return the current file as array
*
* @return array The current file array
*/
public function current()
{
if ($this->intIndex < 0)
{
$this->first();
}
return $this->arrFiles[$this->intIndex];
}
/**
* Reset the archive
*
* @return ZipReader The object instance
*/
public function reset()
{
$this->intIndex = -1;
return $this;
}
/**
* Unzip the current file and return its contents as string
*
* @return string The file content
*
* @throws \Exception If the current file is encrypted or not a compressed file
*/
public function unzip()
{
if ($this->intIndex < 0)
{
$this->first();
}
$strName = $this->arrFiles[$this->intIndex]['file_name'];
// Encrypted files are not supported
if ($this->arrFiles[$this->intIndex]['general_purpose_bit_flag'] & 0x0001)
{
throw new \Exception("File $strName is encrypted");
}
// Reposition pointer
if (@fseek($this->resFile, $this->arrFiles[$this->intIndex]['offset_of_local_header']) !== 0)
{
throw new \Exception("Cannot reposition pointer");
}
$strSignature = @fread($this->resFile, 4);
// Not a file
if ($strSignature != self::FILE_SIGNATURE)
{
throw new \Exception("$strName is not a compressed file");
}
// Get extra field length
fseek($this->resFile, 24, SEEK_CUR);
$arrEFL = unpack('v', @fread($this->resFile, 2));
// Reposition pointer
fseek($this->resFile, ($this->arrFiles[$this->intIndex]['file_name_length'] + $arrEFL[1]), SEEK_CUR);
// Empty file
if ($this->arrFiles[$this->intIndex]['compressed_size'] < 1)
{
return '';
}
// Read data
$strBuffer = @fread($this->resFile, $this->arrFiles[$this->intIndex]['compressed_size']);
// Decompress data
switch ($this->arrFiles[$this->intIndex]['compression_method'])
{
// Stored
case 0:
break;
// Deflated
case 8:
$strBuffer = gzinflate($strBuffer);
break;
// BZIP2
case 12:
if (!\extension_loaded('bz2'))
{
throw new \Exception('PHP extension "bz2" required to decompress BZIP2 files');
}
$strBuffer = bzdecompress($strBuffer);
break;
// Unknown
default:
throw new \Exception('Unknown compression method');
}
// Check uncompressed data
if ($strBuffer === false)
{
throw new \Exception('Could not decompress data');
}
// Check uncompressed size
if (\strlen($strBuffer) != $this->arrFiles[$this->intIndex]['uncompressed_size'])
{
throw new \Exception('Size of the uncompressed file does not match header value');
}
return $strBuffer;
}
/**
* Return a list of all files in the archive
*
* @throws \Exception If the central directory cannot be found
*/
protected function readCentralDirectory()
{
$strMbCharset = null;
// Set the mbstring encoding to ASCII (see #5842)
if (\ini_get('mbstring.func_overload') > 0)
{
$strMbCharset = mb_internal_encoding();
if (mb_internal_encoding('ASCII') === false)
{
$strMbCharset = null;
}
}
$intOffset = 0;
$pos = 0;
$intInterval = min(filesize($this->strRootDir . '/' . $this->strFile), 1024);
$strBuffer = '';
// Read to delimiter
do
{
$intOffset -= $intInterval;
$fseek = @fseek($this->resFile, $intOffset, SEEK_END);
$strBuffer = @fread($this->resFile, abs($intOffset)) . $strBuffer;
}
while ($fseek != -1 && ($pos = strpos($strBuffer, self::CENTRAL_DIR_END)) === false);
// Reposition pointer
fseek($this->resFile, ($intOffset + $pos), SEEK_END);
$strSignature = @fread($this->resFile, 4);
// Read archive header
if ($strSignature != self::CENTRAL_DIR_END)
{
throw new \Exception('Error reading central directory');
}
$arrHeader = array();
$arrHeader['number_of_this_disk'] = unpack('v', @fread($this->resFile, 2));
$arrHeader['number_of_disk_with_cd'] = unpack('v', @fread($this->resFile, 2));
$arrHeader['total_cd_entries_disk'] = unpack('v', @fread($this->resFile, 2));
$arrHeader['total_cd_entries'] = unpack('v', @fread($this->resFile, 2));
$arrHeader['size_of_cd'] = unpack('V', @fread($this->resFile, 4));
$arrHeader['offset_start_cd'] = unpack('V', @fread($this->resFile, 4));
$arrHeader['zipfile_comment_length'] = unpack('v', @fread($this->resFile, 2));
$arrHeader['zipfile_comment'] = $arrHeader['zipfile_comment_length'][1] ? @fread($this->resFile, $arrHeader['zipfile_comment_length'][1]) : '';
// Eliminate nested arrays
foreach ($arrHeader as $k=>$v)
{
$arrHeader[$k] = \is_array($v) ? $v[1] : $v;
}
$this->arrHeader = $arrHeader;
// Reposition pointer to begin of the central directory
fseek($this->resFile, $this->arrHeader['offset_start_cd']);
$strSignature = @fread($this->resFile, 4);
// Build file list
while ($strSignature == self::CENTRAL_DIR_START)
{
$arrFile = array();
$arrFile['version_made_by'] = unpack('v', @fread($this->resFile, 2));
$arrFile['version_needed_to_extract'] = unpack('v', @fread($this->resFile, 2));
$arrFile['general_purpose_bit_flag'] = unpack('v', @fread($this->resFile, 2));
$arrFile['compression_method'] = unpack('v', @fread($this->resFile, 2));
$arrFile['last_mod_file_time'] = unpack('v', @fread($this->resFile, 2));
$arrFile['last_mod_file_date'] = unpack('v', @fread($this->resFile, 2));
$arrFile['crc-32'] = unpack('V', @fread($this->resFile, 4));
$arrFile['compressed_size'] = unpack('V', @fread($this->resFile, 4));
$arrFile['uncompressed_size'] = unpack('V', @fread($this->resFile, 4));
$arrFile['file_name_length'] = unpack('v', @fread($this->resFile, 2));
$arrFile['extra_field_length'] = unpack('v', @fread($this->resFile, 2));
$arrFile['file_comment_length'] = unpack('v', @fread($this->resFile, 2));
$arrFile['disk_number_start'] = unpack('v', @fread($this->resFile, 2));
$arrFile['internal_file_attributes'] = unpack('v', @fread($this->resFile, 2));
$arrFile['external_file_attributes'] = unpack('V', @fread($this->resFile, 4));
$arrFile['offset_of_local_header'] = unpack('V', @fread($this->resFile, 4));
$arrFile['file_name'] = @fread($this->resFile, $arrFile['file_name_length'][1]);
$arrFile['extra_field'] = $arrFile['extra_field_length'][1] ? @fread($this->resFile, $arrFile['extra_field_length'][1]) : '';
$arrFile['file_comment'] = $arrFile['file_comment_length'][1] ? @fread($this->resFile, $arrFile['file_comment_length'][1]) : '';
// Skip directories
if (substr($arrFile['file_name'], -1) == '/')
{
$strSignature = @fread($this->resFile, 4);
continue;
}
// Eliminate nested arrays
foreach ($arrFile as $k=>$v)
{
$arrFile[$k] = \is_array($v) ? $v[1] : $v;
}
// Split file path
$arrFile['file_basename'] = basename($arrFile['file_name']);
$arrFile['file_dirname'] = (($path = \dirname($arrFile['file_name'])) != '.' ? $path : '');
// Add UNIX time
$arrFile['last_mod_file_unix'] = $this->decToUnix((int) $arrFile['last_mod_file_time'], (int) $arrFile['last_mod_file_date']);
// Read next signature
$strSignature = @fread($this->resFile, 4);
$this->arrFiles[] = $arrFile;
}
$this->intLast = (\count($this->arrFiles) - 1);
// Restore the mbstring encoding (see #5842)
$strMbCharset && mb_internal_encoding($strMbCharset);
}
/**
* Calculate the Unix timestamp from two hexadecimal values
*
* @param integer $intTime The time integer
* @param integer $intDate The date integer
*
* @return integer The Unix timestamp
*/
protected function decToUnix($intTime, $intDate)
{
return mktime
(
($intTime & 0xf800) >> 11,
($intTime & 0x07e0) >> 5,
($intTime & 0x001f) << 1,
($intDate & 0x01e0) >> 5,
($intDate & 0x001f),
(($intDate & 0xfe00) >> 9) + 1980
);
}
}
class_alias(ZipReader::class, 'ZipReader');