Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ZipOutputStream.php « Stream « PhpZip « src « zip « nelexa « vendor - github.com/CarnetApp/CarnetNextcloud.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c1c875d7835bd4a38c8b59801029eafbd807f118 (plain)
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
<?php

namespace PhpZip\Stream;

use PhpZip\Crypto\TraditionalPkwareEncryptionEngine;
use PhpZip\Crypto\WinZipAesEngine;
use PhpZip\Exception\InvalidArgumentException;
use PhpZip\Exception\RuntimeException;
use PhpZip\Exception\ZipException;
use PhpZip\Extra\ExtraFieldsFactory;
use PhpZip\Extra\Fields\ApkAlignmentExtraField;
use PhpZip\Extra\Fields\WinZipAesEntryExtraField;
use PhpZip\Extra\Fields\Zip64ExtraField;
use PhpZip\Model\EndOfCentralDirectory;
use PhpZip\Model\Entry\OutputOffsetEntry;
use PhpZip\Model\Entry\ZipChangesEntry;
use PhpZip\Model\Entry\ZipSourceEntry;
use PhpZip\Model\ZipEntry;
use PhpZip\Model\ZipModel;
use PhpZip\Util\PackUtil;
use PhpZip\Util\StringUtil;
use PhpZip\ZipFileInterface;

/**
 * Write
 * ip file
 *
 * @author Ne-Lexa alexey@nelexa.ru
 * @license MIT
 */
class ZipOutputStream implements ZipOutputStreamInterface
{
    /**
     * @var resource
     */
    protected $out;
    /**
     * @var ZipModel
     */
    protected $zipModel;

    /**
     * ZipOutputStream constructor.
     * @param resource $out
     * @param ZipModel $zipModel
     * @throws InvalidArgumentException
     */
    public function __construct($out, ZipModel $zipModel)
    {
        if (!is_resource($out)) {
            throw new InvalidArgumentException('$out must be resource');
        }
        $this->out = $out;
        $this->zipModel = $zipModel;
    }

    public function writeZip()
    {
        $entries = $this->zipModel->getEntries();
        $outPosEntries = [];
        foreach ($entries as $entry) {
            $outPosEntries[] = new OutputOffsetEntry(ftell($this->out), $entry);
            $this->writeEntry($entry);
        }
        $centralDirectoryOffset = ftell($this->out);
        foreach ($outPosEntries as $outputEntry) {
            $this->writeCentralDirectoryHeader($outputEntry);
        }
        $this->writeEndOfCentralDirectoryRecord($centralDirectoryOffset);
    }

    /**
     * @param ZipEntry $entry
     * @throws ZipException
     */
    public function writeEntry(ZipEntry $entry)
    {
        if ($entry instanceof ZipSourceEntry) {
            $entry->getInputStream()->copyEntry($entry, $this);
            return;
        }

        $entryContent = $this->entryCommitChangesAndReturnContent($entry);

        $offset = ftell($this->out);
        $compressedSize = $entry->getCompressedSize();

        $extra = $entry->getExtra();

        $nameLength = strlen($entry->getName());
        $extraLength = strlen($extra);

        // zip align
        if (
            $this->zipModel->isZipAlign() &&
            !$entry->isEncrypted() &&
            $entry->getMethod() === ZipFileInterface::METHOD_STORED
        ) {
            $dataAlignmentMultiple = $this->zipModel->getZipAlign();
            if (StringUtil::endsWith($entry->getName(), '.so')) {
                $dataAlignmentMultiple = ApkAlignmentExtraField::ANDROID_COMMON_PAGE_ALIGNMENT_BYTES;
            }
            $dataMinStartOffset =
                $offset +
                ZipEntry::LOCAL_FILE_HEADER_MIN_LEN +
                $extraLength +
                $nameLength +
                ApkAlignmentExtraField::ALIGNMENT_ZIP_EXTRA_MIN_SIZE_BYTES;

            $padding =
                ($dataAlignmentMultiple - ($dataMinStartOffset % $dataAlignmentMultiple))
                % $dataAlignmentMultiple;

            $alignExtra = new ApkAlignmentExtraField();
            $alignExtra->setMultiple($dataAlignmentMultiple);
            $alignExtra->setPadding($padding);

            $extraFieldsCollection = clone $entry->getExtraFieldsCollection();
            $extraFieldsCollection->add($alignExtra);

            $extra = ExtraFieldsFactory::createSerializedData($extraFieldsCollection);
            $extraLength = strlen($extra);
        }

        $size = $nameLength + $extraLength;
        if (0xffff < $size) {
            throw new ZipException(
                $entry->getName() . " (the total size of " . $size .
                " bytes for the name, extra fields and comment " .
                "exceeds the maximum size of " . 0xffff . " bytes)"
            );
        }

        $dd = $entry->isDataDescriptorRequired();
        fwrite(
            $this->out,
            pack(
                'VvvvVVVVvv',
                // local file header signature     4 bytes  (0x04034b50)
                ZipEntry::LOCAL_FILE_HEADER_SIG,
                // version needed to extract       2 bytes
                $entry->getVersionNeededToExtract(),
                // general purpose bit flag        2 bytes
                $entry->getGeneralPurposeBitFlags(),
                // compression method              2 bytes
                $entry->getMethod(),
                // last mod file time              2 bytes
                // last mod file date              2 bytes
                $entry->getDosTime(),
                // crc-32                          4 bytes
                $dd ? 0 : $entry->getCrc(),
                // compressed size                 4 bytes
                $dd ? 0 : $entry->getCompressedSize(),
                // uncompressed size               4 bytes
                $dd ? 0 : $entry->getSize(),
                // file name length                2 bytes
                $nameLength,
                // extra field length              2 bytes
                $extraLength
            )
        );
        if ($nameLength > 0) {
            fwrite($this->out, $entry->getName());
        }
        if ($extraLength > 0) {
            fwrite($this->out, $extra);
        }

        if ($entry instanceof ZipChangesEntry && !$entry->isChangedContent()) {
            $entry->getSourceEntry()->getInputStream()->copyEntryData($entry->getSourceEntry(), $this);
        } elseif (null !== $entryContent) {
            fwrite($this->out, $entryContent);
        }

        assert(ZipEntry::UNKNOWN !== $entry->getCrc());
        assert(ZipEntry::UNKNOWN !== $entry->getSize());
        if ($entry->getGeneralPurposeBitFlag(ZipEntry::GPBF_DATA_DESCRIPTOR)) {
            // data descriptor signature       4 bytes  (0x08074b50)
            // crc-32                          4 bytes
            fwrite($this->out, pack('VV', ZipEntry::DATA_DESCRIPTOR_SIG, $entry->getCrc()));
            // compressed size                 4 or 8 bytes
            // uncompressed size               4 or 8 bytes
            if ($entry->isZip64ExtensionsRequired()) {
                fwrite($this->out, PackUtil::packLongLE($compressedSize));
                fwrite($this->out, PackUtil::packLongLE($entry->getSize()));
            } else {
                fwrite($this->out, pack('VV', $entry->getCompressedSize(), $entry->getSize()));
            }
        } elseif ($entry->getCompressedSize() != $compressedSize) {
            throw new ZipException(
                $entry->getName() . " (expected compressed entry size of "
                . $entry->getCompressedSize() . " bytes, " .
                "but is actually " . $compressedSize . " bytes)"
            );
        }
    }

    /**
     * @param ZipEntry $entry
     * @return null|string
     * @throws ZipException
     */
    protected function entryCommitChangesAndReturnContent(ZipEntry $entry)
    {
        if (ZipEntry::UNKNOWN === $entry->getPlatform()) {
            $entry->setPlatform(ZipEntry::PLATFORM_UNIX);
        }
        if (ZipEntry::UNKNOWN === $entry->getTime()) {
            $entry->setTime(time());
        }
        $method = $entry->getMethod();

        $encrypted = $entry->isEncrypted();
        // See appendix D of PKWARE's ZIP File Format Specification.
        $utf8 = true;

        if ($encrypted && null === $entry->getPassword()) {
            throw new ZipException("Can not password from entry " . $entry->getName());
        }

        // Compose General Purpose Bit Flag.
        $general = ($encrypted ? ZipEntry::GPBF_ENCRYPTED : 0)
            | ($entry->isDataDescriptorRequired() ? ZipEntry::GPBF_DATA_DESCRIPTOR : 0)
            | ($utf8 ? ZipEntry::GPBF_UTF8 : 0);

        $entryContent = null;
        $extraFieldsCollection = $entry->getExtraFieldsCollection();
        if (!($entry instanceof ZipChangesEntry && !$entry->isChangedContent())) {
            $entryContent = $entry->getEntryContent();

            if ($entryContent !== null) {
                $entry->setSize(strlen($entryContent));
                $entry->setCrc(crc32($entryContent));

                if ($encrypted && ZipEntry::METHOD_WINZIP_AES === $method) {
                    /**
                     * @var WinZipAesEntryExtraField $field
                     */
                    $field = $extraFieldsCollection->get(WinZipAesEntryExtraField::getHeaderId());
                    if (null !== $field) {
                        $method = $field->getMethod();
                    }
                }

                switch ($method) {
                    case ZipFileInterface::METHOD_STORED:
                        break;

                    case ZipFileInterface::METHOD_DEFLATED:
                        $entryContent = gzdeflate($entryContent, $entry->getCompressionLevel());
                        break;

                    case ZipFileInterface::METHOD_BZIP2:
                        $compressionLevel = $entry->getCompressionLevel() === ZipFileInterface::LEVEL_DEFAULT_COMPRESSION ?
                            ZipEntry::LEVEL_DEFAULT_BZIP2_COMPRESSION :
                            $entry->getCompressionLevel();
                        $entryContent = bzcompress($entryContent, $compressionLevel);
                        if (is_int($entryContent)) {
                            throw new ZipException('Error bzip2 compress. Error code: ' . $entryContent);
                        }
                        break;

                    case ZipEntry::UNKNOWN:
                        $entryContent = $this->determineBestCompressionMethod($entry, $entryContent);
                        $method = $entry->getMethod();
                        break;

                    default:
                        throw new ZipException($entry->getName() . " (unsupported compression method " . $method . ")");
                }

                if (ZipFileInterface::METHOD_DEFLATED === $method) {
                    $bit1 = false;
                    $bit2 = false;
                    switch ($entry->getCompressionLevel()) {
                        case ZipFileInterface::LEVEL_BEST_COMPRESSION:
                            $bit1 = true;
                            break;

                        case ZipFileInterface::LEVEL_FAST:
                            $bit2 = true;
                            break;

                        case ZipFileInterface::LEVEL_SUPER_FAST:
                            $bit1 = true;
                            $bit2 = true;
                            break;
                    }

                    $general |= ($bit1 ? ZipEntry::GPBF_COMPRESSION_FLAG1 : 0);
                    $general |= ($bit2 ? ZipEntry::GPBF_COMPRESSION_FLAG2 : 0);
                }

                if ($encrypted) {
                    if (in_array($entry->getEncryptionMethod(), [
                        ZipFileInterface::ENCRYPTION_METHOD_WINZIP_AES_128,
                        ZipFileInterface::ENCRYPTION_METHOD_WINZIP_AES_192,
                        ZipFileInterface::ENCRYPTION_METHOD_WINZIP_AES_256,
                    ], true)) {
                        $keyStrength = WinZipAesEntryExtraField::getKeyStrangeFromEncryptionMethod($entry->getEncryptionMethod()); // size bits
                        $field = ExtraFieldsFactory::createWinZipAesEntryExtra();
                        $field->setKeyStrength($keyStrength);
                        $field->setMethod($method);
                        $size = $entry->getSize();
                        if (20 <= $size && ZipFileInterface::METHOD_BZIP2 !== $method) {
                            $field->setVendorVersion(WinZipAesEntryExtraField::VV_AE_1);
                        } else {
                            $field->setVendorVersion(WinZipAesEntryExtraField::VV_AE_2);
                            $entry->setCrc(0);
                        }
                        $extraFieldsCollection->add($field);
                        $entry->setMethod(ZipEntry::METHOD_WINZIP_AES);

                        $winZipAesEngine = new WinZipAesEngine($entry);
                        $entryContent = $winZipAesEngine->encrypt($entryContent);
                    } elseif ($entry->getEncryptionMethod() === ZipFileInterface::ENCRYPTION_METHOD_TRADITIONAL) {
                        $zipCryptoEngine = new TraditionalPkwareEncryptionEngine($entry);
                        $entryContent = $zipCryptoEngine->encrypt($entryContent);
                    }
                }

                $compressedSize = strlen($entryContent);
                $entry->setCompressedSize($compressedSize);
            }
        }

        // Commit changes.
        $entry->setGeneralPurposeBitFlags($general);

        if ($entry->isZip64ExtensionsRequired()) {
            $extraFieldsCollection->add(ExtraFieldsFactory::createZip64Extra($entry));
        } elseif ($extraFieldsCollection->has(Zip64ExtraField::getHeaderId())) {
            $extraFieldsCollection->remove(Zip64ExtraField::getHeaderId());
        }
        return $entryContent;
    }

    /**
     * @param ZipEntry $entry
     * @param string $content
     * @return string
     * @throws ZipException
     */
    protected function determineBestCompressionMethod(ZipEntry $entry, $content)
    {
        if (null !== $content) {
            $entryContent = gzdeflate($content, $entry->getCompressionLevel());
            if (strlen($entryContent) < strlen($content)) {
                $entry->setMethod(ZipFileInterface::METHOD_DEFLATED);
                return $entryContent;
            }
            $entry->setMethod(ZipFileInterface::METHOD_STORED);
        }
        return $content;
    }

    /**
     * Writes a Central File Header record.
     *
     * @param OutputOffsetEntry $outEntry
     * @throws RuntimeException
     * @internal param OutPosEntry $entry
     */
    protected function writeCentralDirectoryHeader(OutputOffsetEntry $outEntry)
    {
        $entry = $outEntry->getEntry();
        $compressedSize = $entry->getCompressedSize();
        $size = $entry->getSize();
        // This test MUST NOT include the CRC-32 because VV_AE_2 sets it to
        // UNKNOWN!
        if (ZipEntry::UNKNOWN === ($compressedSize | $size)) {
            throw new RuntimeException("invalid entry");
        }
        $extra = $entry->getExtra();
        $extraSize = strlen($extra);

        $commentLength = strlen($entry->getComment());
        fwrite(
            $this->out,
            pack(
                'VvvvvVVVVvvvvvVV',
                // central file header signature   4 bytes  (0x02014b50)
                self::CENTRAL_FILE_HEADER_SIG,
                // version made by                 2 bytes
                ($entry->getPlatform() << 8) | 63,
                // version needed to extract       2 bytes
                $entry->getVersionNeededToExtract(),
                // general purpose bit flag        2 bytes
                $entry->getGeneralPurposeBitFlags(),
                // compression method              2 bytes
                $entry->getMethod(),
                // last mod file datetime          4 bytes
                $entry->getDosTime(),
                // crc-32                          4 bytes
                $entry->getCrc(),
                // compressed size                 4 bytes
                $entry->getCompressedSize(),
                // uncompressed size               4 bytes
                $entry->getSize(),
                // file name length                2 bytes
                strlen($entry->getName()),
                // extra field length              2 bytes
                $extraSize,
                // file comment length             2 bytes
                $commentLength,
                // disk number start               2 bytes
                0,
                // internal file attributes        2 bytes
                0,
                // external file attributes        4 bytes
                $entry->getExternalAttributes(),
                // relative offset of local header 4 bytes
                $outEntry->getOffset()
            )
        );
        // file name (variable size)
        fwrite($this->out, $entry->getName());
        if (0 < $extraSize) {
            // extra field (variable size)
            fwrite($this->out, $extra);
        }
        if (0 < $commentLength) {
            // file comment (variable size)
            fwrite($this->out, $entry->getComment());
        }
    }

    protected function writeEndOfCentralDirectoryRecord($centralDirectoryOffset)
    {
        $centralDirectoryEntriesCount = count($this->zipModel);
        $position = ftell($this->out);
        $centralDirectorySize = $position - $centralDirectoryOffset;
        $centralDirectoryEntriesZip64 = $centralDirectoryEntriesCount > 0xffff;
        $centralDirectorySizeZip64 = $centralDirectorySize > 0xffffffff;
        $centralDirectoryOffsetZip64 = $centralDirectoryOffset > 0xffffffff;
        $centralDirectoryEntries16 = $centralDirectoryEntriesZip64 ? 0xffff : (int)$centralDirectoryEntriesCount;
        $centralDirectorySize32 = $centralDirectorySizeZip64 ? 0xffffffff : $centralDirectorySize;
        $centralDirectoryOffset32 = $centralDirectoryOffsetZip64 ? 0xffffffff : $centralDirectoryOffset;
        $zip64 // ZIP64 extensions?
            = $centralDirectoryEntriesZip64
            || $centralDirectorySizeZip64
            || $centralDirectoryOffsetZip64;
        if ($zip64) {
            // [zip64 end of central directory record]
            // relative offset of the zip64 end of central directory record
            $zip64EndOfCentralDirectoryOffset = $position;
            // zip64 end of central dir
            // signature                       4 bytes  (0x06064b50)
            fwrite($this->out, pack('V', EndOfCentralDirectory::ZIP64_END_OF_CENTRAL_DIRECTORY_RECORD_SIG));
            // size of zip64 end of central
            // directory record                8 bytes
            fwrite($this->out, PackUtil::packLongLE(EndOfCentralDirectory::ZIP64_END_OF_CENTRAL_DIRECTORY_RECORD_MIN_LEN - 12));
            // version made by                 2 bytes
            // version needed to extract       2 bytes
            //                                 due to potential use of BZIP2 compression
            // number of this disk             4 bytes
            // number of the disk with the
            // start of the central directory  4 bytes
            fwrite($this->out, pack('vvVV', 63, 46, 0, 0));
            // total number of entries in the
            // central directory on this disk  8 bytes
            fwrite($this->out, PackUtil::packLongLE($centralDirectoryEntriesCount));
            // total number of entries in the
            // central directory               8 bytes
            fwrite($this->out, PackUtil::packLongLE($centralDirectoryEntriesCount));
            // size of the central directory   8 bytes
            fwrite($this->out, PackUtil::packLongLE($centralDirectorySize));
            // offset of start of central
            // directory with respect to
            // the starting disk number        8 bytes
            fwrite($this->out, PackUtil::packLongLE($centralDirectoryOffset));
            // zip64 extensible data sector    (variable size)

            // [zip64 end of central directory locator]
            // signature                       4 bytes  (0x07064b50)
            // number of the disk with the
            // start of the zip64 end of
            // central directory               4 bytes
            fwrite($this->out, pack('VV', EndOfCentralDirectory::ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR_SIG, 0));
            // relative offset of the zip64
            // end of central directory record 8 bytes
            fwrite($this->out, PackUtil::packLongLE($zip64EndOfCentralDirectoryOffset));
            // total number of disks           4 bytes
            fwrite($this->out, pack('V', 1));
        }
        $comment = $this->zipModel->getArchiveComment();
        $commentLength = strlen($comment);
        fwrite(
            $this->out,
            pack(
                'VvvvvVVv',
                // end of central dir signature    4 bytes  (0x06054b50)
                EndOfCentralDirectory::END_OF_CENTRAL_DIRECTORY_RECORD_SIG,
                // number of this disk             2 bytes
                0,
                // number of the disk with the
                // start of the central directory  2 bytes
                0,
                // total number of entries in the
                // central directory on this disk  2 bytes
                $centralDirectoryEntries16,
                // total number of entries in
                // the central directory           2 bytes
                $centralDirectoryEntries16,
                // size of the central directory   4 bytes
                $centralDirectorySize32,
                // offset of start of central
                // directory with respect to
                // the starting disk number        4 bytes
                $centralDirectoryOffset32,
                // .ZIP file comment length        2 bytes
                $commentLength
            )
        );
        if ($commentLength > 0) {
            // .ZIP file comment       (variable size)
            fwrite($this->out, $comment);
        }
    }

    /**
     * @return resource
     */
    public function getStream()
    {
        return $this->out;
    }
}