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