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

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

namespace PhpZip\Model\Entry;

use PhpZip\Exception\ZipException;
use PhpZip\Stream\ZipInputStreamInterface;

/**
 * This class is used to represent a ZIP file entry.
 *
 * @see https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT .ZIP File Format Specification
 * @author Ne-Lexa alexey@nelexa.ru
 * @license MIT
 */
class ZipSourceEntry extends ZipAbstractEntry
{
    /**
     * Max size cached content in memory.
     */
    const MAX_SIZE_CACHED_CONTENT_IN_MEMORY = 524288; // 512 kb
    /**
     * @var ZipInputStreamInterface
     */
    protected $inputStream;
    /**
     * @var string|resource Cached entry content.
     */
    protected $entryContent;
    /**
     * @var string
     */
    protected $readPassword;
    /**
     * @var bool
     */
    private $clone = false;

    /**
     * ZipSourceEntry constructor.
     * @param ZipInputStreamInterface $inputStream
     */
    public function __construct(ZipInputStreamInterface $inputStream)
    {
        parent::__construct();
        $this->inputStream = $inputStream;
    }

    /**
     * @return ZipInputStreamInterface
     */
    public function getInputStream()
    {
        return $this->inputStream;
    }

    /**
     * Returns an string content of the given entry.
     *
     * @return string
     * @throws ZipException
     */
    public function getEntryContent()
    {
        if (null === $this->entryContent) {
            $content = $this->inputStream->readEntryContent($this);
            if ($this->getSize() < self::MAX_SIZE_CACHED_CONTENT_IN_MEMORY) {
                $this->entryContent = $content;
            } else {
                $this->entryContent = fopen('php://temp', 'rb');
                fwrite($this->entryContent, $content);
            }
            return $content;
        }
        if (is_resource($this->entryContent)) {
            return stream_get_contents($this->entryContent, -1, 0);
        }
        return $this->entryContent;
    }

    /**
     * Clone extra fields
     */
    public function __clone()
    {
        $this->clone = true;
        parent::__clone();
    }

    public function __destruct()
    {
        if (!$this->clone && null !== $this->entryContent && is_resource($this->entryContent)) {
            fclose($this->entryContent);
        }
    }
}