1
|
|
|
<?php |
2
|
|
|
namespace wapmorgan\UnifiedArchive\Formats; |
3
|
|
|
|
4
|
|
|
use Exception; |
5
|
|
|
use wapmorgan\UnifiedArchive\ArchiveEntry; |
6
|
|
|
use wapmorgan\UnifiedArchive\ArchiveInformation; |
7
|
|
|
|
8
|
|
|
class SevenZip extends BasicFormat |
9
|
|
|
{ |
10
|
|
|
/** @var Archive7z */ |
11
|
|
|
protected $sevenZip; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* BasicFormat constructor. |
15
|
|
|
* |
16
|
|
|
* @param string $archiveFileName |
17
|
|
|
* |
18
|
|
|
* @throws \Exception |
19
|
|
|
*/ |
20
|
3 |
|
public function __construct($archiveFileName) |
21
|
|
|
{ |
22
|
|
|
try { |
23
|
3 |
|
$this->sevenZip = new Archive7z($archiveFileName, null, null); |
24
|
|
|
} catch (\Archive7z\Exception $e) { |
25
|
|
|
throw new Exception('Could not open 7Zip archive: '.$e->getMessage(), $e->getCode(), $e); |
26
|
|
|
} |
27
|
3 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return ArchiveInformation |
31
|
|
|
*/ |
32
|
3 |
|
public function getArchiveInformation() |
33
|
|
|
{ |
34
|
3 |
|
$information = new ArchiveInformation(); |
35
|
3 |
|
foreach ($this->sevenZip->getEntries() as $entry) { |
36
|
3 |
|
$information->files[] = $entry->getPath(); |
37
|
3 |
|
$information->compressedFilesSize += (int)$entry->getPackedSize(); |
38
|
3 |
|
$information->uncompressedFilesSize += (int)$entry->getSize(); |
39
|
|
|
} |
40
|
3 |
|
return $information; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
public function getFileNames() |
47
|
|
|
{ |
48
|
|
|
$files = []; |
49
|
|
|
foreach ($this->sevenZip->getEntries() as $entry) |
50
|
|
|
$files[] = $entry->getPath(); |
51
|
|
|
return $files; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $fileName |
56
|
|
|
* |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public function isFileExists($fileName) |
60
|
|
|
{ |
61
|
|
|
return $this->sevenZip->getEntry($fileName) !== null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $fileName |
66
|
|
|
* |
67
|
|
|
* @return ArchiveEntry|false |
68
|
|
|
*/ |
69
|
1 |
|
public function getFileData($fileName) |
70
|
|
|
{ |
71
|
1 |
|
$entry = $this->sevenZip->getEntry($fileName); |
72
|
1 |
|
return new ArchiveEntry($fileName, $entry->getPackedSize(), $entry->getSize(), |
73
|
1 |
|
strtotime($entry->getModified())); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $fileName |
78
|
|
|
* |
79
|
|
|
* @return string|false |
80
|
|
|
*/ |
81
|
1 |
|
public function getFileContent($fileName) |
82
|
|
|
{ |
83
|
1 |
|
$entry = $this->sevenZip->getEntry($fileName); |
84
|
1 |
|
return $entry->getContent(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $fileName |
89
|
|
|
* |
90
|
|
|
* @return bool|resource|string |
91
|
|
|
*/ |
92
|
1 |
|
public function getFileResource($fileName) |
93
|
|
|
{ |
94
|
1 |
|
$resource = fopen('php://temp', 'r+'); |
95
|
1 |
|
$entry = $this->sevenZip->getEntry($fileName); |
96
|
|
|
|
97
|
1 |
|
fwrite($resource, $entry->getContent()); |
|
|
|
|
98
|
1 |
|
rewind($resource); |
|
|
|
|
99
|
1 |
|
return $resource; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $outputFolder |
104
|
|
|
* @param array $files |
105
|
|
|
* |
106
|
|
|
* @return false|int |
107
|
|
|
* @throws \Archive7z\Exception |
108
|
|
|
* @throws \Exception |
109
|
|
|
*/ |
110
|
|
|
public function extractFiles($outputFolder, array $files) |
111
|
|
|
{ |
112
|
|
|
$this->sevenZip->setOutputDirectory($outputFolder); |
113
|
|
|
$count = 0; |
114
|
|
|
try { |
115
|
|
|
|
116
|
|
|
foreach ($files as $file) { |
117
|
|
|
$this->sevenZip->extractEntry($file); |
118
|
|
|
$count++; |
119
|
|
|
} |
120
|
|
|
return $count; |
121
|
|
|
} catch (Exception $e) { |
122
|
|
|
throw new Exception('Could not extract archive: '.$e->getMessage(), $e->getCode(), $e); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $outputFolder |
128
|
|
|
* |
129
|
|
|
* @return false|bool |
130
|
|
|
* @throws \Exception |
131
|
|
|
*/ |
132
|
|
|
public function extractArchive($outputFolder) |
133
|
|
|
{ |
134
|
|
|
$this->sevenZip->setOutputDirectory($outputFolder); |
135
|
|
|
try { |
136
|
|
|
$this->sevenZip->extract(); |
137
|
|
|
return true; |
|
|
|
|
138
|
|
|
} catch (Exception $e) { |
139
|
|
|
throw new Exception('Could not extract archive: '.$e->getMessage(), $e->getCode(), $e); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param array $files |
145
|
|
|
* |
146
|
|
|
* @return false|int |
147
|
|
|
* @throws \Exception |
148
|
|
|
*/ |
149
|
|
|
public function deleteFiles(array $files) |
150
|
|
|
{ |
151
|
|
|
$count = 0; |
152
|
|
|
try { |
153
|
|
|
foreach ($files as $file) { |
154
|
|
|
$this->sevenZip->delEntry($file); |
155
|
|
|
$count++; |
156
|
|
|
} |
157
|
|
|
return $count; |
158
|
|
|
} catch (Exception $e) { |
159
|
|
|
throw new Exception('Could not modify archive: '.$e->getMessage(), $e->getCode(), $e); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param array $files |
165
|
|
|
* |
166
|
|
|
* @return false|int |
167
|
|
|
* @throws \Exception |
168
|
|
|
*/ |
169
|
|
|
public function addFiles(array $files) |
170
|
|
|
{ |
171
|
|
|
$added_files = 0; |
172
|
|
|
try { |
173
|
|
|
foreach ($files as $localName => $filename) { |
174
|
|
|
if (!is_null($filename)) { |
175
|
|
|
$this->sevenZip->addEntry($filename); |
176
|
|
|
$this->sevenZip->renameEntry($filename, $localName); |
177
|
|
|
$added_files++; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} catch (Exception $e) { |
181
|
|
|
throw new Exception('Could not modify archive: '.$e->getMessage(), $e->getCode(), $e); |
182
|
|
|
} |
183
|
|
|
return $added_files; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param array $files |
188
|
|
|
* @param string $archiveFileName |
189
|
|
|
* |
190
|
|
|
* @return false|int |
191
|
|
|
* @throws \Archive7z\Exception |
192
|
|
|
* @throws \Exception |
193
|
|
|
*/ |
194
|
|
|
public static function createArchive(array $files, $archiveFileName) { |
195
|
|
|
$seven_zip = new Archive7z($archiveFileName); |
196
|
|
|
try { |
197
|
|
|
foreach ($files as $localName => $filename) { |
198
|
|
|
if ($filename !== null) { |
199
|
|
|
$seven_zip->addEntry($filename, true); |
200
|
|
|
$seven_zip->renameEntry($filename, $localName); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
unset($seven_zip); |
204
|
|
|
} catch (Exception $e) { |
205
|
|
|
throw new Exception('Could not create archive: '.$e->getMessage(), $e->getCode(), $e); |
206
|
|
|
} |
207
|
|
|
return count($files); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return bool |
212
|
|
|
* @throws \Archive7z\Exception |
213
|
|
|
*/ |
214
|
|
|
public static function canCreateArchive() |
215
|
|
|
{ |
216
|
|
|
return static::canAddFiles(); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return bool |
221
|
|
|
* @throws \Archive7z\Exception |
222
|
|
|
*/ |
223
|
|
|
public static function canAddFiles() |
224
|
|
|
{ |
225
|
|
|
$version = Archive7z::getBinaryVersion(); |
226
|
|
|
return $version !== false && version_compare('9.30', $version, '<='); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @return bool |
231
|
|
|
*/ |
232
|
|
|
public static function canDeleteFiles() |
233
|
|
|
{ |
234
|
|
|
return true; |
235
|
|
|
} |
236
|
|
|
} |