(archive, toc_entry)
| 630 | |
| 631 | # Given a path to a Zip file and a toc_entry, return the (uncompressed) data. |
| 632 | def _get_data(archive, toc_entry): |
| 633 | datapath, compress, data_size, file_size, file_offset, time, date, crc = toc_entry |
| 634 | if data_size < 0: |
| 635 | raise ZipImportError('negative data size') |
| 636 | |
| 637 | with _io.open_code(archive) as fp: |
| 638 | # Check to make sure the local file header is correct |
| 639 | try: |
| 640 | fp.seek(file_offset) |
| 641 | except OSError: |
| 642 | raise ZipImportError(f"can't read Zip file: {archive!r}", path=archive) |
| 643 | buffer = fp.read(30) |
| 644 | if len(buffer) != 30: |
| 645 | raise EOFError('EOF read where not expected') |
| 646 | |
| 647 | if buffer[:4] != b'PK\x03\x04': |
| 648 | # Bad: Local File Header |
| 649 | raise ZipImportError(f'bad local file header: {archive!r}', path=archive) |
| 650 | |
| 651 | name_size = _unpack_uint16(buffer[26:28]) |
| 652 | extra_size = _unpack_uint16(buffer[28:30]) |
| 653 | header_size = 30 + name_size + extra_size |
| 654 | file_offset += header_size # Start of file data |
| 655 | try: |
| 656 | fp.seek(file_offset) |
| 657 | except OSError: |
| 658 | raise ZipImportError(f"can't read Zip file: {archive!r}", path=archive) |
| 659 | raw_data = fp.read(data_size) |
| 660 | if len(raw_data) != data_size: |
| 661 | raise OSError("zipimport: can't read data") |
| 662 | |
| 663 | match compress: |
| 664 | case 0: # stored |
| 665 | return raw_data |
| 666 | case 8: # deflate aka zlib |
| 667 | try: |
| 668 | decompress = _get_zlib_decompress_func() |
| 669 | except Exception: |
| 670 | raise ZipImportError("can't decompress data; zlib not available") |
| 671 | return decompress(raw_data, -15) |
| 672 | case 93: # zstd |
| 673 | try: |
| 674 | return _zstd_decompress(raw_data) |
| 675 | except Exception: |
| 676 | raise ZipImportError("could not decompress zstd data") |
| 677 | # bz2 and lzma could be added, but are largely obsolete. |
| 678 | case _: |
| 679 | raise ZipImportError(f"zipimport: unsupported compression {compress}") |
| 680 | |
| 681 | |
| 682 | # Lenient date/time comparison function. The precision of the mtime |
no test coverage detected
searching dependent graphs…