1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpZip\Model\Entry; |
4
|
|
|
|
5
|
|
|
use PhpZip\Exception\InvalidArgumentException; |
6
|
|
|
use PhpZip\Exception\ZipException; |
7
|
|
|
use PhpZip\Extra\ExtraFieldsCollection; |
8
|
|
|
use PhpZip\Extra\ExtraFieldsFactory; |
9
|
|
|
use PhpZip\Extra\Fields\WinZipAesEntryExtraField; |
10
|
|
|
use PhpZip\Model\ZipEntry; |
11
|
|
|
use PhpZip\Util\DateTimeConverter; |
12
|
|
|
use PhpZip\Util\StringUtil; |
13
|
|
|
use PhpZip\ZipFile; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Abstract ZIP entry. |
17
|
|
|
* |
18
|
|
|
* @see https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT .ZIP File Format Specification |
19
|
|
|
* |
20
|
|
|
* @author Ne-Lexa [email protected] |
21
|
|
|
* @license MIT |
22
|
|
|
*/ |
23
|
|
|
abstract class ZipAbstractEntry implements ZipEntry |
24
|
|
|
{ |
25
|
|
|
/** @var string Entry name (filename in archive) */ |
26
|
|
|
private $name; |
27
|
|
|
|
28
|
|
|
/** @var int Made by platform */ |
29
|
|
|
private $createdOS = self::UNKNOWN; |
30
|
|
|
|
31
|
|
|
/** @var int Extracted by platform */ |
32
|
|
|
private $extractedOS = self::UNKNOWN; |
33
|
|
|
|
34
|
|
|
/** @var int */ |
35
|
|
|
private $softwareVersion = self::UNKNOWN; |
36
|
|
|
|
37
|
|
|
/** @var int */ |
38
|
|
|
private $versionNeededToExtract = self::UNKNOWN; |
39
|
|
|
|
40
|
|
|
/** @var int Compression method */ |
41
|
|
|
private $method = self::UNKNOWN; |
42
|
|
|
|
43
|
|
|
/** @var int */ |
44
|
|
|
private $generalPurposeBitFlags = 0; |
45
|
|
|
|
46
|
|
|
/** @var int Dos time */ |
47
|
|
|
private $dosTime = self::UNKNOWN; |
48
|
|
|
|
49
|
|
|
/** @var int Crc32 */ |
50
|
|
|
private $crc = self::UNKNOWN; |
51
|
|
|
|
52
|
|
|
/** @var int Compressed size */ |
53
|
|
|
private $compressedSize = self::UNKNOWN; |
54
|
|
|
|
55
|
|
|
/** @var int Uncompressed size */ |
56
|
|
|
private $size = self::UNKNOWN; |
57
|
|
|
|
58
|
|
|
/** @var int Internal attributes */ |
59
|
|
|
private $internalAttributes = 0; |
60
|
|
|
|
61
|
|
|
/** @var int External attributes */ |
62
|
|
|
private $externalAttributes = 0; |
63
|
|
|
|
64
|
|
|
/** @var int relative Offset Of Local File Header */ |
65
|
|
|
private $offset = 0; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Collections of Extra Fields. |
69
|
|
|
* Keys from Header ID [int] and value Extra Field [ExtraField]. |
70
|
|
|
* Should be null or may be empty if no Extra Fields are used. |
71
|
|
|
* |
72
|
|
|
* @var ExtraFieldsCollection |
73
|
|
|
*/ |
74
|
|
|
private $extraFieldsCollection; |
75
|
|
|
|
76
|
|
|
/** @var string|null comment field */ |
77
|
|
|
private $comment; |
78
|
|
|
|
79
|
|
|
/** @var string entry password for read or write encryption data */ |
80
|
|
|
private $password; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Encryption method. |
84
|
|
|
* |
85
|
|
|
* @see ZipFile::ENCRYPTION_METHOD_TRADITIONAL |
86
|
|
|
* @see ZipFile::ENCRYPTION_METHOD_WINZIP_AES_128 |
87
|
|
|
* @see ZipFile::ENCRYPTION_METHOD_WINZIP_AES_192 |
88
|
|
|
* @see ZipFile::ENCRYPTION_METHOD_WINZIP_AES_256 |
89
|
|
|
* |
90
|
|
|
* @var int |
91
|
|
|
*/ |
92
|
|
|
private $encryptionMethod = ZipFile::ENCRYPTION_METHOD_TRADITIONAL; |
93
|
|
|
|
94
|
|
|
/** @var int */ |
95
|
|
|
private $compressionLevel = ZipFile::LEVEL_DEFAULT_COMPRESSION; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* ZipAbstractEntry constructor. |
99
|
|
|
*/ |
100
|
|
|
public function __construct() |
101
|
|
|
{ |
102
|
|
|
$this->extraFieldsCollection = new ExtraFieldsCollection(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param ZipEntry $entry |
107
|
|
|
* |
108
|
|
|
* @throws ZipException |
109
|
|
|
*/ |
110
|
|
|
public function setEntry(ZipEntry $entry) |
111
|
|
|
{ |
112
|
|
|
$this->setName($entry->getName()); |
113
|
|
|
$this->setSoftwareVersion($entry->getSoftwareVersion()); |
114
|
|
|
$this->setCreatedOS($entry->getCreatedOS()); |
115
|
|
|
$this->setExtractedOS($entry->getExtractedOS()); |
116
|
|
|
$this->setVersionNeededToExtract($entry->getVersionNeededToExtract()); |
117
|
|
|
$this->setMethod($entry->getMethod()); |
118
|
|
|
$this->setGeneralPurposeBitFlags($entry->getGeneralPurposeBitFlags()); |
119
|
|
|
$this->setDosTime($entry->getDosTime()); |
120
|
|
|
$this->setCrc($entry->getCrc()); |
121
|
|
|
$this->setCompressedSize($entry->getCompressedSize()); |
122
|
|
|
$this->setSize($entry->getSize()); |
123
|
|
|
$this->setInternalAttributes($entry->getInternalAttributes()); |
124
|
|
|
$this->setExternalAttributes($entry->getExternalAttributes()); |
125
|
|
|
$this->setOffset($entry->getOffset()); |
126
|
|
|
$this->setExtra($entry->getExtra()); |
127
|
|
|
$this->setComment($entry->getComment()); |
128
|
|
|
$this->setPassword($entry->getPassword()); |
129
|
|
|
$this->setEncryptionMethod($entry->getEncryptionMethod()); |
130
|
|
|
$this->setCompressionLevel($entry->getCompressionLevel()); |
131
|
|
|
$this->setEncrypted($entry->isEncrypted()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns the ZIP entry name. |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
public function getName() |
140
|
|
|
{ |
141
|
|
|
return $this->name; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Set entry name. |
146
|
|
|
* |
147
|
|
|
* @param string $name New entry name |
148
|
|
|
* |
149
|
|
|
* @throws ZipException |
150
|
|
|
* |
151
|
|
|
* @return ZipEntry |
152
|
|
|
*/ |
153
|
|
|
public function setName($name) |
154
|
|
|
{ |
155
|
|
|
$length = \strlen($name); |
156
|
|
|
|
157
|
|
|
if ($length < 0x0000 || $length > 0xffff) { |
158
|
|
|
throw new ZipException('Illegal zip entry name parameter'); |
159
|
|
|
} |
160
|
|
|
$this->setGeneralPurposeBitFlag(self::GPBF_UTF8, true); |
161
|
|
|
$this->name = $name; |
162
|
|
|
$this->externalAttributes = $this->isDirectory() ? 0x10 : 0; |
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Sets the indexed General Purpose Bit Flag. |
169
|
|
|
* |
170
|
|
|
* @param int $mask |
171
|
|
|
* @param bool $bit |
172
|
|
|
* |
173
|
|
|
* @return ZipEntry |
174
|
|
|
*/ |
175
|
|
|
public function setGeneralPurposeBitFlag($mask, $bit) |
176
|
|
|
{ |
177
|
|
|
if ($bit) { |
178
|
|
|
$this->generalPurposeBitFlags |= $mask; |
179
|
|
|
} else { |
180
|
|
|
$this->generalPurposeBitFlags &= ~$mask; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return int Get platform |
188
|
|
|
* |
189
|
|
|
* @deprecated Use {@see ZipEntry::getCreatedOS()} |
190
|
|
|
* @noinspection PhpUsageOfSilenceOperatorInspection |
191
|
|
|
*/ |
192
|
|
|
public function getPlatform() |
193
|
|
|
{ |
194
|
|
|
@trigger_error('ZipEntry::getPlatform() is deprecated. Use ZipEntry::getCreatedOS()', \E_USER_DEPRECATED); |
195
|
|
|
|
196
|
|
|
return $this->getCreatedOS(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param int $platform |
201
|
|
|
* |
202
|
|
|
* @return ZipEntry |
203
|
|
|
* |
204
|
|
|
* @throws ZipException |
205
|
|
|
* |
206
|
|
|
* @deprecated Use {@see ZipEntry::setCreatedOS()} |
207
|
|
|
* @noinspection PhpUsageOfSilenceOperatorInspection |
208
|
|
|
*/ |
209
|
|
|
public function setPlatform($platform) |
210
|
|
|
{ |
211
|
|
|
@trigger_error('ZipEntry::setPlatform() is deprecated. Use ZipEntry::setCreatedOS()', \E_USER_DEPRECATED); |
212
|
|
|
|
213
|
|
|
return $this->setCreatedOS($platform); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return int platform |
218
|
|
|
*/ |
219
|
|
|
public function getCreatedOS() |
220
|
|
|
{ |
221
|
|
|
return $this->createdOS; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Set platform. |
226
|
|
|
* |
227
|
|
|
* @param int $platform |
228
|
|
|
* |
229
|
|
|
* @throws ZipException |
230
|
|
|
* |
231
|
|
|
* @return ZipEntry |
232
|
|
|
*/ |
233
|
|
|
public function setCreatedOS($platform) |
234
|
|
|
{ |
235
|
|
|
$platform = (int) $platform; |
236
|
|
|
|
237
|
|
|
if ($platform < 0x00 || $platform > 0xff) { |
238
|
|
|
throw new ZipException('Platform out of range'); |
239
|
|
|
} |
240
|
|
|
$this->createdOS = $platform; |
241
|
|
|
|
242
|
|
|
return $this; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @return int |
247
|
|
|
*/ |
248
|
|
|
public function getExtractedOS() |
249
|
|
|
{ |
250
|
|
|
return $this->extractedOS; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Set extracted OS. |
255
|
|
|
* |
256
|
|
|
* @param int $platform |
257
|
|
|
* |
258
|
|
|
* @throws ZipException |
259
|
|
|
* |
260
|
|
|
* @return ZipEntry |
261
|
|
|
*/ |
262
|
|
|
public function setExtractedOS($platform) |
263
|
|
|
{ |
264
|
|
|
$platform = (int) $platform; |
265
|
|
|
|
266
|
|
|
if ($platform < 0x00 || $platform > 0xff) { |
267
|
|
|
throw new ZipException('Platform out of range'); |
268
|
|
|
} |
269
|
|
|
$this->extractedOS = $platform; |
270
|
|
|
|
271
|
|
|
return $this; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @return int |
276
|
|
|
*/ |
277
|
|
|
public function getSoftwareVersion() |
278
|
|
|
{ |
279
|
|
|
return $this->softwareVersion; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @param int $softwareVersion |
284
|
|
|
* |
285
|
|
|
* @return ZipEntry |
286
|
|
|
*/ |
287
|
|
|
public function setSoftwareVersion($softwareVersion) |
288
|
|
|
{ |
289
|
|
|
$this->softwareVersion = (int) $softwareVersion; |
290
|
|
|
|
291
|
|
|
return $this; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Version needed to extract. |
296
|
|
|
* |
297
|
|
|
* @return int |
298
|
|
|
*/ |
299
|
|
|
public function getVersionNeededToExtract() |
300
|
|
|
{ |
301
|
|
|
if ($this->versionNeededToExtract === self::UNKNOWN) { |
302
|
|
|
$method = $this->getMethod(); |
303
|
|
|
|
304
|
|
|
if ($method === self::METHOD_WINZIP_AES) { |
305
|
|
|
return 51; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
if ($method === ZipFile::METHOD_BZIP2) { |
309
|
|
|
return 46; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
if ($this->isZip64ExtensionsRequired()) { |
313
|
|
|
return 45; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
return $method === ZipFile::METHOD_DEFLATED || $this->isDirectory() ? 20 : 10; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
return $this->versionNeededToExtract; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Set version needed to extract. |
324
|
|
|
* |
325
|
|
|
* @param int $version |
326
|
|
|
* |
327
|
|
|
* @return ZipEntry |
328
|
|
|
*/ |
329
|
|
|
public function setVersionNeededToExtract($version) |
330
|
|
|
{ |
331
|
|
|
$this->versionNeededToExtract = $version; |
332
|
|
|
|
333
|
|
|
return $this; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* @return bool |
338
|
|
|
*/ |
339
|
|
|
public function isZip64ExtensionsRequired() |
340
|
|
|
{ |
341
|
|
|
return $this->getCompressedSize() >= 0xffffffff |
342
|
|
|
|| $this->getSize() >= 0xffffffff; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Returns the compressed size of this entry. |
347
|
|
|
* |
348
|
|
|
* @see int |
349
|
|
|
*/ |
350
|
|
|
public function getCompressedSize() |
351
|
|
|
{ |
352
|
|
|
return $this->compressedSize; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Sets the compressed size of this entry. |
357
|
|
|
* |
358
|
|
|
* @param int $compressedSize the Compressed Size |
359
|
|
|
* |
360
|
|
|
* @return ZipEntry |
361
|
|
|
*/ |
362
|
|
|
public function setCompressedSize($compressedSize) |
363
|
|
|
{ |
364
|
|
|
$this->compressedSize = $compressedSize; |
365
|
|
|
|
366
|
|
|
return $this; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* Returns the uncompressed size of this entry. |
371
|
|
|
* |
372
|
|
|
* @see ZipEntry::setCompressedSize |
373
|
|
|
*/ |
374
|
|
|
public function getSize() |
375
|
|
|
{ |
376
|
|
|
return $this->size; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* Sets the uncompressed size of this entry. |
381
|
|
|
* |
382
|
|
|
* @param int $size the (Uncompressed) Size |
383
|
|
|
* |
384
|
|
|
* @return ZipEntry |
385
|
|
|
*/ |
386
|
|
|
public function setSize($size) |
387
|
|
|
{ |
388
|
|
|
$this->size = $size; |
389
|
|
|
|
390
|
|
|
return $this; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Return relative Offset Of Local File Header. |
395
|
|
|
* |
396
|
|
|
* @return int |
397
|
|
|
*/ |
398
|
|
|
public function getOffset() |
399
|
|
|
{ |
400
|
|
|
return $this->offset; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* @param int $offset |
405
|
|
|
* |
406
|
|
|
* @return ZipEntry |
407
|
|
|
*/ |
408
|
|
|
public function setOffset($offset) |
409
|
|
|
{ |
410
|
|
|
$this->offset = (int) $offset; |
411
|
|
|
|
412
|
|
|
return $this; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* Returns the General Purpose Bit Flags. |
417
|
|
|
* |
418
|
|
|
* @return int |
419
|
|
|
*/ |
420
|
|
|
public function getGeneralPurposeBitFlags() |
421
|
|
|
{ |
422
|
|
|
return $this->generalPurposeBitFlags & 0xffff; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
/** |
426
|
|
|
* Sets the General Purpose Bit Flags. |
427
|
|
|
* |
428
|
|
|
* @param mixed $general |
429
|
|
|
* |
430
|
|
|
* @throws ZipException |
431
|
|
|
* |
432
|
|
|
* @return ZipEntry |
433
|
|
|
* |
434
|
|
|
* @var int general |
435
|
|
|
*/ |
436
|
|
|
public function setGeneralPurposeBitFlags($general) |
437
|
|
|
{ |
438
|
|
|
if ($general < 0x0000 || $general > 0xffff) { |
439
|
|
|
throw new ZipException('general out of range'); |
440
|
|
|
} |
441
|
|
|
$this->generalPurposeBitFlags = $general; |
442
|
|
|
|
443
|
|
|
if ($this->method === ZipFile::METHOD_DEFLATED) { |
444
|
|
|
$bit1 = $this->getGeneralPurposeBitFlag(self::GPBF_COMPRESSION_FLAG1); |
445
|
|
|
$bit2 = $this->getGeneralPurposeBitFlag(self::GPBF_COMPRESSION_FLAG2); |
446
|
|
|
|
447
|
|
|
if ($bit1 && !$bit2) { |
448
|
|
|
$this->compressionLevel = ZipFile::LEVEL_BEST_COMPRESSION; |
449
|
|
|
} elseif (!$bit1 && $bit2) { |
450
|
|
|
$this->compressionLevel = ZipFile::LEVEL_FAST; |
451
|
|
|
} elseif ($bit1 && $bit2) { |
452
|
|
|
$this->compressionLevel = ZipFile::LEVEL_SUPER_FAST; |
453
|
|
|
} else { |
454
|
|
|
$this->compressionLevel = ZipFile::LEVEL_DEFAULT_COMPRESSION; |
455
|
|
|
} |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
return $this; |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
/** |
462
|
|
|
* Returns true if and only if this ZIP entry is encrypted. |
463
|
|
|
* |
464
|
|
|
* @return bool |
465
|
|
|
*/ |
466
|
|
|
public function isEncrypted() |
467
|
|
|
{ |
468
|
|
|
return $this->getGeneralPurposeBitFlag(self::GPBF_ENCRYPTED); |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
/** |
472
|
|
|
* Returns the indexed General Purpose Bit Flag. |
473
|
|
|
* |
474
|
|
|
* @param int $mask |
475
|
|
|
* |
476
|
|
|
* @return bool |
477
|
|
|
*/ |
478
|
|
|
public function getGeneralPurposeBitFlag($mask) |
479
|
|
|
{ |
480
|
|
|
return ($this->generalPurposeBitFlags & $mask) !== 0; |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* Sets the encryption property to false and removes any other |
485
|
|
|
* encryption artifacts. |
486
|
|
|
* |
487
|
|
|
* @throws ZipException |
488
|
|
|
* |
489
|
|
|
* @return ZipEntry |
490
|
|
|
*/ |
491
|
|
|
public function disableEncryption() |
492
|
|
|
{ |
493
|
|
|
$this->setEncrypted(false); |
494
|
|
|
$headerId = WinZipAesEntryExtraField::getHeaderId(); |
495
|
|
|
|
496
|
|
|
if (isset($this->extraFieldsCollection[$headerId])) { |
497
|
|
|
/** @var WinZipAesEntryExtraField $field */ |
498
|
|
|
$field = $this->extraFieldsCollection[$headerId]; |
499
|
|
|
|
500
|
|
|
if ($this->getMethod() === self::METHOD_WINZIP_AES) { |
501
|
|
|
$this->setMethod($field === null ? self::UNKNOWN : $field->getMethod()); |
502
|
|
|
} |
503
|
|
|
unset($this->extraFieldsCollection[$headerId]); |
504
|
|
|
} |
505
|
|
|
$this->password = null; |
506
|
|
|
|
507
|
|
|
return $this; |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
/** |
511
|
|
|
* Sets the encryption flag for this ZIP entry. |
512
|
|
|
* |
513
|
|
|
* @param bool $encrypted |
514
|
|
|
* |
515
|
|
|
* @return ZipEntry |
516
|
|
|
*/ |
517
|
|
|
public function setEncrypted($encrypted) |
518
|
|
|
{ |
519
|
|
|
$encrypted = (bool) $encrypted; |
520
|
|
|
$this->setGeneralPurposeBitFlag(self::GPBF_ENCRYPTED, $encrypted); |
521
|
|
|
|
522
|
|
|
return $this; |
523
|
|
|
} |
524
|
|
|
|
525
|
|
|
/** |
526
|
|
|
* Returns the compression method for this entry. |
527
|
|
|
* |
528
|
|
|
* @return int |
529
|
|
|
*/ |
530
|
|
|
public function getMethod() |
531
|
|
|
{ |
532
|
|
|
return $this->method; |
533
|
|
|
} |
534
|
|
|
|
535
|
|
|
/** |
536
|
|
|
* Sets the compression method for this entry. |
537
|
|
|
* |
538
|
|
|
* @param int $method |
539
|
|
|
* |
540
|
|
|
* @throws ZipException if method is not STORED, DEFLATED, BZIP2 or UNKNOWN |
541
|
|
|
* |
542
|
|
|
* @return ZipEntry |
543
|
|
|
*/ |
544
|
|
|
public function setMethod($method) |
545
|
|
|
{ |
546
|
|
|
if ($method === self::UNKNOWN) { |
547
|
|
|
$this->method = $method; |
548
|
|
|
|
549
|
|
|
return $this; |
550
|
|
|
} |
551
|
|
|
|
552
|
|
|
if ($method < 0x0000 || $method > 0xffff) { |
553
|
|
|
throw new ZipException('method out of range: ' . $method); |
554
|
|
|
} |
555
|
|
|
switch ($method) { |
556
|
|
|
case self::METHOD_WINZIP_AES: |
557
|
|
|
case ZipFile::METHOD_STORED: |
558
|
|
|
case ZipFile::METHOD_DEFLATED: |
559
|
|
|
case ZipFile::METHOD_BZIP2: |
560
|
|
|
$this->method = $method; |
561
|
|
|
break; |
562
|
|
|
|
563
|
|
|
default: |
564
|
|
|
throw new ZipException($this->name . " (unsupported compression method {$method})"); |
565
|
|
|
} |
566
|
|
|
|
567
|
|
|
return $this; |
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
/** |
571
|
|
|
* Get Unix Timestamp. |
572
|
|
|
* |
573
|
|
|
* @return int |
574
|
|
|
*/ |
575
|
|
|
public function getTime() |
576
|
|
|
{ |
577
|
|
|
if ($this->getDosTime() === self::UNKNOWN) { |
578
|
|
|
return self::UNKNOWN; |
579
|
|
|
} |
580
|
|
|
|
581
|
|
|
return DateTimeConverter::toUnixTimestamp($this->getDosTime()); |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
/** |
585
|
|
|
* Get Dos Time. |
586
|
|
|
* |
587
|
|
|
* @return int |
588
|
|
|
*/ |
589
|
|
|
public function getDosTime() |
590
|
|
|
{ |
591
|
|
|
return $this->dosTime; |
592
|
|
|
} |
593
|
|
|
|
594
|
|
|
/** |
595
|
|
|
* Set Dos Time. |
596
|
|
|
* |
597
|
|
|
* @param int $dosTime |
598
|
|
|
* |
599
|
|
|
* @throws ZipException |
600
|
|
|
* |
601
|
|
|
* @return ZipEntry |
602
|
|
|
*/ |
603
|
|
|
public function setDosTime($dosTime) |
604
|
|
|
{ |
605
|
|
|
$dosTime = (int) $dosTime; |
606
|
|
|
|
607
|
|
|
if ($dosTime < 0x00000000 || $dosTime > 0xffffffff) { |
608
|
|
|
throw new ZipException('DosTime out of range'); |
609
|
|
|
} |
610
|
|
|
$this->dosTime = $dosTime; |
611
|
|
|
|
612
|
|
|
return $this; |
613
|
|
|
} |
614
|
|
|
|
615
|
|
|
/** |
616
|
|
|
* Set time from unix timestamp. |
617
|
|
|
* |
618
|
|
|
* @param int $unixTimestamp |
619
|
|
|
* |
620
|
|
|
* @throws ZipException |
621
|
|
|
* |
622
|
|
|
* @return ZipEntry |
623
|
|
|
*/ |
624
|
|
|
public function setTime($unixTimestamp) |
625
|
|
|
{ |
626
|
|
|
$known = $unixTimestamp !== self::UNKNOWN; |
627
|
|
|
|
628
|
|
|
if ($known) { |
629
|
|
|
$this->dosTime = DateTimeConverter::toDosTime($unixTimestamp); |
630
|
|
|
} else { |
631
|
|
|
$this->dosTime = 0; |
632
|
|
|
} |
633
|
|
|
|
634
|
|
|
return $this; |
635
|
|
|
} |
636
|
|
|
|
637
|
|
|
/** |
638
|
|
|
* Returns the external file attributes. |
639
|
|
|
* |
640
|
|
|
* @return int the external file attributes |
641
|
|
|
*/ |
642
|
|
|
public function getExternalAttributes() |
643
|
|
|
{ |
644
|
|
|
return $this->externalAttributes; |
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
/** |
648
|
|
|
* Sets the external file attributes. |
649
|
|
|
* |
650
|
|
|
* @param int $externalAttributes the external file attributes |
651
|
|
|
* |
652
|
|
|
* @return ZipEntry |
653
|
|
|
*/ |
654
|
|
|
public function setExternalAttributes($externalAttributes) |
655
|
|
|
{ |
656
|
|
|
$this->externalAttributes = $externalAttributes; |
657
|
|
|
|
658
|
|
|
return $this; |
659
|
|
|
} |
660
|
|
|
|
661
|
|
|
/** |
662
|
|
|
* Sets the internal file attributes. |
663
|
|
|
* |
664
|
|
|
* @param int $attributes the internal file attributes |
665
|
|
|
* |
666
|
|
|
* @return ZipEntry |
667
|
|
|
*/ |
668
|
|
|
public function setInternalAttributes($attributes) |
669
|
|
|
{ |
670
|
|
|
$this->internalAttributes = (int) $attributes; |
671
|
|
|
|
672
|
|
|
return $this; |
673
|
|
|
} |
674
|
|
|
|
675
|
|
|
/** |
676
|
|
|
* Returns the internal file attributes. |
677
|
|
|
* |
678
|
|
|
* @return int the internal file attributes |
679
|
|
|
*/ |
680
|
|
|
public function getInternalAttributes() |
681
|
|
|
{ |
682
|
|
|
return $this->internalAttributes; |
683
|
|
|
} |
684
|
|
|
|
685
|
|
|
/** |
686
|
|
|
* Returns true if and only if this ZIP entry represents a directory entry |
687
|
|
|
* (i.e. end with '/'). |
688
|
|
|
* |
689
|
|
|
* @return bool |
690
|
|
|
*/ |
691
|
|
|
public function isDirectory() |
692
|
|
|
{ |
693
|
|
|
return StringUtil::endsWith($this->name, '/'); |
694
|
|
|
} |
695
|
|
|
|
696
|
|
|
/** |
697
|
|
|
* @return ExtraFieldsCollection |
698
|
|
|
*/ |
699
|
|
|
public function &getExtraFieldsCollection() |
700
|
|
|
{ |
701
|
|
|
return $this->extraFieldsCollection; |
702
|
|
|
} |
703
|
|
|
|
704
|
|
|
/** |
705
|
|
|
* Returns a protective copy of the serialized Extra Fields. |
706
|
|
|
* |
707
|
|
|
* @throws ZipException |
708
|
|
|
* |
709
|
|
|
* @return string |
710
|
|
|
*/ |
711
|
|
|
public function getExtra() |
712
|
|
|
{ |
713
|
|
|
return ExtraFieldsFactory::createSerializedData($this->extraFieldsCollection); |
714
|
|
|
} |
715
|
|
|
|
716
|
|
|
/** |
717
|
|
|
* Sets the serialized Extra Fields by making a protective copy. |
718
|
|
|
* Note that this method parses the serialized Extra Fields according to |
719
|
|
|
* the ZIP File Format Specification and limits its size to 64 KB. |
720
|
|
|
* Therefore, this property cannot not be used to hold arbitrary |
721
|
|
|
* (application) data. |
722
|
|
|
* Consider storing such data in a separate entry instead. |
723
|
|
|
* |
724
|
|
|
* @param string $data the byte array holding the serialized Extra Fields |
725
|
|
|
* |
726
|
|
|
* @throws ZipException if the serialized Extra Fields exceed 64 KB |
727
|
|
|
* |
728
|
|
|
* @return ZipEntry |
729
|
|
|
*/ |
730
|
|
|
public function setExtra($data) |
731
|
|
|
{ |
732
|
|
|
$this->extraFieldsCollection = ExtraFieldsFactory::createExtraFieldCollections($data, $this); |
733
|
|
|
|
734
|
|
|
return $this; |
735
|
|
|
} |
736
|
|
|
|
737
|
|
|
/** |
738
|
|
|
* Returns comment entry. |
739
|
|
|
* |
740
|
|
|
* @return string |
741
|
|
|
*/ |
742
|
|
|
public function getComment() |
743
|
|
|
{ |
744
|
|
|
return $this->comment !== null ? $this->comment : ''; |
745
|
|
|
} |
746
|
|
|
|
747
|
|
|
/** |
748
|
|
|
* Set entry comment. |
749
|
|
|
* |
750
|
|
|
* @param string|null $comment |
751
|
|
|
* |
752
|
|
|
* @throws ZipException |
753
|
|
|
* |
754
|
|
|
* @return ZipEntry |
755
|
|
|
*/ |
756
|
|
|
public function setComment($comment) |
757
|
|
|
{ |
758
|
|
|
if ($comment !== null) { |
759
|
|
|
$commentLength = \strlen($comment); |
760
|
|
|
|
761
|
|
|
if ($commentLength < 0x0000 || $commentLength > 0xffff) { |
762
|
|
|
throw new ZipException('Comment too long'); |
763
|
|
|
} |
764
|
|
|
$this->setGeneralPurposeBitFlag(self::GPBF_UTF8, true); |
765
|
|
|
} |
766
|
|
|
$this->comment = $comment; |
767
|
|
|
|
768
|
|
|
return $this; |
769
|
|
|
} |
770
|
|
|
|
771
|
|
|
/** |
772
|
|
|
* @return bool |
773
|
|
|
*/ |
774
|
|
|
public function isDataDescriptorRequired() |
775
|
|
|
{ |
776
|
|
|
return ($this->getCrc() | $this->getCompressedSize() | $this->getSize()) === self::UNKNOWN; |
777
|
|
|
} |
778
|
|
|
|
779
|
|
|
/** |
780
|
|
|
* Return crc32 content or 0 for WinZip AES v2. |
781
|
|
|
* |
782
|
|
|
* @return int |
783
|
|
|
*/ |
784
|
|
|
public function getCrc() |
785
|
|
|
{ |
786
|
|
|
return $this->crc; |
787
|
|
|
} |
788
|
|
|
|
789
|
|
|
/** |
790
|
|
|
* Set crc32 content. |
791
|
|
|
* |
792
|
|
|
* @param int $crc |
793
|
|
|
* |
794
|
|
|
* @return ZipEntry |
795
|
|
|
*/ |
796
|
|
|
public function setCrc($crc) |
797
|
|
|
{ |
798
|
|
|
$this->crc = (int) $crc; |
799
|
|
|
|
800
|
|
|
return $this; |
801
|
|
|
} |
802
|
|
|
|
803
|
|
|
/** |
804
|
|
|
* @return string |
805
|
|
|
*/ |
806
|
|
|
public function getPassword() |
807
|
|
|
{ |
808
|
|
|
return $this->password; |
809
|
|
|
} |
810
|
|
|
|
811
|
|
|
/** |
812
|
|
|
* Set password and encryption method from entry. |
813
|
|
|
* |
814
|
|
|
* @param string $password |
815
|
|
|
* @param int|null $encryptionMethod |
816
|
|
|
* |
817
|
|
|
* @throws ZipException |
818
|
|
|
* |
819
|
|
|
* @return ZipEntry |
820
|
|
|
*/ |
821
|
|
|
public function setPassword($password, $encryptionMethod = null) |
822
|
|
|
{ |
823
|
|
|
$this->password = $password; |
824
|
|
|
|
825
|
|
|
if ($encryptionMethod !== null) { |
826
|
|
|
$this->setEncryptionMethod($encryptionMethod); |
827
|
|
|
} |
828
|
|
|
|
829
|
|
|
if (!empty($this->password)) { |
830
|
|
|
$this->setEncrypted(true); |
831
|
|
|
} else { |
832
|
|
|
$this->disableEncryption(); |
833
|
|
|
} |
834
|
|
|
|
835
|
|
|
return $this; |
836
|
|
|
} |
837
|
|
|
|
838
|
|
|
/** |
839
|
|
|
* @return int |
840
|
|
|
*/ |
841
|
|
|
public function getEncryptionMethod() |
842
|
|
|
{ |
843
|
|
|
return $this->encryptionMethod; |
844
|
|
|
} |
845
|
|
|
|
846
|
|
|
/** |
847
|
|
|
* Set encryption method. |
848
|
|
|
* |
849
|
|
|
* @param int $encryptionMethod |
850
|
|
|
* |
851
|
|
|
* @throws ZipException |
852
|
|
|
* |
853
|
|
|
* @return ZipEntry |
854
|
|
|
* |
855
|
|
|
* @see ZipFile::ENCRYPTION_METHOD_WINZIP_AES_256 |
856
|
|
|
* @see ZipFile::ENCRYPTION_METHOD_TRADITIONAL |
857
|
|
|
* @see ZipFile::ENCRYPTION_METHOD_WINZIP_AES_128 |
858
|
|
|
* @see ZipFile::ENCRYPTION_METHOD_WINZIP_AES_192 |
859
|
|
|
*/ |
860
|
|
|
public function setEncryptionMethod($encryptionMethod) |
861
|
|
|
{ |
862
|
|
|
if ($encryptionMethod !== null) { |
|
|
|
|
863
|
|
|
if ( |
864
|
|
|
$encryptionMethod !== ZipFile::ENCRYPTION_METHOD_TRADITIONAL |
865
|
|
|
&& $encryptionMethod !== ZipFile::ENCRYPTION_METHOD_WINZIP_AES_128 |
866
|
|
|
&& $encryptionMethod !== ZipFile::ENCRYPTION_METHOD_WINZIP_AES_192 |
867
|
|
|
&& $encryptionMethod !== ZipFile::ENCRYPTION_METHOD_WINZIP_AES_256 |
868
|
|
|
) { |
869
|
|
|
throw new ZipException('Invalid encryption method'); |
870
|
|
|
} |
871
|
|
|
$this->encryptionMethod = $encryptionMethod; |
872
|
|
|
} |
873
|
|
|
|
874
|
|
|
return $this; |
875
|
|
|
} |
876
|
|
|
|
877
|
|
|
/** |
878
|
|
|
* @return int |
879
|
|
|
*/ |
880
|
|
|
public function getCompressionLevel() |
881
|
|
|
{ |
882
|
|
|
return $this->compressionLevel; |
883
|
|
|
} |
884
|
|
|
|
885
|
|
|
/** |
886
|
|
|
* @param int $compressionLevel |
887
|
|
|
* |
888
|
|
|
* @return ZipEntry |
889
|
|
|
*/ |
890
|
|
|
public function setCompressionLevel($compressionLevel = ZipFile::LEVEL_DEFAULT_COMPRESSION) |
891
|
|
|
{ |
892
|
|
|
if ($compressionLevel < ZipFile::LEVEL_DEFAULT_COMPRESSION || |
893
|
|
|
$compressionLevel > ZipFile::LEVEL_BEST_COMPRESSION |
894
|
|
|
) { |
895
|
|
|
throw new InvalidArgumentException( |
896
|
|
|
'Invalid compression level. Minimum level ' . |
897
|
|
|
ZipFile::LEVEL_DEFAULT_COMPRESSION . '. Maximum level ' . ZipFile::LEVEL_BEST_COMPRESSION |
898
|
|
|
); |
899
|
|
|
} |
900
|
|
|
$this->compressionLevel = $compressionLevel; |
901
|
|
|
|
902
|
|
|
return $this; |
903
|
|
|
} |
904
|
|
|
|
905
|
|
|
/** |
906
|
|
|
* Clone extra fields. |
907
|
|
|
*/ |
908
|
|
|
public function __clone() |
909
|
|
|
{ |
910
|
|
|
$this->extraFieldsCollection = clone $this->extraFieldsCollection; |
911
|
|
|
} |
912
|
|
|
} |
913
|
|
|
|