Total Complexity | 82 |
Total Lines | 685 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like ZipInputStream often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ZipInputStream, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class ZipInputStream implements ZipInputStreamInterface |
||
32 | { |
||
33 | /** @var resource */ |
||
34 | protected $in; |
||
35 | |||
36 | /** @var ZipModel */ |
||
37 | protected $zipModel; |
||
38 | |||
39 | /** |
||
40 | * ZipInputStream constructor. |
||
41 | * |
||
42 | * @param resource $in |
||
43 | */ |
||
44 | public function __construct($in) |
||
45 | { |
||
46 | if (!\is_resource($in)) { |
||
47 | throw new RuntimeException('$in must be resource'); |
||
48 | } |
||
49 | $this->in = $in; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @throws ZipException |
||
54 | * |
||
55 | * @return ZipModel |
||
56 | */ |
||
57 | public function readZip() |
||
58 | { |
||
59 | $this->checkZipFileSignature(); |
||
60 | $endOfCentralDirectory = $this->readEndOfCentralDirectory(); |
||
61 | $entries = $this->mountCentralDirectory($endOfCentralDirectory); |
||
62 | $this->zipModel = ZipModel::newSourceModel($entries, $endOfCentralDirectory); |
||
63 | |||
64 | return $this->zipModel; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Check zip file signature. |
||
69 | * |
||
70 | * @throws ZipException if this not .ZIP file. |
||
71 | */ |
||
72 | protected function checkZipFileSignature() |
||
73 | { |
||
74 | rewind($this->in); |
||
75 | // Constraint: A ZIP file must start with a Local File Header |
||
76 | // or a (ZIP64) End Of Central Directory Record if it's empty. |
||
77 | $signatureBytes = fread($this->in, 4); |
||
78 | |||
79 | if (\strlen($signatureBytes) < 4) { |
||
80 | throw new ZipException('Invalid zip file.'); |
||
81 | } |
||
82 | $signature = unpack('V', $signatureBytes)[1]; |
||
83 | |||
84 | if ( |
||
85 | $signature !== ZipEntry::LOCAL_FILE_HEADER_SIG |
||
86 | && $signature !== EndOfCentralDirectory::ZIP64_END_OF_CD_RECORD_SIG |
||
87 | && $signature !== EndOfCentralDirectory::END_OF_CD_SIG |
||
88 | ) { |
||
89 | throw new ZipException( |
||
90 | 'Expected Local File Header or (ZIP64) End Of Central Directory Record! Signature: ' . $signature |
||
91 | ); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @throws ZipException |
||
97 | * |
||
98 | * @return EndOfCentralDirectory |
||
99 | */ |
||
100 | protected function readEndOfCentralDirectory() |
||
101 | { |
||
102 | if (!$this->findEndOfCentralDirectory()) { |
||
103 | throw new ZipException('Invalid zip file. The end of the central directory could not be found.'); |
||
104 | } |
||
105 | |||
106 | $positionECD = ftell($this->in) - 4; |
||
107 | $buffer = fread($this->in, fstat($this->in)['size'] - $positionECD); |
||
108 | |||
109 | $unpack = unpack( |
||
110 | 'vdiskNo/vcdDiskNo/vcdEntriesDisk/' . |
||
111 | 'vcdEntries/VcdSize/VcdPos/vcommentLength', |
||
112 | substr($buffer, 0, 18) |
||
113 | ); |
||
114 | |||
115 | if ( |
||
116 | $unpack['diskNo'] !== 0 || |
||
117 | $unpack['cdDiskNo'] !== 0 || |
||
118 | $unpack['cdEntriesDisk'] !== $unpack['cdEntries'] |
||
119 | ) { |
||
120 | throw new ZipException( |
||
121 | 'ZIP file spanning/splitting is not supported!' |
||
122 | ); |
||
123 | } |
||
124 | // .ZIP file comment (variable sizeECD) |
||
125 | $comment = null; |
||
126 | |||
127 | if ($unpack['commentLength'] > 0) { |
||
128 | $comment = substr($buffer, 18, $unpack['commentLength']); |
||
129 | } |
||
130 | |||
131 | // Check for ZIP64 End Of Central Directory Locator exists. |
||
132 | $zip64ECDLocatorPosition = $positionECD - EndOfCentralDirectory::ZIP64_END_OF_CD_LOCATOR_LEN; |
||
133 | fseek($this->in, $zip64ECDLocatorPosition); |
||
134 | // zip64 end of central dir locator |
||
135 | // signature 4 bytes (0x07064b50) |
||
136 | if ($zip64ECDLocatorPosition > 0 && unpack( |
||
137 | 'V', |
||
138 | fread($this->in, 4) |
||
139 | )[1] === EndOfCentralDirectory::ZIP64_END_OF_CD_LOCATOR_SIG) { |
||
140 | $positionECD = $this->findZip64ECDPosition(); |
||
141 | $endCentralDirectory = $this->readZip64EndOfCentralDirectory($positionECD); |
||
142 | $endCentralDirectory->setComment($comment); |
||
143 | } else { |
||
144 | $endCentralDirectory = new EndOfCentralDirectory( |
||
145 | $unpack['cdEntries'], |
||
146 | $unpack['cdPos'], |
||
147 | $unpack['cdSize'], |
||
148 | false, |
||
149 | $comment |
||
150 | ); |
||
151 | } |
||
152 | |||
153 | return $endCentralDirectory; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @throws ZipException |
||
158 | * |
||
159 | * @return bool |
||
160 | */ |
||
161 | protected function findEndOfCentralDirectory() |
||
162 | { |
||
163 | $max = fstat($this->in)['size'] - EndOfCentralDirectory::END_OF_CENTRAL_DIRECTORY_RECORD_MIN_LEN; |
||
164 | |||
165 | if ($max < 0) { |
||
166 | throw new ZipException('Too short to be a zip file'); |
||
167 | } |
||
168 | $min = $max >= 0xffff ? $max - 0xffff : 0; |
||
169 | // Search for End of central directory record. |
||
170 | for ($position = $max; $position >= $min; $position--) { |
||
171 | fseek($this->in, $position); |
||
172 | // end of central dir signature 4 bytes (0x06054b50) |
||
173 | if (unpack('V', fread($this->in, 4))[1] !== EndOfCentralDirectory::END_OF_CD_SIG) { |
||
174 | continue; |
||
175 | } |
||
176 | |||
177 | return true; |
||
178 | } |
||
179 | |||
180 | return false; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Read Zip64 end of central directory locator and returns |
||
185 | * Zip64 end of central directory position. |
||
186 | * |
||
187 | * number of the disk with the |
||
188 | * start of the zip64 end of |
||
189 | * central directory 4 bytes |
||
190 | * relative offset of the zip64 |
||
191 | * end of central directory record 8 bytes |
||
192 | * total number of disks 4 bytes |
||
193 | * |
||
194 | * @throws ZipException |
||
195 | * |
||
196 | * @return int Zip64 End Of Central Directory position |
||
197 | */ |
||
198 | protected function findZip64ECDPosition() |
||
199 | { |
||
200 | $diskNo = unpack('V', fread($this->in, 4))[1]; |
||
201 | $zip64ECDPos = PackUtil::unpackLongLE(fread($this->in, 8)); |
||
202 | $totalDisks = unpack('V', fread($this->in, 4))[1]; |
||
203 | |||
204 | if ($diskNo !== 0 || $totalDisks > 1) { |
||
205 | throw new ZipException('ZIP file spanning/splitting is not supported!'); |
||
206 | } |
||
207 | |||
208 | return $zip64ECDPos; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Read zip64 end of central directory locator and zip64 end |
||
213 | * of central directory record. |
||
214 | * |
||
215 | * zip64 end of central dir |
||
216 | * signature 4 bytes (0x06064b50) |
||
217 | * size of zip64 end of central |
||
218 | * directory record 8 bytes |
||
219 | * version made by 2 bytes |
||
220 | * version needed to extract 2 bytes |
||
221 | * number of this disk 4 bytes |
||
222 | * number of the disk with the |
||
223 | * start of the central directory 4 bytes |
||
224 | * total number of entries in the |
||
225 | * central directory on this disk 8 bytes |
||
226 | * total number of entries in the |
||
227 | * central directory 8 bytes |
||
228 | * size of the central directory 8 bytes |
||
229 | * offset of start of central |
||
230 | * directory with respect to |
||
231 | * the starting disk number 8 bytes |
||
232 | * zip64 extensible data sector (variable size) |
||
233 | * |
||
234 | * @param int $zip64ECDPosition |
||
235 | * |
||
236 | * @throws ZipException |
||
237 | * |
||
238 | * @return EndOfCentralDirectory |
||
239 | */ |
||
240 | protected function readZip64EndOfCentralDirectory($zip64ECDPosition) |
||
241 | { |
||
242 | fseek($this->in, $zip64ECDPosition); |
||
243 | |||
244 | $buffer = fread($this->in, 56 /* zip64 end of cd rec length */); |
||
245 | |||
246 | if (unpack('V', $buffer)[1] !== EndOfCentralDirectory::ZIP64_END_OF_CD_RECORD_SIG) { |
||
247 | throw new ZipException('Expected ZIP64 End Of Central Directory Record!'); |
||
248 | } |
||
249 | |||
250 | $data = unpack( |
||
251 | 'VdiskNo/VcdDiskNo', |
||
252 | substr($buffer, 16) |
||
253 | ); |
||
254 | $cdEntriesDisk = PackUtil::unpackLongLE(substr($buffer, 24, 8)); |
||
255 | $entryCount = PackUtil::unpackLongLE(substr($buffer, 32, 8)); |
||
256 | $cdSize = PackUtil::unpackLongLE(substr($buffer, 40, 8)); |
||
257 | $cdPos = PackUtil::unpackLongLE(substr($buffer, 48, 8)); |
||
258 | |||
259 | if ($data['diskNo'] !== 0 || $data['cdDiskNo'] !== 0 || $entryCount !== $cdEntriesDisk) { |
||
260 | throw new ZipException('ZIP file spanning/splitting is not supported!'); |
||
261 | } |
||
262 | |||
263 | if ($entryCount < 0 || $entryCount > 0x7fffffff) { |
||
264 | throw new ZipException('Total Number Of Entries In The Central Directory out of range!'); |
||
265 | } |
||
266 | |||
267 | // skip zip64 extensible data sector (variable sizeEndCD) |
||
268 | |||
269 | return new EndOfCentralDirectory( |
||
270 | $entryCount, |
||
271 | $cdPos, |
||
272 | $cdSize, |
||
273 | true |
||
274 | ); |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Reads the central directory from the given seekable byte channel |
||
279 | * and populates the internal tables with ZipEntry instances. |
||
280 | * |
||
281 | * The ZipEntry's will know all data that can be obtained from the |
||
282 | * central directory alone, but not the data that requires the local |
||
283 | * file header or additional data to be read. |
||
284 | * |
||
285 | * @param EndOfCentralDirectory $endOfCentralDirectory |
||
286 | * |
||
287 | * @throws ZipException |
||
288 | * |
||
289 | * @return ZipEntry[] |
||
290 | */ |
||
291 | protected function mountCentralDirectory(EndOfCentralDirectory $endOfCentralDirectory) |
||
292 | { |
||
293 | $entries = []; |
||
294 | |||
295 | fseek($this->in, $endOfCentralDirectory->getCdOffset()); |
||
296 | |||
297 | if (!($cdStream = fopen('php://temp', 'w+b'))) { |
||
298 | throw new ZipException('Temp resource can not open from write'); |
||
299 | } |
||
300 | stream_copy_to_stream($this->in, $cdStream, $endOfCentralDirectory->getCdSize()); |
||
301 | rewind($cdStream); |
||
302 | for ($numEntries = $endOfCentralDirectory->getEntryCount(); $numEntries > 0; $numEntries--) { |
||
303 | $entry = $this->readCentralDirectoryEntry($cdStream); |
||
304 | $entries[$entry->getName()] = $entry; |
||
305 | } |
||
306 | fclose($cdStream); |
||
307 | |||
308 | return $entries; |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * Read central directory entry. |
||
313 | * |
||
314 | * central file header signature 4 bytes (0x02014b50) |
||
315 | * version made by 2 bytes |
||
316 | * version needed to extract 2 bytes |
||
317 | * general purpose bit flag 2 bytes |
||
318 | * compression method 2 bytes |
||
319 | * last mod file time 2 bytes |
||
320 | * last mod file date 2 bytes |
||
321 | * crc-32 4 bytes |
||
322 | * compressed size 4 bytes |
||
323 | * uncompressed size 4 bytes |
||
324 | * file name length 2 bytes |
||
325 | * extra field length 2 bytes |
||
326 | * file comment length 2 bytes |
||
327 | * disk number start 2 bytes |
||
328 | * internal file attributes 2 bytes |
||
329 | * external file attributes 4 bytes |
||
330 | * relative offset of local header 4 bytes |
||
331 | * |
||
332 | * file name (variable size) |
||
333 | * extra field (variable size) |
||
334 | * file comment (variable size) |
||
335 | * |
||
336 | * @param resource $stream |
||
337 | * |
||
338 | * @throws ZipException |
||
339 | * |
||
340 | * @return ZipEntry |
||
341 | */ |
||
342 | public function readCentralDirectoryEntry($stream) |
||
343 | { |
||
344 | if (unpack('V', fread($stream, 4))[1] !== ZipOutputStreamInterface::CENTRAL_FILE_HEADER_SIG) { |
||
345 | throw new ZipException('Corrupt zip file. Cannot read central dir entry.'); |
||
346 | } |
||
347 | |||
348 | $data = unpack( |
||
349 | 'vversionMadeBy/vversionNeededToExtract/' . |
||
350 | 'vgeneralPurposeBitFlag/vcompressionMethod/' . |
||
351 | 'VlastModFile/Vcrc/VcompressedSize/' . |
||
352 | 'VuncompressedSize/vfileNameLength/vextraFieldLength/' . |
||
353 | 'vfileCommentLength/vdiskNumberStart/vinternalFileAttributes/' . |
||
354 | 'VexternalFileAttributes/VoffsetLocalHeader', |
||
355 | fread($stream, 42) |
||
356 | ); |
||
357 | |||
358 | $createdOS = ($data['versionMadeBy'] & 0xFF00) >> 8; |
||
359 | $softwareVersion = $data['versionMadeBy'] & 0x00FF; |
||
360 | |||
361 | $extractOS = ($data['versionNeededToExtract'] & 0xFF00) >> 8; |
||
362 | $extractVersion = $data['versionNeededToExtract'] & 0x00FF; |
||
363 | |||
364 | $name = fread($stream, $data['fileNameLength']); |
||
365 | |||
366 | $extra = ''; |
||
367 | |||
368 | if ($data['extraFieldLength'] > 0) { |
||
369 | $extra = fread($stream, $data['extraFieldLength']); |
||
370 | } |
||
371 | |||
372 | $comment = null; |
||
373 | |||
374 | if ($data['fileCommentLength'] > 0) { |
||
375 | $comment = fread($stream, $data['fileCommentLength']); |
||
376 | } |
||
377 | |||
378 | $entry = new ZipSourceEntry($this); |
||
379 | $entry->setName($name); |
||
380 | $entry->setCreatedOS($createdOS); |
||
381 | $entry->setSoftwareVersion($softwareVersion); |
||
382 | $entry->setVersionNeededToExtract($extractVersion); |
||
383 | $entry->setExtractedOS($extractOS); |
||
384 | $entry->setMethod($data['compressionMethod']); |
||
385 | $entry->setGeneralPurposeBitFlags($data['generalPurposeBitFlag']); |
||
386 | $entry->setDosTime($data['lastModFile']); |
||
387 | $entry->setCrc($data['crc']); |
||
388 | $entry->setCompressedSize($data['compressedSize']); |
||
389 | $entry->setSize($data['uncompressedSize']); |
||
390 | $entry->setInternalAttributes($data['internalFileAttributes']); |
||
391 | $entry->setExternalAttributes($data['externalFileAttributes']); |
||
392 | $entry->setOffset($data['offsetLocalHeader']); |
||
393 | $entry->setComment($comment); |
||
394 | $entry->setExtra($extra); |
||
395 | |||
396 | return $entry; |
||
397 | } |
||
398 | |||
399 | /** |
||
400 | * @param ZipEntry $entry |
||
401 | * |
||
402 | * @throws ZipException |
||
403 | * |
||
404 | * @return string |
||
405 | */ |
||
406 | public function readEntryContent(ZipEntry $entry) |
||
572 | } |
||
573 | |||
574 | /** |
||
575 | * @return resource |
||
576 | */ |
||
577 | public function getStream() |
||
578 | { |
||
579 | return $this->in; |
||
580 | } |
||
581 | |||
582 | /** |
||
583 | * Copy the input stream of the LOC entry zip and the data into |
||
584 | * the output stream and zip the alignment if necessary. |
||
585 | * |
||
586 | * @param ZipEntry $entry |
||
587 | * @param ZipOutputStreamInterface $out |
||
588 | * |
||
589 | * @throws ZipException |
||
590 | */ |
||
591 | public function copyEntry(ZipEntry $entry, ZipOutputStreamInterface $out) |
||
687 | } |
||
688 | |||
689 | /** |
||
690 | * @param ZipEntry $entry |
||
691 | * @param ZipOutputStreamInterface $out |
||
692 | */ |
||
693 | public function copyEntryData(ZipEntry $entry, ZipOutputStreamInterface $out) |
||
694 | { |
||
695 | $offset = $entry->getOffset(); |
||
696 | $nameLength = \strlen($entry->getName()); |
||
697 | |||
698 | fseek($this->in, $offset + ZipEntry::LOCAL_FILE_HEADER_MIN_LEN - 2, \SEEK_SET); |
||
699 | $extraLength = unpack('v', fread($this->in, 2))[1]; |
||
700 | |||
701 | fseek($this->in, $offset + ZipEntry::LOCAL_FILE_HEADER_MIN_LEN + $nameLength + $extraLength, \SEEK_SET); |
||
702 | // copy raw data from input stream to output stream |
||
703 | stream_copy_to_stream($this->in, $out->getStream(), $entry->getCompressedSize()); |
||
704 | } |
||
705 | |||
706 | public function __destruct() |
||
709 | } |
||
710 | |||
711 | public function close() |
||
716 | } |
||
717 | } |
||
718 | } |
||
719 |