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
|
|
|
|
16
|
|
|
/** @var \RarArchive */ |
17
|
|
|
protected $rar; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @return array |
21
|
|
|
*/ |
22
|
1 |
|
public static function getSupportedFormats() |
23
|
|
|
{ |
24
|
|
|
return [ |
25
|
1 |
|
Formats::RAR, |
26
|
|
|
]; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param $format |
31
|
|
|
* @return bool |
32
|
|
|
*/ |
33
|
|
|
public static function checkFormatSupport($format) |
34
|
|
|
{ |
35
|
|
|
switch ($format) { |
36
|
|
|
case Formats::RAR: |
37
|
|
|
return extension_loaded('rar'); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritDoc |
43
|
|
|
*/ |
44
|
|
|
public static function getDescription() |
45
|
|
|
{ |
46
|
|
|
return 'adapter for ext-rar'; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritDoc |
51
|
|
|
*/ |
52
|
|
|
public static function getInstallationInstruction() |
53
|
|
|
{ |
54
|
|
|
return !extension_loaded('rar') |
55
|
|
|
? 'install `rar` extension' |
56
|
|
|
: null; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritDoc |
61
|
|
|
*/ |
62
|
|
|
public static function canStream($format) |
63
|
|
|
{ |
64
|
|
|
return true; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritDoc |
69
|
|
|
*/ |
70
|
|
|
public function __construct($archiveFileName, $format, $password = null) |
71
|
|
|
{ |
72
|
|
|
\RarException::setUsingExceptions(true); |
73
|
|
|
$this->open($archiveFileName, $password); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $archiveFileName |
78
|
|
|
* @param $password |
79
|
|
|
* @throws Exception |
80
|
|
|
*/ |
81
|
|
|
protected function open($archiveFileName, $password) |
82
|
|
|
{ |
83
|
|
|
$this->rar = \RarArchive::open($archiveFileName, $password); |
84
|
|
|
if ($this->rar === false) { |
85
|
|
|
throw new Exception('Could not open Rar archive'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Rar format destructor |
91
|
|
|
*/ |
92
|
|
|
public function __destruct() |
93
|
|
|
{ |
94
|
|
|
$this->rar->close(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return ArchiveInformation |
99
|
|
|
*/ |
100
|
|
|
public function getArchiveInformation() |
101
|
|
|
{ |
102
|
|
|
$information = new ArchiveInformation(); |
103
|
|
|
foreach ($this->rar->getEntries() as $i => $entry) { |
104
|
|
|
if ($entry->isDirectory()) continue; |
105
|
|
|
$information->files[] = $entry->getName(); |
106
|
|
|
$information->compressedFilesSize += $entry->getPackedSize(); |
107
|
|
|
$information->uncompressedFilesSize += $entry->getUnpackedSize(); |
108
|
|
|
} |
109
|
|
|
return $information; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return string|null |
114
|
|
|
*/ |
115
|
|
|
public function getComment() |
116
|
|
|
{ |
117
|
|
|
return $this->rar->getComment(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
public function getFileNames() |
124
|
|
|
{ |
125
|
|
|
$files = []; |
126
|
|
|
foreach ($this->rar->getEntries() as $i => $entry) { |
127
|
|
|
if ($entry->isDirectory()) continue; |
128
|
|
|
$files[] = $entry->getName(); |
129
|
|
|
} |
130
|
|
|
return $files; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string $fileName |
135
|
|
|
* |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
|
|
public function isFileExists($fileName) |
139
|
|
|
{ |
140
|
|
|
return $this->rar->getEntry($fileName) !== false; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param string $fileName |
145
|
|
|
* |
146
|
|
|
* @return ArchiveEntry|false |
147
|
|
|
*/ |
148
|
|
|
public function getFileData($fileName) |
149
|
|
|
{ |
150
|
|
|
$entry = $this->rar->getEntry($fileName); |
151
|
|
|
return new ArchiveEntry($fileName, $entry->getPackedSize(), $entry->getUnpackedSize(), |
152
|
|
|
strtotime($entry->getFileTime()), $entry->getMethod() != self::NONE_RAR_COMPRESSION); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string $fileName |
157
|
|
|
* |
158
|
|
|
* @return string|false |
159
|
|
|
*/ |
160
|
|
|
public function getFileContent($fileName) |
161
|
|
|
{ |
162
|
|
|
$entry = $this->rar->getEntry($fileName); |
163
|
|
|
if ($entry->isDirectory()) return false; |
164
|
|
|
return stream_get_contents($entry->getStream()); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param string $fileName |
169
|
|
|
* |
170
|
|
|
* @return bool|resource|string |
171
|
|
|
*/ |
172
|
|
|
public function getFileStream($fileName) |
173
|
|
|
{ |
174
|
|
|
$entry = $this->rar->getEntry($fileName); |
175
|
|
|
if ($entry->isDirectory()) return false; |
|
|
|
|
176
|
|
|
return $entry->getStream(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param string $outputFolder |
181
|
|
|
* @param array $files |
182
|
|
|
* |
183
|
|
|
* @return false|int |
184
|
|
|
*/ |
185
|
|
|
public function extractFiles($outputFolder, array $files) |
186
|
|
|
{ |
187
|
|
|
$count = 0; |
188
|
|
|
foreach ($files as $file) { |
189
|
|
|
if ($this->rar->getEntry($file)->extract($outputFolder)) { |
190
|
|
|
$count++; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
return $count; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param string $outputFolder |
198
|
|
|
* |
199
|
|
|
* @return false|resource |
200
|
|
|
*/ |
201
|
|
|
public function extractArchive($outputFolder) |
202
|
|
|
{ |
203
|
|
|
return $this->extractFiles($outputFolder, $this->getFileNames()); |
|
|
|
|
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param string $inArchiveName |
208
|
|
|
* @param string $content |
209
|
|
|
* @return bool|void |
210
|
|
|
* @throws UnsupportedOperationException |
211
|
|
|
*/ |
212
|
|
|
public function addFileFromString($inArchiveName, $content) |
213
|
|
|
{ |
214
|
|
|
throw new UnsupportedOperationException(); |
215
|
|
|
} |
216
|
|
|
} |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: