1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gaufrette\Adapter; |
4
|
|
|
|
5
|
|
|
use ZipArchive; |
6
|
|
|
use Gaufrette\Adapter; |
7
|
|
|
use Gaufrette\Util; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* ZIP Archive adapter. |
11
|
|
|
* |
12
|
|
|
* @author Boris Guéry <[email protected]> |
13
|
|
|
* @author Antoine Hérault <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class Zip implements Adapter |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string The zip archive full path |
19
|
|
|
*/ |
20
|
|
|
protected $zipFile; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var ZipArchive |
24
|
|
|
*/ |
25
|
|
|
protected $zipArchive; |
26
|
|
|
|
27
|
|
|
public function __construct($zipFile) |
28
|
|
|
{ |
29
|
|
|
if (!extension_loaded('zip')) { |
30
|
|
|
throw new \RuntimeException(sprintf( |
31
|
|
|
'Unable to use %s as the ZIP extension is not available.', |
32
|
|
|
__CLASS__ |
33
|
|
|
)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$this->zipFile = $zipFile; |
37
|
|
|
$this->reinitZipArchive(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function read($key) |
44
|
|
|
{ |
45
|
|
|
if (false === ($content = $this->zipArchive->getFromName($key, 0))) { |
46
|
|
|
return false; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $content; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function write($key, $content) |
56
|
|
|
{ |
57
|
|
|
if (!$this->zipArchive->addFromString($key, $content)) { |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (!$this->save()) { |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return Util\Size::fromContent($content); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
public function exists($key) |
72
|
|
|
{ |
73
|
|
|
return (boolean) $this->getStat($key); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function keys() |
80
|
|
|
{ |
81
|
|
|
$keys = []; |
82
|
|
|
|
83
|
|
|
for ($i = 0; $i < $this->zipArchive->numFiles; ++$i) { |
84
|
|
|
$keys[$i] = $this->zipArchive->getNameIndex($i); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $keys; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @todo implement |
92
|
|
|
* |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
public function isDirectory($key) |
96
|
|
|
{ |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
public function mtime($key) |
104
|
|
|
{ |
105
|
|
|
$stat = $this->getStat($key); |
106
|
|
|
|
107
|
|
|
return $stat['mtime'] ?? false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
|
|
public function delete($key) |
114
|
|
|
{ |
115
|
|
|
if (!$this->zipArchive->deleteName($key)) { |
116
|
|
|
return false; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this->save(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
|
public function rename($sourceKey, $targetKey) |
126
|
|
|
{ |
127
|
|
|
if (!$this->zipArchive->renameName($sourceKey, $targetKey)) { |
128
|
|
|
return false; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $this->save(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns the stat of a file in the zip archive |
136
|
|
|
* (name, index, crc, mtime, compression size, compression method, filesize). |
137
|
|
|
* |
138
|
|
|
* @param $key |
139
|
|
|
* |
140
|
|
|
* @return array|bool |
141
|
|
|
*/ |
142
|
|
|
public function getStat($key) |
143
|
|
|
{ |
144
|
|
|
$stat = $this->zipArchive->statName($key); |
145
|
|
|
if (false === $stat) { |
146
|
|
|
return []; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $stat; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function __destruct() |
153
|
|
|
{ |
154
|
|
|
if ($this->zipArchive) { |
155
|
|
|
try { |
156
|
|
|
$this->zipArchive->close(); |
157
|
|
|
} catch (\Exception $e) { |
|
|
|
|
158
|
|
|
} |
159
|
|
|
unset($this->zipArchive); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
protected function reinitZipArchive() |
164
|
|
|
{ |
165
|
|
|
$this->zipArchive = new ZipArchive(); |
166
|
|
|
|
167
|
|
|
if (true !== ($resultCode = $this->zipArchive->open($this->zipFile, ZipArchive::CREATE))) { |
168
|
|
|
switch ($resultCode) { |
169
|
|
|
case ZipArchive::ER_EXISTS: |
170
|
|
|
$errMsg = 'File already exists.'; |
171
|
|
|
|
172
|
|
|
break; |
173
|
|
|
case ZipArchive::ER_INCONS: |
174
|
|
|
$errMsg = 'Zip archive inconsistent.'; |
175
|
|
|
|
176
|
|
|
break; |
177
|
|
|
case ZipArchive::ER_INVAL: |
178
|
|
|
$errMsg = 'Invalid argument.'; |
179
|
|
|
|
180
|
|
|
break; |
181
|
|
|
case ZipArchive::ER_MEMORY: |
182
|
|
|
$errMsg = 'Malloc failure.'; |
183
|
|
|
|
184
|
|
|
break; |
185
|
|
|
case ZipArchive::ER_NOENT: |
186
|
|
|
$errMsg = 'Invalid argument.'; |
187
|
|
|
|
188
|
|
|
break; |
189
|
|
|
case ZipArchive::ER_NOZIP: |
190
|
|
|
$errMsg = 'Not a zip archive.'; |
191
|
|
|
|
192
|
|
|
break; |
193
|
|
|
case ZipArchive::ER_OPEN: |
194
|
|
|
$errMsg = 'Can\'t open file.'; |
195
|
|
|
|
196
|
|
|
break; |
197
|
|
|
case ZipArchive::ER_READ: |
198
|
|
|
$errMsg = 'Read error.'; |
199
|
|
|
|
200
|
|
|
break; |
201
|
|
|
case ZipArchive::ER_SEEK: |
202
|
|
|
$errMsg = 'Seek error.'; |
203
|
|
|
|
204
|
|
|
break; |
205
|
|
|
default: |
206
|
|
|
$errMsg = 'Unknown error.'; |
207
|
|
|
|
208
|
|
|
break; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
throw new \RuntimeException(sprintf('%s', $errMsg)); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Saves archive modifications and updates current ZipArchive instance. |
219
|
|
|
* |
220
|
|
|
* @throws \RuntimeException If file could not be saved |
221
|
|
|
*/ |
222
|
|
|
protected function save() |
223
|
|
|
{ |
224
|
|
|
// Close to save modification |
225
|
|
|
if (!$this->zipArchive->close()) { |
226
|
|
|
return false; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
// Re-initialize to get updated version |
230
|
|
|
$this->reinitZipArchive(); |
231
|
|
|
|
232
|
|
|
return true; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|