1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpZip\Model; |
4
|
|
|
|
5
|
|
|
use PhpZip\Constants\ZipEncryptionMethod; |
6
|
|
|
use PhpZip\Exception\InvalidArgumentException; |
7
|
|
|
use PhpZip\Exception\ZipEntryNotFoundException; |
8
|
|
|
use PhpZip\Exception\ZipException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class ZipContainer. |
12
|
|
|
*/ |
13
|
|
|
class ZipContainer extends ImmutableZipContainer |
14
|
|
|
{ |
15
|
|
|
/** @var ImmutableZipContainer|null */ |
16
|
|
|
private $sourceContainer; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var int|null Apk zipalign value |
20
|
|
|
* |
21
|
|
|
* @todo remove and use in ApkFileWriter |
22
|
|
|
*/ |
23
|
|
|
private $zipAlign; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* MutableZipContainer constructor. |
27
|
|
|
* |
28
|
|
|
* @param ImmutableZipContainer|null $sourceContainer |
29
|
|
|
*/ |
30
|
174 |
|
public function __construct(ImmutableZipContainer $sourceContainer = null) |
31
|
|
|
{ |
32
|
174 |
|
$entries = []; |
33
|
174 |
|
$archiveComment = null; |
34
|
|
|
|
35
|
174 |
|
if ($sourceContainer !== null) { |
36
|
84 |
|
foreach ($sourceContainer->getEntries() as $entryName => $entry) { |
37
|
83 |
|
$entries[$entryName] = clone $entry; |
38
|
|
|
} |
39
|
84 |
|
$archiveComment = $sourceContainer->getArchiveComment(); |
40
|
|
|
} |
41
|
174 |
|
parent::__construct($entries, $archiveComment); |
42
|
174 |
|
$this->sourceContainer = $sourceContainer; |
43
|
174 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return ImmutableZipContainer|null |
47
|
|
|
*/ |
48
|
|
|
public function getSourceContainer() |
49
|
|
|
{ |
50
|
|
|
return $this->sourceContainer; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param ZipEntry $entry |
55
|
|
|
*/ |
56
|
91 |
|
public function addEntry(ZipEntry $entry) |
57
|
|
|
{ |
58
|
91 |
|
$this->entries[$entry->getName()] = $entry; |
59
|
91 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string|ZipEntry $entry |
63
|
|
|
* |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
13 |
|
public function deleteEntry($entry) |
67
|
|
|
{ |
68
|
13 |
|
$entry = $entry instanceof ZipEntry ? $entry->getName() : (string) $entry; |
69
|
|
|
|
70
|
13 |
|
if (isset($this->entries[$entry])) { |
71
|
12 |
|
unset($this->entries[$entry]); |
72
|
|
|
|
73
|
12 |
|
return true; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string|ZipEntry $old |
81
|
|
|
* @param string|ZipEntry $new |
82
|
|
|
* |
83
|
|
|
* @throws ZipException |
84
|
|
|
* |
85
|
|
|
* @return ZipEntry New zip entry |
86
|
|
|
*/ |
87
|
5 |
|
public function renameEntry($old, $new) |
88
|
|
|
{ |
89
|
5 |
|
$old = $old instanceof ZipEntry ? $old->getName() : (string) $old; |
90
|
5 |
|
$new = $new instanceof ZipEntry ? $new->getName() : (string) $new; |
91
|
|
|
|
92
|
5 |
|
if (isset($this->entries[$new])) { |
93
|
1 |
|
throw new InvalidArgumentException('New entry name ' . $new . ' is exists.'); |
94
|
|
|
} |
95
|
|
|
|
96
|
4 |
|
$entry = $this->getEntry($old); |
97
|
3 |
|
$newEntry = $entry->rename($new); |
98
|
|
|
|
99
|
3 |
|
$this->deleteEntry($entry); |
100
|
3 |
|
$this->addEntry($newEntry); |
101
|
|
|
|
102
|
3 |
|
return $newEntry; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string|ZipEntry $entryName |
107
|
|
|
* |
108
|
|
|
* @throws ZipEntryNotFoundException |
109
|
|
|
* |
110
|
|
|
* @return ZipEntry |
111
|
|
|
*/ |
112
|
70 |
|
public function getEntry($entryName) |
113
|
|
|
{ |
114
|
70 |
|
$entry = $this->getEntryOrNull($entryName); |
115
|
|
|
|
116
|
70 |
|
if ($entry !== null) { |
117
|
66 |
|
return $entry; |
118
|
|
|
} |
119
|
|
|
|
120
|
4 |
|
throw new ZipEntryNotFoundException($entryName); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string|ZipEntry $entryName |
125
|
|
|
* |
126
|
|
|
* @return ZipEntry|null |
127
|
|
|
*/ |
128
|
70 |
|
public function getEntryOrNull($entryName) |
129
|
|
|
{ |
130
|
70 |
|
$entryName = $entryName instanceof ZipEntry ? $entryName->getName() : (string) $entryName; |
131
|
|
|
|
132
|
70 |
|
return isset($this->entries[$entryName]) ? $this->entries[$entryName] : null; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param string|ZipEntry $entryName |
137
|
|
|
* |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
42 |
|
public function hasEntry($entryName) |
141
|
|
|
{ |
142
|
42 |
|
$entryName = $entryName instanceof ZipEntry ? $entryName->getName() : (string) $entryName; |
143
|
|
|
|
144
|
42 |
|
return isset($this->entries[$entryName]); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Delete all entries. |
149
|
|
|
*/ |
150
|
1 |
|
public function deleteAll() |
151
|
|
|
{ |
152
|
1 |
|
$this->entries = []; |
153
|
1 |
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Delete entries by regex pattern. |
157
|
|
|
* |
158
|
|
|
* @param string $regexPattern Regex pattern |
159
|
|
|
* |
160
|
|
|
* @return ZipEntry[] Deleted entries |
161
|
|
|
*/ |
162
|
|
|
public function deleteByRegex($regexPattern) |
163
|
|
|
{ |
164
|
|
|
if (empty($regexPattern)) { |
165
|
|
|
throw new InvalidArgumentException('The regex pattern is not specified'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** @var ZipEntry[] $found */ |
169
|
|
|
$found = []; |
170
|
|
|
|
171
|
|
|
foreach ($this->entries as $entryName => $entry) { |
172
|
|
|
if (preg_match($regexPattern, $entryName)) { |
173
|
|
|
$found[] = $entry; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
foreach ($found as $entry) { |
178
|
|
|
$this->deleteEntry($entry); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $found; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Undo all changes done in the archive. |
186
|
|
|
*/ |
187
|
1 |
|
public function unchangeAll() |
188
|
|
|
{ |
189
|
1 |
|
$this->entries = []; |
190
|
|
|
|
191
|
1 |
|
if ($this->sourceContainer !== null) { |
192
|
1 |
|
foreach ($this->sourceContainer->getEntries() as $entry) { |
193
|
1 |
|
$this->entries[$entry->getName()] = clone $entry; |
194
|
|
|
} |
195
|
|
|
} |
196
|
1 |
|
$this->unchangeArchiveComment(); |
197
|
1 |
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Undo change archive comment. |
201
|
|
|
*/ |
202
|
2 |
|
public function unchangeArchiveComment() |
203
|
|
|
{ |
204
|
2 |
|
$this->archiveComment = null; |
205
|
|
|
|
206
|
2 |
|
if ($this->sourceContainer !== null) { |
207
|
2 |
|
$this->archiveComment = $this->sourceContainer->archiveComment; |
208
|
|
|
} |
209
|
2 |
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Revert all changes done to an entry with the given name. |
213
|
|
|
* |
214
|
|
|
* @param string|ZipEntry $entry Entry name or ZipEntry |
215
|
|
|
* |
216
|
|
|
* @return bool |
217
|
|
|
*/ |
218
|
1 |
|
public function unchangeEntry($entry) |
219
|
|
|
{ |
220
|
1 |
|
$entry = $entry instanceof ZipEntry ? $entry->getName() : (string) $entry; |
221
|
|
|
|
222
|
|
|
if ( |
223
|
1 |
|
$this->sourceContainer !== null && |
224
|
1 |
|
isset($this->entries[$entry], $this->sourceContainer->entries[$entry]) |
225
|
|
|
) { |
226
|
1 |
|
$this->entries[$entry] = clone $this->sourceContainer->entries[$entry]; |
227
|
|
|
|
228
|
1 |
|
return true; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return false; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Entries sort by name. |
236
|
|
|
* |
237
|
|
|
* Example: |
238
|
|
|
* ```php |
239
|
|
|
* $zipContainer->sortByName(static function (string $nameA, string $nameB): int { |
240
|
|
|
* return strcmp($nameA, $nameB); |
241
|
|
|
* }); |
242
|
|
|
* ``` |
243
|
|
|
* |
244
|
|
|
* @param callable $cmp |
245
|
|
|
*/ |
246
|
|
|
public function sortByName(callable $cmp) |
247
|
|
|
{ |
248
|
|
|
uksort($this->entries, $cmp); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Entries sort by entry. |
253
|
|
|
* |
254
|
|
|
* Example: |
255
|
|
|
* ```php |
256
|
|
|
* $zipContainer->sortByEntry(static function (ZipEntry $a, ZipEntry $b): int { |
257
|
|
|
* return strcmp($a->getName(), $b->getName()); |
258
|
|
|
* }); |
259
|
|
|
* ``` |
260
|
|
|
* |
261
|
|
|
* @param callable $cmp |
262
|
|
|
*/ |
263
|
|
|
public function sortByEntry(callable $cmp) |
264
|
|
|
{ |
265
|
|
|
uasort($this->entries, $cmp); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @param string|null $archiveComment |
270
|
|
|
*/ |
271
|
4 |
|
public function setArchiveComment($archiveComment) |
272
|
|
|
{ |
273
|
4 |
|
if ($archiveComment !== null && $archiveComment !== '') { |
274
|
4 |
|
$archiveComment = (string) $archiveComment; |
275
|
4 |
|
$length = \strlen($archiveComment); |
276
|
|
|
|
277
|
4 |
|
if ($length > 0xffff) { |
278
|
1 |
|
throw new InvalidArgumentException('Length comment out of range'); |
279
|
|
|
} |
280
|
|
|
} |
281
|
3 |
|
$this->archiveComment = $archiveComment; |
282
|
3 |
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @return ZipEntryMatcher |
286
|
|
|
*/ |
287
|
17 |
|
public function matcher() |
288
|
|
|
{ |
289
|
17 |
|
return new ZipEntryMatcher($this); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Specify a password for extracting files. |
294
|
|
|
* |
295
|
|
|
* @param string|null $password |
296
|
|
|
*/ |
297
|
9 |
|
public function setReadPassword($password) |
298
|
|
|
{ |
299
|
9 |
|
if ($this->sourceContainer !== null) { |
300
|
9 |
|
foreach ($this->sourceContainer->entries as $entry) { |
301
|
9 |
|
if ($entry->isEncrypted()) { |
302
|
9 |
|
$entry->setPassword($password); |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
} |
306
|
9 |
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* @param string $entryName |
310
|
|
|
* @param string $password |
311
|
|
|
* |
312
|
|
|
* @throws ZipEntryNotFoundException |
313
|
|
|
* @throws ZipException |
314
|
|
|
*/ |
315
|
2 |
|
public function setReadPasswordEntry($entryName, $password) |
316
|
|
|
{ |
317
|
2 |
|
if (!isset($this->sourceContainer->entries[$entryName])) { |
318
|
|
|
throw new ZipEntryNotFoundException($entryName); |
319
|
|
|
} |
320
|
|
|
|
321
|
2 |
|
if ($this->sourceContainer->entries[$entryName]->isEncrypted()) { |
322
|
2 |
|
$this->sourceContainer->entries[$entryName]->setPassword($password); |
323
|
|
|
} |
324
|
2 |
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* @return int|null |
328
|
|
|
*/ |
329
|
|
|
public function getZipAlign() |
330
|
|
|
{ |
331
|
|
|
return $this->zipAlign; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* @param int|null $zipAlign |
336
|
|
|
*/ |
337
|
4 |
|
public function setZipAlign($zipAlign) |
338
|
|
|
{ |
339
|
4 |
|
$this->zipAlign = $zipAlign === null ? null : (int) $zipAlign; |
340
|
4 |
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* @return bool |
344
|
|
|
*/ |
345
|
79 |
|
public function isZipAlign() |
346
|
|
|
{ |
347
|
79 |
|
return $this->zipAlign !== null; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* @param string|null $writePassword |
352
|
|
|
*/ |
353
|
11 |
|
public function setWritePassword($writePassword) |
354
|
|
|
{ |
355
|
11 |
|
$this->matcher()->all()->setPassword($writePassword); |
356
|
11 |
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Remove password. |
360
|
|
|
*/ |
361
|
2 |
|
public function removePassword() |
362
|
|
|
{ |
363
|
2 |
|
$this->matcher()->all()->setPassword(null); |
364
|
2 |
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* @param string|ZipEntry $entryName |
368
|
|
|
*/ |
369
|
1 |
|
public function removePasswordEntry($entryName) |
370
|
|
|
{ |
371
|
1 |
|
$this->matcher()->add($entryName)->setPassword(null); |
372
|
1 |
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* @param int $encryptionMethod |
376
|
|
|
*/ |
377
|
11 |
|
public function setEncryptionMethod($encryptionMethod = ZipEncryptionMethod::WINZIP_AES_256) |
378
|
|
|
{ |
379
|
11 |
|
$this->matcher()->all()->setEncryptionMethod($encryptionMethod); |
380
|
10 |
|
} |
381
|
|
|
} |
382
|
|
|
|