Issues (155)

src/Drivers/Cab.php (1 issue)

Labels
Severity
1
<?php
2
namespace wapmorgan\UnifiedArchive\Drivers;
3
4
use CabArchive;
0 ignored issues
show
The type CabArchive 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...
5
use Exception;
6
use wapmorgan\UnifiedArchive\Abilities;
7
use wapmorgan\UnifiedArchive\ArchiveEntry;
8
use wapmorgan\UnifiedArchive\ArchiveInformation;
9
use wapmorgan\UnifiedArchive\Drivers\Basic\BasicPureDriver;
10
use wapmorgan\UnifiedArchive\Exceptions\ArchiveExtractionException;
11
use wapmorgan\UnifiedArchive\Exceptions\UnsupportedOperationException;
12
use wapmorgan\UnifiedArchive\Formats;
13
14
class Cab extends BasicPureDriver
15
{
16
    const PACKAGE_NAME = 'wapmorgan/cab-archive';
17
    const MAIN_CLASS = '\CabArchive';
18
19
    /** @var CabArchive */
20
    protected $cab;
21
22 1
    /**
23
     * @inheritDoc
24
     */
25 1
    public static function getDescription()
26
    {
27
        return 'php-library';
28
    }
29
30
    /**
31
     * @return array
32
     */
33
    public static function getFormats()
34
    {
35
        return [
36
            Formats::CAB,
37
        ];
38
    }
39
40
    /**
41
     * @param $format
42
     * @return array
43
     */
44
    public static function getFormatAbilities($format)
45
    {
46
        if (!class_exists('\CabArchive')) {
47
            return [];
48
        }
49
50
        switch ($format) {
51
            case Formats::CAB:
52
                $abilities = [
53
                    Abilities::OPEN,
54
                ];
55
56
                $parts = explode('.', PHP_VERSION);
57
                // not supported on versions below 7.0.22, 7.1.8, 7.2.0
58
                if (
59
                    $parts[0] > 7
60
                    || $parts[1] >= 2
61
                    || (($parts[1] == 1 && $parts[2] >= 8) || ($parts[1] == 0 && $parts[2] >= 22))
62
                ) {
63
                    $abilities[] = Abilities::EXTRACT_CONTENT;
64
                }
65
66
                return $abilities;
67
        }
68
    }
69
70
    /**
71
     * @inheritDoc
72
     * @throws Exception
73
     */
74
    public function __construct($archiveFileName, $format, $password = null)
75
    {
76
        parent::__construct($archiveFileName, $format);
77
        if ($password !== null)
78
            throw new UnsupportedOperationException('Cab archive does not support password!');
79
        $this->open($archiveFileName);
80
    }
81
82
    /**
83
     * Iso format destructor
84
     */
85
    public function __destruct()
86
    {
87
        $this->cab = null;
88
    }
89
90
    /**
91
     * @param $archiveFileName
92
     * @throws Exception
93
     */
94
    protected function open($archiveFileName)
95
    {
96
        try {
97
            $this->cab = new CabArchive($archiveFileName);
98
        } catch (Exception $e) {
99
            throw new Exception('Could not open Cab archive: '.$e->getMessage(), $e->getCode(), $e);
100
        }
101
    }
102
103
    /**
104
     * @return ArchiveInformation
105
     */
106
    public function getArchiveInformation()
107
    {
108
        $information = new ArchiveInformation();
109
        foreach ($this->cab->getFileNames() as $file) {
110
            $information->files[] = $file;
111
            $file_info = $this->cab->getFileData($file);
112
            $information->uncompressedFilesSize += $file_info->size;
113
            $information->compressedFilesSize += $file_info->packedSize;
114
        }
115
        return $information;
116
    }
117
118
    /**
119
     * @return array
120
     */
121
    public function getFileNames()
122
    {
123
        return $this->cab->getFileNames();
124
    }
125
126
    /**
127
     * @param string $fileName
128
     *
129
     * @return bool
130
     */
131
    public function isFileExists($fileName)
132
    {
133
        return in_array($fileName, $this->cab->getFileNames(), true);
134
    }
135
136
    /**
137
     * @param string $fileName
138
     *
139
     * @return ArchiveEntry|false
140
     */
141
    public function getFileData($fileName)
142
    {
143
        $data = $this->cab->getFileData($fileName);
144
145
        return new ArchiveEntry($fileName, $data->packedSize, $data->size, $data->unixtime, $data->is_compressed);
146
    }
147
148
    /**
149
     * @param string $fileName
150
     *
151
     * @return string|false
152
     * @throws Exception
153
     */
154
    public function getFileContent($fileName)
155
    {
156
        return $this->cab->getFileContent($fileName);
157
    }
158
159
    /**
160
     * @param string $fileName
161
     *
162
     * @return bool|resource|string
163
     * @throws Exception
164
     */
165
    public function getFileStream($fileName)
166
    {
167
        return self::wrapStringInStream($this->cab->getFileContent($fileName));
168
    }
169
170
    /**
171
     * @param string $outputFolder
172
     * @param array $files
173
     * @return int Number of extracted files
174
     * @throws ArchiveExtractionException
175
     */
176
    public function extractFiles($outputFolder, array $files)
177
    {
178
        try {
179
            return $this->cab->extract($outputFolder, $files);
180
        } catch (Exception $e) {
181
            throw new ArchiveExtractionException($e->getMessage(),
182
                $e->getCode(),
183
                $e->getPrevious()
184
            );
185
        }
186
    }
187
188
    /**
189
     * @param string $outputFolder
190
     * @return int
191
     * @throws ArchiveExtractionException
192
     */
193
    public function extractArchive($outputFolder)
194
    {
195
        try {
196
            return $this->cab->extract($outputFolder);
197
        } catch (Exception $e) {
198
            throw new ArchiveExtractionException($e->getMessage(),
199
                $e->getCode(),
200
                $e->getPrevious()
201
            );
202
        }
203
    }
204
}
205