1
|
|
|
<?php |
2
|
|
|
namespace wapmorgan\UnifiedArchive\Drivers; |
3
|
|
|
|
4
|
|
|
use Exception; |
5
|
|
|
use wapmorgan\UnifiedArchive\ArchiveEntry; |
6
|
|
|
use wapmorgan\UnifiedArchive\ArchiveInformation; |
7
|
|
|
use wapmorgan\UnifiedArchive\Drivers\BasicDriver; |
8
|
|
|
use wapmorgan\UnifiedArchive\Exceptions\ArchiveModificationException; |
9
|
|
|
use wapmorgan\UnifiedArchive\Exceptions\UnsupportedOperationException; |
10
|
|
|
use wapmorgan\UnifiedArchive\Formats; |
11
|
|
|
|
12
|
|
|
class Rar extends BasicDriver |
13
|
|
|
{ |
14
|
|
|
const NONE_RAR_COMPRESSION = 48; |
15
|
|
|
const TYPE = self::TYPE_EXTENSION; |
16
|
|
|
|
17
|
|
|
/** @var \RarArchive */ |
18
|
|
|
protected $rar; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @inheritDoc |
22
|
1 |
|
*/ |
23
|
|
|
public static function getDescription() |
24
|
|
|
{ |
25
|
1 |
|
return 'adapter for ext-rar' . (self::isInstalled() ? ' (' . phpversion('rar') . ')' : null); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public static function isInstalled() |
29
|
|
|
{ |
30
|
|
|
return extension_loaded('rar'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritDoc |
35
|
|
|
*/ |
36
|
|
|
public static function getInstallationInstruction() |
37
|
|
|
{ |
38
|
|
|
return 'install [rar] extension.' . "\n" . 'Can be installed with pecl: `pecl install rar`'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
|
public static function getSupportedFormats() |
45
|
|
|
{ |
46
|
|
|
return [ |
47
|
|
|
Formats::RAR, |
48
|
|
|
]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param $format |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public static function checkFormatSupport($format) |
56
|
|
|
{ |
57
|
|
|
if (!static::isInstalled()) { |
58
|
|
|
return []; |
59
|
|
|
} |
60
|
|
|
switch ($format) { |
61
|
|
|
case Formats::RAR: |
62
|
|
|
return [ |
63
|
|
|
BasicDriver::OPEN, |
64
|
|
|
BasicDriver::OPEN_ENCRYPTED, |
65
|
|
|
BasicDriver::EXTRACT_CONTENT, |
66
|
|
|
BasicDriver::STREAM_CONTENT, |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @inheritDoc |
73
|
|
|
*/ |
74
|
|
|
public static function canStream($format) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritDoc |
81
|
|
|
*/ |
82
|
|
|
public function __construct($archiveFileName, $format, $password = null) |
83
|
|
|
{ |
84
|
|
|
parent::__construct($archiveFileName, $format); |
85
|
|
|
\RarException::setUsingExceptions(true); |
86
|
|
|
$this->open($archiveFileName, $password); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param $archiveFileName |
91
|
|
|
* @param $password |
92
|
|
|
* @throws Exception |
93
|
|
|
*/ |
94
|
|
|
protected function open($archiveFileName, $password) |
95
|
|
|
{ |
96
|
|
|
$this->rar = \RarArchive::open($archiveFileName, $password); |
97
|
|
|
if ($this->rar === false) { |
98
|
|
|
throw new Exception('Could not open Rar archive'); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Rar format destructor |
104
|
|
|
*/ |
105
|
|
|
public function __destruct() |
106
|
|
|
{ |
107
|
|
|
$this->rar->close(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return ArchiveInformation |
112
|
|
|
*/ |
113
|
|
|
public function getArchiveInformation() |
114
|
|
|
{ |
115
|
|
|
$information = new ArchiveInformation(); |
116
|
|
|
foreach ($this->rar->getEntries() as $i => $entry) { |
117
|
|
|
if ($entry->isDirectory()) continue; |
118
|
|
|
$information->files[] = $entry->getName(); |
119
|
|
|
$information->compressedFilesSize += $entry->getPackedSize(); |
120
|
|
|
$information->uncompressedFilesSize += $entry->getUnpackedSize(); |
121
|
|
|
} |
122
|
|
|
return $information; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return string|null |
127
|
|
|
*/ |
128
|
|
|
public function getComment() |
129
|
|
|
{ |
130
|
|
|
return $this->rar->getComment(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
public function getFileNames() |
137
|
|
|
{ |
138
|
|
|
$files = []; |
139
|
|
|
foreach ($this->rar->getEntries() as $i => $entry) { |
140
|
|
|
if ($entry->isDirectory()) continue; |
141
|
|
|
$files[] = $entry->getName(); |
142
|
|
|
} |
143
|
|
|
return $files; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $fileName |
148
|
|
|
* |
149
|
|
|
* @return bool |
150
|
|
|
*/ |
151
|
|
|
public function isFileExists($fileName) |
152
|
|
|
{ |
153
|
|
|
return $this->rar->getEntry($fileName) !== false; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $fileName |
158
|
|
|
* |
159
|
|
|
* @return ArchiveEntry|false |
160
|
|
|
*/ |
161
|
|
|
public function getFileData($fileName) |
162
|
|
|
{ |
163
|
|
|
$entry = $this->rar->getEntry($fileName); |
164
|
|
|
return new ArchiveEntry($fileName, $entry->getPackedSize(), $entry->getUnpackedSize(), |
165
|
|
|
strtotime($entry->getFileTime()), $entry->getMethod() != self::NONE_RAR_COMPRESSION); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param string $fileName |
170
|
|
|
* |
171
|
|
|
* @return string|false |
172
|
|
|
*/ |
173
|
|
|
public function getFileContent($fileName) |
174
|
|
|
{ |
175
|
|
|
$entry = $this->rar->getEntry($fileName); |
176
|
|
|
if ($entry->isDirectory()) return false; |
177
|
|
|
return stream_get_contents($entry->getStream()); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param string $fileName |
182
|
|
|
* |
183
|
|
|
* @return bool|resource|string |
184
|
|
|
*/ |
185
|
|
|
public function getFileStream($fileName) |
186
|
|
|
{ |
187
|
|
|
$entry = $this->rar->getEntry($fileName); |
188
|
|
|
if ($entry->isDirectory()) return false; |
|
|
|
|
189
|
|
|
return $entry->getStream(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param string $outputFolder |
194
|
|
|
* @param array $files |
195
|
|
|
* |
196
|
|
|
* @return false|int |
197
|
|
|
*/ |
198
|
|
|
public function extractFiles($outputFolder, array $files) |
199
|
|
|
{ |
200
|
|
|
$count = 0; |
201
|
|
|
foreach ($files as $file) { |
202
|
|
|
if ($this->rar->getEntry($file)->extract($outputFolder)) { |
203
|
|
|
$count++; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
return $count; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param string $outputFolder |
211
|
|
|
* |
212
|
|
|
* @return false|resource |
213
|
|
|
*/ |
214
|
|
|
public function extractArchive($outputFolder) |
215
|
|
|
{ |
216
|
|
|
return $this->extractFiles($outputFolder, $this->getFileNames()); |
|
|
|
|
217
|
|
|
} |
218
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.