Passed
Push — master ( b1d4f6...7824cd )
by f
10:25
created

NelexaZip::extractFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 2
rs 10
1
<?php
2
3
namespace wapmorgan\UnifiedArchive\Drivers;
4
5
use PhpZip\ZipFile;
0 ignored issues
show
Bug introduced by
The type PhpZip\ZipFile was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use wapmorgan\UnifiedArchive\ArchiveEntry;
7
use wapmorgan\UnifiedArchive\ArchiveInformation;
8
use wapmorgan\UnifiedArchive\Exceptions\NonExistentArchiveFileException;
9
use wapmorgan\UnifiedArchive\Formats;
10
11
class NelexaZip extends BasicDriver
12
{
13
    /**
14
     * @var ZipFile
15
     */
16
    protected $zip;
17
18
    /**
19
     * @var array
20
     */
21
    protected $files;
22
23
    /**
24
     * @inheritDoc
25
     * @throws \PhpZip\Exception\ZipException
26
     */
27
    public function __construct($archiveFileName, $format, $password = null)
28
    {
29
        $this->zip = new ZipFile();
30
        $this->zip->openFile($archiveFileName);
31
        if ($password !== null) {
32
            $this->zip->setReadPassword($password);
33
        }
34
    }
35
36
    /**
37
     * @inheritDoc
38
     */
39
    public function getArchiveInformation()
40
    {
41
        $this->files = [];
42
        $information = new ArchiveInformation();
43
44
        foreach ($this->zip->getAllInfo() as $info) {
45
            if ($info->isFolder())
46
                continue;
47
48
            $this->files[] = $information->files[] = str_replace('\\', '/', $info->getName());
49
            $information->compressedFilesSize += $info->getCompressedSize();
50
            $information->uncompressedFilesSize += $info->getSize();
51
        }
52
        return $information;
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58
    public function getFileNames()
59
    {
60
        return $this->files;
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66
    public function isFileExists($fileName)
67
    {
68
        return $this->zip->hasEntry($fileName);
69
    }
70
71
    /**
72
     * @inheritDoc
73
     */
74
    public function getFileData($fileName)
75
    {
76
        $info = $this->zip->getEntryInfo($fileName);
77
        return new ArchiveEntry(
78
            $fileName,
79
            $info->getCompressedSize(),
80
            $info->getSize(),
81
            $info->getMtime(),
82
            null,
83
            $info->getComment(),
84
            $info->getCrc()
85
        );
86
    }
87
88
    /**
89
     * @inheritDoc
90
     */
91
    public function getFileContent($fileName)
92
    {
93
        return $this->zip->getEntryContents($fileName);
94
    }
95
96
    /**
97
     * @inheritDoc
98
     * @throws NonExistentArchiveFileException
99
     */
100
    public function getFileStream($fileName)
101
    {
102
        return static::wrapStringInStream($this->getFileContent($fileName));
103
    }
104
105
    /**
106
     * @inheritDoc
107
     */
108
    public function extractFiles($outputFolder, array $files)
109
    {
110
        // TODO: Implement extractFiles() method.
111
    }
112
113
    /**
114
     * @inheritDoc
115
     */
116
    public function extractArchive($outputFolder)
117
    {
118
        // TODO: Implement extractArchive() method.
119
    }
120
121
    /**
122
     * @inheritDoc
123
     * @throws \PhpZip\Exception\ZipException
124
     */
125
    public function addFileFromString($inArchiveName, $content)
126
    {
127
        return $this->zip->addFromString($inArchiveName, $content);
128
    }
129
130
    public function getComment()
131
    {
132
        return $this->zip->getArchiveComment();
133
    }
134
135
    public function setComment($comment)
136
    {
137
        return $this->zip->setArchiveComment($comment);
138
    }
139
140
    public static function getSupportedFormats()
141
    {
142
        return [
143
            Formats::ZIP,
144
        ];
145
    }
146
147
    public static function checkFormatSupport($format)
148
    {
149
        return [
150
            BasicDriver::OPEN,
151
            BasicDriver::OPEN_ENCRYPTED,
152
            BasicDriver::EXTRACT_CONTENT,
153
            BasicDriver::APPEND,
154
            BasicDriver::DELETE,
155
        ];
156
    }
157
}