Return data from the "End of Central Directory" record, or None. The data is a list of the nine items in the ZIP "End of central dir" record followed by a tenth item, the file seek offset of this record.
(fpin)
| 283 | |
| 284 | |
| 285 | def _EndRecData(fpin): |
| 286 | """Return data from the "End of Central Directory" record, or None. |
| 287 | |
| 288 | The data is a list of the nine items in the ZIP "End of central dir" |
| 289 | record followed by a tenth item, the file seek offset of this record.""" |
| 290 | |
| 291 | # Determine file size |
| 292 | fpin.seek(0, 2) |
| 293 | filesize = fpin.tell() |
| 294 | |
| 295 | # Check to see if this is ZIP file with no archive comment (the |
| 296 | # "end of central directory" structure should be the last item in the |
| 297 | # file if this is the case). |
| 298 | try: |
| 299 | fpin.seek(-sizeEndCentDir, 2) |
| 300 | except OSError: |
| 301 | return None |
| 302 | data = fpin.read() |
| 303 | if (len(data) == sizeEndCentDir and |
| 304 | data[0:4] == stringEndArchive and |
| 305 | data[-2:] == b"\000\000"): |
| 306 | # the signature is correct and there's no comment, unpack structure |
| 307 | endrec = struct.unpack(structEndArchive, data) |
| 308 | endrec=list(endrec) |
| 309 | |
| 310 | # Append a blank comment and record start offset |
| 311 | endrec.append(b"") |
| 312 | endrec.append(filesize - sizeEndCentDir) |
| 313 | |
| 314 | # Try to read the "Zip64 end of central directory" structure |
| 315 | return _EndRecData64(fpin, -sizeEndCentDir, endrec) |
| 316 | |
| 317 | # Either this is not a ZIP file, or it is a ZIP file with an archive |
| 318 | # comment. Search the end of the file for the "end of central directory" |
| 319 | # record signature. The comment is the last item in the ZIP file and may be |
| 320 | # up to 64K long. It is assumed that the "end of central directory" magic |
| 321 | # number does not appear in the comment. |
| 322 | maxCommentStart = max(filesize - (1 << 16) - sizeEndCentDir, 0) |
| 323 | fpin.seek(maxCommentStart, 0) |
| 324 | data = fpin.read() |
| 325 | start = data.rfind(stringEndArchive) |
| 326 | if start >= 0: |
| 327 | # found the magic number; attempt to unpack and interpret |
| 328 | recData = data[start:start+sizeEndCentDir] |
| 329 | if len(recData) != sizeEndCentDir: |
| 330 | # Zip file is corrupted. |
| 331 | return None |
| 332 | endrec = list(struct.unpack(structEndArchive, recData)) |
| 333 | commentSize = endrec[_ECD_COMMENT_SIZE] #as claimed by the zip file |
| 334 | comment = data[start+sizeEndCentDir:start+sizeEndCentDir+commentSize] |
| 335 | endrec.append(comment) |
| 336 | endrec.append(maxCommentStart + start) |
| 337 | |
| 338 | # Try to read the "Zip64 end of central directory" structure |
| 339 | return _EndRecData64(fpin, maxCommentStart + start - filesize, |
| 340 | endrec) |
| 341 | |
| 342 | # Unable to find a valid end of central directory structure |