annotate zipfile32.py @ 123:90c002c960cb

Fixed CPU time display on UNIX Previously, the total CPU time spent by the testee on all test cases up to and including the current one was displayed.
author Oleg Oshmyan <chortos@inbox.lv>
date Sun, 24 Apr 2011 19:28:40 +0100
parents bbf9c434fa57
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
97
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1 """
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
2 Read and write ZIP files.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
3
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
4 XXX references to utf-8 need further investigation.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
5 """
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
6 import io
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
7 import os
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
8 import re
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
9 import imp
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
10 import sys
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
11 import time
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
12 import stat
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
13 import shutil
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
14 import struct
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
15 import binascii
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
16
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
17
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
18 try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
19 import zlib # We may need its compression method
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
20 crc32 = zlib.crc32
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
21 except ImportError:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
22 zlib = None
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
23 crc32 = binascii.crc32
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
24
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
25 try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
26 import bz2 # We may need its compression method
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
27 except ImportError:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
28 bz2 = None
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
29
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
30 __all__ = ["BadZipFile", "BadZipfile", "error", "ZIP_STORED", "ZIP_DEFLATED",
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
31 "is_zipfile", "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile",
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
32 "ZIP_BZIP2"]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
33
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
34 class BadZipFile(Exception):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
35 pass
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
36
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
37
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
38 class LargeZipFile(Exception):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
39 """
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
40 Raised when writing a zipfile, the zipfile requires ZIP64 extensions
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
41 and those extensions are disabled.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
42 """
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
43
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
44 error = BadZipfile = BadZipFile # Pre-3.2 compatibility names
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
45
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
46
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
47 ZIP64_LIMIT = (1 << 31) - 1
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
48 ZIP_FILECOUNT_LIMIT = 1 << 16
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
49 ZIP_MAX_COMMENT = (1 << 16) - 1
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
50
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
51 # constants for Zip file compression methods
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
52 ZIP_STORED = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
53 ZIP_DEFLATED = 8
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
54 ZIP_BZIP2 = 12
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
55 # Other ZIP compression methods not supported
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
56
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
57 # Below are some formats and associated data for reading/writing headers using
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
58 # the struct module. The names and structures of headers/records are those used
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
59 # in the PKWARE description of the ZIP file format:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
60 # http://www.pkware.com/documents/casestudies/APPNOTE.TXT
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
61 # (URL valid as of January 2008)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
62
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
63 # The "end of central directory" structure, magic number, size, and indices
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
64 # (section V.I in the format document)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
65 structEndArchive = b"<4s4H2LH"
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
66 stringEndArchive = b"PK\005\006"
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
67 sizeEndCentDir = struct.calcsize(structEndArchive)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
68
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
69 _ECD_SIGNATURE = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
70 _ECD_DISK_NUMBER = 1
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
71 _ECD_DISK_START = 2
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
72 _ECD_ENTRIES_THIS_DISK = 3
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
73 _ECD_ENTRIES_TOTAL = 4
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
74 _ECD_SIZE = 5
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
75 _ECD_OFFSET = 6
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
76 _ECD_COMMENT_SIZE = 7
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
77 # These last two indices are not part of the structure as defined in the
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
78 # spec, but they are used internally by this module as a convenience
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
79 _ECD_COMMENT = 8
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
80 _ECD_LOCATION = 9
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
81
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
82 # The "central directory" structure, magic number, size, and indices
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
83 # of entries in the structure (section V.F in the format document)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
84 structCentralDir = "<4s4B4HL2L5H2L"
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
85 stringCentralDir = b"PK\001\002"
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
86 sizeCentralDir = struct.calcsize(structCentralDir)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
87
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
88 # indexes of entries in the central directory structure
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
89 _CD_SIGNATURE = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
90 _CD_CREATE_VERSION = 1
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
91 _CD_CREATE_SYSTEM = 2
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
92 _CD_EXTRACT_VERSION = 3
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
93 _CD_EXTRACT_SYSTEM = 4
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
94 _CD_FLAG_BITS = 5
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
95 _CD_COMPRESS_TYPE = 6
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
96 _CD_TIME = 7
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
97 _CD_DATE = 8
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
98 _CD_CRC = 9
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
99 _CD_COMPRESSED_SIZE = 10
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
100 _CD_UNCOMPRESSED_SIZE = 11
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
101 _CD_FILENAME_LENGTH = 12
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
102 _CD_EXTRA_FIELD_LENGTH = 13
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
103 _CD_COMMENT_LENGTH = 14
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
104 _CD_DISK_NUMBER_START = 15
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
105 _CD_INTERNAL_FILE_ATTRIBUTES = 16
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
106 _CD_EXTERNAL_FILE_ATTRIBUTES = 17
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
107 _CD_LOCAL_HEADER_OFFSET = 18
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
108
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
109 # The "local file header" structure, magic number, size, and indices
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
110 # (section V.A in the format document)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
111 structFileHeader = "<4s2B4HL2L2H"
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
112 stringFileHeader = b"PK\003\004"
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
113 sizeFileHeader = struct.calcsize(structFileHeader)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
114
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
115 _FH_SIGNATURE = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
116 _FH_EXTRACT_VERSION = 1
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
117 _FH_EXTRACT_SYSTEM = 2
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
118 _FH_GENERAL_PURPOSE_FLAG_BITS = 3
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
119 _FH_COMPRESSION_METHOD = 4
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
120 _FH_LAST_MOD_TIME = 5
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
121 _FH_LAST_MOD_DATE = 6
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
122 _FH_CRC = 7
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
123 _FH_COMPRESSED_SIZE = 8
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
124 _FH_UNCOMPRESSED_SIZE = 9
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
125 _FH_FILENAME_LENGTH = 10
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
126 _FH_EXTRA_FIELD_LENGTH = 11
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
127
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
128 # The "Zip64 end of central directory locator" structure, magic number, and size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
129 structEndArchive64Locator = "<4sLQL"
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
130 stringEndArchive64Locator = b"PK\x06\x07"
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
131 sizeEndCentDir64Locator = struct.calcsize(structEndArchive64Locator)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
132
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
133 # The "Zip64 end of central directory" record, magic number, size, and indices
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
134 # (section V.G in the format document)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
135 structEndArchive64 = "<4sQ2H2L4Q"
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
136 stringEndArchive64 = b"PK\x06\x06"
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
137 sizeEndCentDir64 = struct.calcsize(structEndArchive64)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
138
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
139 _CD64_SIGNATURE = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
140 _CD64_DIRECTORY_RECSIZE = 1
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
141 _CD64_CREATE_VERSION = 2
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
142 _CD64_EXTRACT_VERSION = 3
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
143 _CD64_DISK_NUMBER = 4
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
144 _CD64_DISK_NUMBER_START = 5
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
145 _CD64_NUMBER_ENTRIES_THIS_DISK = 6
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
146 _CD64_NUMBER_ENTRIES_TOTAL = 7
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
147 _CD64_DIRECTORY_SIZE = 8
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
148 _CD64_OFFSET_START_CENTDIR = 9
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
149
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
150 def _check_zipfile(fp):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
151 try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
152 if _EndRecData(fp):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
153 return True # file has correct magic number
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
154 except IOError:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
155 pass
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
156 return False
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
157
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
158 def is_zipfile(filename):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
159 """Quickly see if a file is a ZIP file by checking the magic number.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
160
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
161 The filename argument may be a file or file-like object too.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
162 """
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
163 result = False
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
164 try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
165 if hasattr(filename, "read"):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
166 result = _check_zipfile(fp=filename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
167 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
168 with open(filename, "rb") as fp:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
169 result = _check_zipfile(fp)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
170 except IOError:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
171 pass
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
172 return result
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
173
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
174 def _EndRecData64(fpin, offset, endrec):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
175 """
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
176 Read the ZIP64 end-of-archive records and use that to update endrec
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
177 """
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
178 try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
179 fpin.seek(offset - sizeEndCentDir64Locator, 2)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
180 except IOError:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
181 # If the seek fails, the file is not large enough to contain a ZIP64
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
182 # end-of-archive record, so just return the end record we were given.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
183 return endrec
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
184
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
185 data = fpin.read(sizeEndCentDir64Locator)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
186 sig, diskno, reloff, disks = struct.unpack(structEndArchive64Locator, data)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
187 if sig != stringEndArchive64Locator:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
188 return endrec
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
189
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
190 if diskno != 0 or disks != 1:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
191 raise BadZipFile("zipfiles that span multiple disks are not supported")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
192
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
193 # Assume no 'zip64 extensible data'
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
194 fpin.seek(offset - sizeEndCentDir64Locator - sizeEndCentDir64, 2)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
195 data = fpin.read(sizeEndCentDir64)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
196 sig, sz, create_version, read_version, disk_num, disk_dir, \
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
197 dircount, dircount2, dirsize, diroffset = \
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
198 struct.unpack(structEndArchive64, data)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
199 if sig != stringEndArchive64:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
200 return endrec
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
201
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
202 # Update the original endrec using data from the ZIP64 record
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
203 endrec[_ECD_SIGNATURE] = sig
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
204 endrec[_ECD_DISK_NUMBER] = disk_num
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
205 endrec[_ECD_DISK_START] = disk_dir
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
206 endrec[_ECD_ENTRIES_THIS_DISK] = dircount
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
207 endrec[_ECD_ENTRIES_TOTAL] = dircount2
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
208 endrec[_ECD_SIZE] = dirsize
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
209 endrec[_ECD_OFFSET] = diroffset
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
210 return endrec
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
211
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
212
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
213 def _EndRecData(fpin):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
214 """Return data from the "End of Central Directory" record, or None.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
215
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
216 The data is a list of the nine items in the ZIP "End of central dir"
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
217 record followed by a tenth item, the file seek offset of this record."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
218
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
219 # Determine file size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
220 fpin.seek(0, 2)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
221 filesize = fpin.tell()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
222
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
223 # Check to see if this is ZIP file with no archive comment (the
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
224 # "end of central directory" structure should be the last item in the
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
225 # file if this is the case).
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
226 try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
227 fpin.seek(-sizeEndCentDir, 2)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
228 except IOError:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
229 return None
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
230 data = fpin.read()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
231 if data[0:4] == stringEndArchive and data[-2:] == b"\000\000":
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
232 # the signature is correct and there's no comment, unpack structure
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
233 endrec = struct.unpack(structEndArchive, data)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
234 endrec=list(endrec)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
235
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
236 # Append a blank comment and record start offset
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
237 endrec.append(b"")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
238 endrec.append(filesize - sizeEndCentDir)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
239
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
240 # Try to read the "Zip64 end of central directory" structure
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
241 return _EndRecData64(fpin, -sizeEndCentDir, endrec)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
242
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
243 # Either this is not a ZIP file, or it is a ZIP file with an archive
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
244 # comment. Search the end of the file for the "end of central directory"
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
245 # record signature. The comment is the last item in the ZIP file and may be
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
246 # up to 64K long. It is assumed that the "end of central directory" magic
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
247 # number does not appear in the comment.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
248 maxCommentStart = max(filesize - (1 << 16) - sizeEndCentDir, 0)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
249 fpin.seek(maxCommentStart, 0)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
250 data = fpin.read()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
251 start = data.rfind(stringEndArchive)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
252 if start >= 0:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
253 # found the magic number; attempt to unpack and interpret
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
254 recData = data[start:start+sizeEndCentDir]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
255 endrec = list(struct.unpack(structEndArchive, recData))
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
256 comment = data[start+sizeEndCentDir:]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
257 # check that comment length is correct
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
258 if endrec[_ECD_COMMENT_SIZE] == len(comment):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
259 # Append the archive comment and start offset
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
260 endrec.append(comment)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
261 endrec.append(maxCommentStart + start)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
262
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
263 # Try to read the "Zip64 end of central directory" structure
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
264 return _EndRecData64(fpin, maxCommentStart + start - filesize,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
265 endrec)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
266
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
267 # Unable to find a valid end of central directory structure
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
268 return
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
269
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
270
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
271 class ZipInfo (object):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
272 """Class with attributes describing each file in the ZIP archive."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
273
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
274 __slots__ = (
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
275 'orig_filename',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
276 'filename',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
277 'date_time',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
278 'compress_type',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
279 'comment',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
280 'extra',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
281 'create_system',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
282 'create_version',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
283 'extract_version',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
284 'reserved',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
285 'flag_bits',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
286 'volume',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
287 'internal_attr',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
288 'external_attr',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
289 'header_offset',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
290 'CRC',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
291 'compress_size',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
292 'file_size',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
293 '_raw_time',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
294 )
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
295
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
296 def __init__(self, filename="NoName", date_time=(1980,1,1,0,0,0)):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
297 self.orig_filename = filename # Original file name in archive
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
298
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
299 # Terminate the file name at the first null byte. Null bytes in file
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
300 # names are used as tricks by viruses in archives.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
301 null_byte = filename.find(chr(0))
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
302 if null_byte >= 0:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
303 filename = filename[0:null_byte]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
304 # This is used to ensure paths in generated ZIP files always use
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
305 # forward slashes as the directory separator, as required by the
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
306 # ZIP format specification.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
307 if os.sep != "/" and os.sep in filename:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
308 filename = filename.replace(os.sep, "/")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
309
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
310 self.filename = filename # Normalized file name
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
311 self.date_time = date_time # year, month, day, hour, min, sec
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
312 # Standard values:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
313 self.compress_type = ZIP_STORED # Type of compression for the file
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
314 self.comment = b"" # Comment for each file
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
315 self.extra = b"" # ZIP extra data
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
316 if sys.platform == 'win32':
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
317 self.create_system = 0 # System which created ZIP archive
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
318 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
319 # Assume everything else is unix-y
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
320 self.create_system = 3 # System which created ZIP archive
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
321 self.create_version = 20 # Version which created ZIP archive
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
322 self.extract_version = 20 # Version needed to extract archive
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
323 self.reserved = 0 # Must be zero
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
324 self.flag_bits = 0 # ZIP flag bits
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
325 self.volume = 0 # Volume number of file header
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
326 self.internal_attr = 0 # Internal attributes
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
327 self.external_attr = 0 # External file attributes
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
328 # Other attributes are set by class ZipFile:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
329 # header_offset Byte offset to the file header
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
330 # CRC CRC-32 of the uncompressed file
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
331 # compress_size Size of the compressed file
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
332 # file_size Size of the uncompressed file
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
333
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
334 def FileHeader(self):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
335 """Return the per-file header as a string."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
336 dt = self.date_time
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
337 dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
338 dostime = dt[3] << 11 | dt[4] << 5 | (dt[5] // 2)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
339 if self.flag_bits & 0x08:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
340 # Set these to zero because we write them after the file data
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
341 CRC = compress_size = file_size = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
342 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
343 CRC = self.CRC
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
344 compress_size = self.compress_size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
345 file_size = self.file_size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
346
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
347 extra = self.extra
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
348
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
349 if file_size > ZIP64_LIMIT or compress_size > ZIP64_LIMIT:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
350 # File is larger than what fits into a 4 byte integer,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
351 # fall back to the ZIP64 extension
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
352 fmt = '<HHQQ'
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
353 extra = extra + struct.pack(fmt,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
354 1, struct.calcsize(fmt)-4, file_size, compress_size)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
355 file_size = 0xffffffff
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
356 compress_size = 0xffffffff
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
357 self.extract_version = max(45, self.extract_version)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
358 self.create_version = max(45, self.extract_version)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
359
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
360 filename, flag_bits = self._encodeFilenameFlags()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
361 header = struct.pack(structFileHeader, stringFileHeader,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
362 self.extract_version, self.reserved, flag_bits,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
363 self.compress_type, dostime, dosdate, CRC,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
364 compress_size, file_size,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
365 len(filename), len(extra))
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
366 return header + filename + extra
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
367
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
368 def _encodeFilenameFlags(self):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
369 try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
370 return self.filename.encode('ascii'), self.flag_bits
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
371 except UnicodeEncodeError:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
372 return self.filename.encode('utf-8'), self.flag_bits | 0x800
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
373
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
374 def _decodeExtra(self):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
375 # Try to decode the extra field.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
376 extra = self.extra
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
377 unpack = struct.unpack
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
378 while extra:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
379 tp, ln = unpack('<HH', extra[:4])
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
380 if tp == 1:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
381 if ln >= 24:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
382 counts = unpack('<QQQ', extra[4:28])
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
383 elif ln == 16:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
384 counts = unpack('<QQ', extra[4:20])
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
385 elif ln == 8:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
386 counts = unpack('<Q', extra[4:12])
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
387 elif ln == 0:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
388 counts = ()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
389 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
390 raise RuntimeError("Corrupt extra field %s"%(ln,))
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
391
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
392 idx = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
393
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
394 # ZIP64 extension (large files and/or large archives)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
395 if self.file_size in (0xffffffffffffffff, 0xffffffff):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
396 self.file_size = counts[idx]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
397 idx += 1
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
398
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
399 if self.compress_size == 0xFFFFFFFF:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
400 self.compress_size = counts[idx]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
401 idx += 1
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
402
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
403 if self.header_offset == 0xffffffff:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
404 old = self.header_offset
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
405 self.header_offset = counts[idx]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
406 idx+=1
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
407
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
408 extra = extra[ln+4:]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
409
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
410
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
411 class _ZipDecrypter:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
412 """Class to handle decryption of files stored within a ZIP archive.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
413
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
414 ZIP supports a password-based form of encryption. Even though known
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
415 plaintext attacks have been found against it, it is still useful
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
416 to be able to get data out of such a file.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
417
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
418 Usage:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
419 zd = _ZipDecrypter(mypwd)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
420 plain_char = zd(cypher_char)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
421 plain_text = map(zd, cypher_text)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
422 """
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
423
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
424 def _GenerateCRCTable():
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
425 """Generate a CRC-32 table.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
426
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
427 ZIP encryption uses the CRC32 one-byte primitive for scrambling some
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
428 internal keys. We noticed that a direct implementation is faster than
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
429 relying on binascii.crc32().
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
430 """
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
431 poly = 0xedb88320
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
432 table = [0] * 256
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
433 for i in range(256):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
434 crc = i
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
435 for j in range(8):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
436 if crc & 1:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
437 crc = ((crc >> 1) & 0x7FFFFFFF) ^ poly
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
438 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
439 crc = ((crc >> 1) & 0x7FFFFFFF)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
440 table[i] = crc
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
441 return table
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
442 crctable = _GenerateCRCTable()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
443
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
444 def _crc32(self, ch, crc):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
445 """Compute the CRC32 primitive on one byte."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
446 return ((crc >> 8) & 0xffffff) ^ self.crctable[(crc ^ ch) & 0xff]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
447
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
448 def __init__(self, pwd):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
449 self.key0 = 305419896
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
450 self.key1 = 591751049
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
451 self.key2 = 878082192
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
452 for p in pwd:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
453 self._UpdateKeys(p)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
454
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
455 def _UpdateKeys(self, c):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
456 self.key0 = self._crc32(c, self.key0)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
457 self.key1 = (self.key1 + (self.key0 & 255)) & 4294967295
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
458 self.key1 = (self.key1 * 134775813 + 1) & 4294967295
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
459 self.key2 = self._crc32((self.key1 >> 24) & 255, self.key2)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
460
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
461 def __call__(self, c):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
462 """Decrypt a single character."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
463 assert isinstance(c, int)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
464 k = self.key2 | 2
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
465 c = c ^ (((k * (k^1)) >> 8) & 255)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
466 self._UpdateKeys(c)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
467 return c
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
468
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
469 class ZipExtFile(io.BufferedIOBase):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
470 """File-like object for reading an archive member.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
471 Is returned by ZipFile.open().
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
472 """
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
473
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
474 # Max size supported by decompressor.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
475 MAX_N = 1 << 31 - 1
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
476
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
477 # Read from compressed files in 4k blocks.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
478 MIN_READ_SIZE = 4096
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
479
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
480 # Search for universal newlines or line chunks.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
481 PATTERN = re.compile(br'^(?P<chunk>[^\r\n]+)|(?P<newline>\n|\r\n?)')
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
482
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
483 def __init__(self, fileobj, mode, zipinfo, decrypter=None,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
484 close_fileobj=False):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
485 self._fileobj = fileobj
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
486 self._decrypter = decrypter
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
487 self._close_fileobj = close_fileobj
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
488
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
489 self._compress_type = zipinfo.compress_type
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
490 self._compress_size = zipinfo.compress_size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
491 self._compress_left = zipinfo.compress_size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
492
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
493 if self._compress_type == ZIP_DEFLATED:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
494 self._decompressor = zlib.decompressobj(-15)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
495 elif self._compress_type == ZIP_BZIP2:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
496 self._decompressor = bz2.BZ2Decompressor()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
497 self.MIN_READ_SIZE = 900000
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
498 self._unconsumed = b''
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
499
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
500 self._readbuffer = b''
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
501 self._offset = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
502
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
503 self._universal = 'U' in mode
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
504 self.newlines = None
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
505
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
506 # Adjust read size for encrypted files since the first 12 bytes
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
507 # are for the encryption/password information.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
508 if self._decrypter is not None:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
509 self._compress_left -= 12
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
510
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
511 self.mode = mode
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
512 self.name = zipinfo.filename
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
513
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
514 if hasattr(zipinfo, 'CRC'):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
515 self._expected_crc = zipinfo.CRC
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
516 self._running_crc = crc32(b'') & 0xffffffff
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
517 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
518 self._expected_crc = None
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
519
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
520 def readline(self, limit=-1):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
521 """Read and return a line from the stream.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
522
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
523 If limit is specified, at most limit bytes will be read.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
524 """
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
525
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
526 if not self._universal and limit < 0:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
527 # Shortcut common case - newline found in buffer.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
528 i = self._readbuffer.find(b'\n', self._offset) + 1
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
529 if i > 0:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
530 line = self._readbuffer[self._offset: i]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
531 self._offset = i
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
532 return line
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
533
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
534 if not self._universal:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
535 return io.BufferedIOBase.readline(self, limit)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
536
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
537 line = b''
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
538 while limit < 0 or len(line) < limit:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
539 readahead = self.peek(2)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
540 if readahead == b'':
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
541 return line
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
542
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
543 #
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
544 # Search for universal newlines or line chunks.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
545 #
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
546 # The pattern returns either a line chunk or a newline, but not
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
547 # both. Combined with peek(2), we are assured that the sequence
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
548 # '\r\n' is always retrieved completely and never split into
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
549 # separate newlines - '\r', '\n' due to coincidental readaheads.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
550 #
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
551 match = self.PATTERN.search(readahead)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
552 newline = match.group('newline')
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
553 if newline is not None:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
554 if self.newlines is None:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
555 self.newlines = []
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
556 if newline not in self.newlines:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
557 self.newlines.append(newline)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
558 self._offset += len(newline)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
559 return line + b'\n'
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
560
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
561 chunk = match.group('chunk')
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
562 if limit >= 0:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
563 chunk = chunk[: limit - len(line)]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
564
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
565 self._offset += len(chunk)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
566 line += chunk
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
567
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
568 return line
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
569
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
570 def peek(self, n=1):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
571 """Returns buffered bytes without advancing the position."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
572 if n > len(self._readbuffer) - self._offset:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
573 chunk = self.read(n)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
574 self._offset -= len(chunk)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
575
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
576 # Return up to 512 bytes to reduce allocation overhead for tight loops.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
577 return self._readbuffer[self._offset: self._offset + 512]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
578
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
579 def readable(self):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
580 return True
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
581
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
582 def read(self, n=-1):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
583 """Read and return up to n bytes.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
584 If the argument is omitted, None, or negative, data is read and returned until EOF is reached..
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
585 """
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
586 buf = b''
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
587 if n is None:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
588 n = -1
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
589 while True:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
590 if n < 0:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
591 data = self.read1(n)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
592 elif n > len(buf):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
593 data = self.read1(n - len(buf))
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
594 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
595 return buf
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
596 if len(data) == 0:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
597 return buf
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
598 buf += data
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
599
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
600 def _update_crc(self, newdata, eof):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
601 # Update the CRC using the given data.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
602 if self._expected_crc is None:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
603 # No need to compute the CRC if we don't have a reference value
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
604 return
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
605 self._running_crc = crc32(newdata, self._running_crc) & 0xffffffff
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
606 # Check the CRC if we're at the end of the file
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
607 if eof and self._running_crc != self._expected_crc:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
608 raise BadZipFile("Bad CRC-32 for file %r" % self.name)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
609
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
610 def read1(self, n):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
611 """Read up to n bytes with at most one read() system call."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
612
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
613 # Simplify algorithm (branching) by transforming negative n to large n.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
614 if n < 0 or n is None:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
615 n = self.MAX_N
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
616
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
617 # Bytes available in read buffer.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
618 len_readbuffer = len(self._readbuffer) - self._offset
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
619
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
620 # Read from file.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
621 if self._compress_left > 0 and n > len_readbuffer + len(self._unconsumed):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
622 nbytes = n - len_readbuffer - len(self._unconsumed)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
623 nbytes = max(nbytes, self.MIN_READ_SIZE)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
624 nbytes = min(nbytes, self._compress_left)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
625
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
626 data = self._fileobj.read(nbytes)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
627 self._compress_left -= len(data)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
628
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
629 if data and self._decrypter is not None:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
630 data = bytes(map(self._decrypter, data))
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
631
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
632 if self._compress_type == ZIP_STORED:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
633 self._update_crc(data, eof=(self._compress_left==0))
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
634 self._readbuffer = self._readbuffer[self._offset:] + data
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
635 self._offset = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
636 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
637 # Prepare deflated bytes for decompression.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
638 self._unconsumed += data
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
639
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
640 # Handle unconsumed data.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
641 if (len(self._unconsumed) > 0 and n > len_readbuffer and
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
642 self._compress_type == ZIP_DEFLATED):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
643 data = self._decompressor.decompress(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
644 self._unconsumed,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
645 max(n - len_readbuffer, self.MIN_READ_SIZE)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
646 )
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
647
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
648 self._unconsumed = self._decompressor.unconsumed_tail
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
649 eof = len(self._unconsumed) == 0 and self._compress_left == 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
650 if eof:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
651 data += self._decompressor.flush()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
652
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
653 self._update_crc(data, eof=eof)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
654 self._readbuffer = self._readbuffer[self._offset:] + data
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
655 self._offset = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
656 elif (len(self._unconsumed) > 0 and n > len_readbuffer and
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
657 self._compress_type == ZIP_BZIP2):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
658 try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
659 data = self._decompressor.decompress(self._unconsumed)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
660 except EOFError:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
661 eof = self._compress_left
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
662 data = b''
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
663 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
664 eof = False
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
665 self._unconsumed = b''
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
666
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
667 self._update_crc(data, eof=eof)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
668 self._readbuffer = self._readbuffer[self._offset:] + data
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
669 self._offset = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
670
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
671 # Read from buffer.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
672 data = self._readbuffer[self._offset: self._offset + n]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
673 self._offset += len(data)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
674 return data
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
675
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
676 def close(self):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
677 try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
678 if self._close_fileobj:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
679 self._fileobj.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
680 finally:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
681 super().close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
682
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
683
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
684 class ZipFile:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
685 """ Class with methods to open, read, write, close, list zip files.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
686
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
687 z = ZipFile(file, mode="r", compression=ZIP_STORED, allowZip64=False)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
688
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
689 file: Either the path to the file, or a file-like object.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
690 If it is a path, the file will be opened and closed by ZipFile.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
691 mode: The mode can be either read "r", write "w" or append "a".
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
692 compression: ZIP_STORED (no compression), ZIP_DEFLATED (requires zlib)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
693 or ZIP_BZIP2 (requires bz2).
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
694 allowZip64: if True ZipFile will create files with ZIP64 extensions when
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
695 needed, otherwise it will raise an exception when this would
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
696 be necessary.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
697
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
698 """
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
699
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
700 fp = None # Set here since __del__ checks it
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
701
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
702 def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=False):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
703 """Open the ZIP file with mode read "r", write "w" or append "a"."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
704 if mode not in ("r", "w", "a"):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
705 raise RuntimeError('ZipFile() requires mode "r", "w", or "a"')
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
706
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
707 if compression == ZIP_STORED:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
708 pass
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
709 elif compression == ZIP_DEFLATED:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
710 if not zlib:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
711 raise RuntimeError(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
712 "Compression requires the (missing) zlib module")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
713 elif compression == ZIP_BZIP2:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
714 if not bz2:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
715 raise RuntimeError(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
716 "Compression requires the (missing) bz2 module")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
717 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
718 raise RuntimeError("That compression method is not supported")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
719
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
720 self._allowZip64 = allowZip64
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
721 self._didModify = False
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
722 self.debug = 0 # Level of printing: 0 through 3
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
723 self.NameToInfo = {} # Find file info given name
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
724 self.filelist = [] # List of ZipInfo instances for archive
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
725 self.compression = compression # Method of compression
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
726 self.mode = key = mode.replace('b', '')[0]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
727 self.pwd = None
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
728 self.comment = b''
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
729
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
730 # Check if we were passed a file-like object
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
731 if isinstance(file, str):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
732 # No, it's a filename
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
733 self._filePassed = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
734 self.filename = file
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
735 modeDict = {'r' : 'rb', 'w': 'wb', 'a' : 'r+b'}
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
736 try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
737 self.fp = io.open(file, modeDict[mode])
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
738 except IOError:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
739 if mode == 'a':
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
740 mode = key = 'w'
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
741 self.fp = io.open(file, modeDict[mode])
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
742 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
743 raise
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
744 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
745 self._filePassed = 1
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
746 self.fp = file
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
747 self.filename = getattr(file, 'name', None)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
748
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
749 if key == 'r':
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
750 self._GetContents()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
751 elif key == 'w':
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
752 # set the modified flag so central directory gets written
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
753 # even if no files are added to the archive
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
754 self._didModify = True
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
755 elif key == 'a':
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
756 try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
757 # See if file is a zip file
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
758 self._RealGetContents()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
759 # seek to start of directory and overwrite
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
760 self.fp.seek(self.start_dir, 0)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
761 except BadZipFile:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
762 # file is not a zip file, just append
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
763 self.fp.seek(0, 2)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
764
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
765 # set the modified flag so central directory gets written
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
766 # even if no files are added to the archive
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
767 self._didModify = True
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
768 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
769 if not self._filePassed:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
770 self.fp.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
771 self.fp = None
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
772 raise RuntimeError('Mode must be "r", "w" or "a"')
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
773
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
774 def __enter__(self):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
775 return self
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
776
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
777 def __exit__(self, type, value, traceback):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
778 self.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
779
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
780 def _GetContents(self):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
781 """Read the directory, making sure we close the file if the format
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
782 is bad."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
783 try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
784 self._RealGetContents()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
785 except BadZipFile:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
786 if not self._filePassed:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
787 self.fp.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
788 self.fp = None
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
789 raise
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
790
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
791 def _RealGetContents(self):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
792 """Read in the table of contents for the ZIP file."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
793 fp = self.fp
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
794 try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
795 endrec = _EndRecData(fp)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
796 except IOError:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
797 raise BadZipFile("File is not a zip file")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
798 if not endrec:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
799 raise BadZipFile("File is not a zip file")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
800 if self.debug > 1:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
801 print(endrec)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
802 size_cd = endrec[_ECD_SIZE] # bytes in central directory
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
803 offset_cd = endrec[_ECD_OFFSET] # offset of central directory
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
804 self.comment = endrec[_ECD_COMMENT] # archive comment
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
805
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
806 # "concat" is zero, unless zip was concatenated to another file
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
807 concat = endrec[_ECD_LOCATION] - size_cd - offset_cd
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
808 if endrec[_ECD_SIGNATURE] == stringEndArchive64:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
809 # If Zip64 extension structures are present, account for them
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
810 concat -= (sizeEndCentDir64 + sizeEndCentDir64Locator)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
811
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
812 if self.debug > 2:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
813 inferred = concat + offset_cd
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
814 print("given, inferred, offset", offset_cd, inferred, concat)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
815 # self.start_dir: Position of start of central directory
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
816 self.start_dir = offset_cd + concat
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
817 fp.seek(self.start_dir, 0)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
818 data = fp.read(size_cd)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
819 fp = io.BytesIO(data)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
820 total = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
821 while total < size_cd:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
822 centdir = fp.read(sizeCentralDir)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
823 if centdir[0:4] != stringCentralDir:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
824 raise BadZipFile("Bad magic number for central directory")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
825 centdir = struct.unpack(structCentralDir, centdir)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
826 if self.debug > 2:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
827 print(centdir)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
828 filename = fp.read(centdir[_CD_FILENAME_LENGTH])
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
829 flags = centdir[5]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
830 if flags & 0x800:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
831 # UTF-8 file names extension
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
832 filename = filename.decode('utf-8')
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
833 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
834 # Historical ZIP filename encoding
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
835 filename = filename.decode('cp437')
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
836 # Create ZipInfo instance to store file information
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
837 x = ZipInfo(filename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
838 x.extra = fp.read(centdir[_CD_EXTRA_FIELD_LENGTH])
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
839 x.comment = fp.read(centdir[_CD_COMMENT_LENGTH])
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
840 x.header_offset = centdir[_CD_LOCAL_HEADER_OFFSET]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
841 (x.create_version, x.create_system, x.extract_version, x.reserved,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
842 x.flag_bits, x.compress_type, t, d,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
843 x.CRC, x.compress_size, x.file_size) = centdir[1:12]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
844 x.volume, x.internal_attr, x.external_attr = centdir[15:18]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
845 # Convert date/time code to (year, month, day, hour, min, sec)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
846 x._raw_time = t
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
847 x.date_time = ( (d>>9)+1980, (d>>5)&0xF, d&0x1F,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
848 t>>11, (t>>5)&0x3F, (t&0x1F) * 2 )
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
849
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
850 x._decodeExtra()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
851 x.header_offset = x.header_offset + concat
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
852 self.filelist.append(x)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
853 self.NameToInfo[x.filename] = x
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
854
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
855 # update total bytes read from central directory
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
856 total = (total + sizeCentralDir + centdir[_CD_FILENAME_LENGTH]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
857 + centdir[_CD_EXTRA_FIELD_LENGTH]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
858 + centdir[_CD_COMMENT_LENGTH])
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
859
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
860 if self.debug > 2:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
861 print("total", total)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
862
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
863
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
864 def namelist(self):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
865 """Return a list of file names in the archive."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
866 l = []
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
867 for data in self.filelist:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
868 l.append(data.filename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
869 return l
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
870
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
871 def infolist(self):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
872 """Return a list of class ZipInfo instances for files in the
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
873 archive."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
874 return self.filelist
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
875
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
876 def printdir(self, file=None):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
877 """Print a table of contents for the zip file."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
878 print("%-46s %19s %12s" % ("File Name", "Modified ", "Size"),
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
879 file=file)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
880 for zinfo in self.filelist:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
881 date = "%d-%02d-%02d %02d:%02d:%02d" % zinfo.date_time[:6]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
882 print("%-46s %s %12d" % (zinfo.filename, date, zinfo.file_size),
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
883 file=file)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
884
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
885 def testzip(self):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
886 """Read all the files and check the CRC."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
887 chunk_size = 2 ** 20
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
888 for zinfo in self.filelist:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
889 try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
890 # Read by chunks, to avoid an OverflowError or a
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
891 # MemoryError with very large embedded files.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
892 f = self.open(zinfo.filename, "r")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
893 while f.read(chunk_size): # Check CRC-32
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
894 pass
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
895 except BadZipFile:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
896 return zinfo.filename
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
897
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
898 def getinfo(self, name):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
899 """Return the instance of ZipInfo given 'name'."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
900 info = self.NameToInfo.get(name)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
901 if info is None:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
902 raise KeyError(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
903 'There is no item named %r in the archive' % name)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
904
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
905 return info
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
906
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
907 def setpassword(self, pwd):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
908 """Set default password for encrypted files."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
909 if pwd and not isinstance(pwd, bytes):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
910 raise TypeError("pwd: expected bytes, got %s" % type(pwd))
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
911 if pwd:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
912 self.pwd = pwd
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
913 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
914 self.pwd = None
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
915
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
916 def read(self, name, pwd=None):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
917 """Return file bytes (as a string) for name."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
918 with self.open(name, "r", pwd) as fp:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
919 return fp.read()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
920
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
921 def open(self, name, mode="r", pwd=None):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
922 """Return file-like object for 'name'."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
923 if mode not in ("r", "U", "rU"):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
924 raise RuntimeError('open() requires mode "r", "U", or "rU"')
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
925 if pwd and not isinstance(pwd, bytes):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
926 raise TypeError("pwd: expected bytes, got %s" % type(pwd))
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
927 if not self.fp:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
928 raise RuntimeError(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
929 "Attempt to read ZIP archive that was already closed")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
930
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
931 # Only open a new file for instances where we were not
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
932 # given a file object in the constructor
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
933 if self._filePassed:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
934 zef_file = self.fp
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
935 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
936 zef_file = io.open(self.filename, 'rb')
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
937
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
938 # Make sure we have an info object
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
939 if isinstance(name, ZipInfo):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
940 # 'name' is already an info object
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
941 zinfo = name
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
942 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
943 # Get info object for name
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
944 try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
945 zinfo = self.getinfo(name)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
946 except KeyError:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
947 if not self._filePassed:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
948 zef_file.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
949 raise
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
950 zef_file.seek(zinfo.header_offset, 0)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
951
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
952 # Skip the file header:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
953 fheader = zef_file.read(sizeFileHeader)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
954 if fheader[0:4] != stringFileHeader:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
955 raise BadZipFile("Bad magic number for file header")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
956
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
957 fheader = struct.unpack(structFileHeader, fheader)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
958 fname = zef_file.read(fheader[_FH_FILENAME_LENGTH])
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
959 if fheader[_FH_EXTRA_FIELD_LENGTH]:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
960 zef_file.read(fheader[_FH_EXTRA_FIELD_LENGTH])
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
961
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
962 if zinfo.flag_bits & 0x800:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
963 # UTF-8 filename
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
964 fname_str = fname.decode("utf-8")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
965 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
966 fname_str = fname.decode("cp437")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
967
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
968 if fname_str != zinfo.orig_filename:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
969 if not self._filePassed:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
970 zef_file.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
971 raise BadZipFile(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
972 'File name in directory %r and header %r differ.'
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
973 % (zinfo.orig_filename, fname))
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
974
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
975 # check for encrypted flag & handle password
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
976 is_encrypted = zinfo.flag_bits & 0x1
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
977 zd = None
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
978 if is_encrypted:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
979 if not pwd:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
980 pwd = self.pwd
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
981 if not pwd:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
982 if not self._filePassed:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
983 zef_file.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
984 raise RuntimeError("File %s is encrypted, "
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
985 "password required for extraction" % name)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
986
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
987 zd = _ZipDecrypter(pwd)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
988 # The first 12 bytes in the cypher stream is an encryption header
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
989 # used to strengthen the algorithm. The first 11 bytes are
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
990 # completely random, while the 12th contains the MSB of the CRC,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
991 # or the MSB of the file time depending on the header type
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
992 # and is used to check the correctness of the password.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
993 header = zef_file.read(12)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
994 h = list(map(zd, header[0:12]))
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
995 if zinfo.flag_bits & 0x8:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
996 # compare against the file type from extended local headers
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
997 check_byte = (zinfo._raw_time >> 8) & 0xff
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
998 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
999 # compare against the CRC otherwise
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1000 check_byte = (zinfo.CRC >> 24) & 0xff
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1001 if h[11] != check_byte:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1002 if not self._filePassed:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1003 zef_file.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1004 raise RuntimeError("Bad password for file", name)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1005
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1006 return ZipExtFile(zef_file, mode, zinfo, zd,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1007 close_fileobj=not self._filePassed)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1008
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1009 def extract(self, member, path=None, pwd=None):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1010 """Extract a member from the archive to the current working directory,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1011 using its full name. Its file information is extracted as accurately
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1012 as possible. `member' may be a filename or a ZipInfo object. You can
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1013 specify a different directory using `path'.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1014 """
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1015 if not isinstance(member, ZipInfo):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1016 member = self.getinfo(member)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1017
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1018 if path is None:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1019 path = os.getcwd()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1020
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1021 return self._extract_member(member, path, pwd)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1022
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1023 def extractall(self, path=None, members=None, pwd=None):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1024 """Extract all members from the archive to the current working
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1025 directory. `path' specifies a different directory to extract to.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1026 `members' is optional and must be a subset of the list returned
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1027 by namelist().
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1028 """
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1029 if members is None:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1030 members = self.namelist()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1031
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1032 for zipinfo in members:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1033 self.extract(zipinfo, path, pwd)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1034
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1035 def _extract_member(self, member, targetpath, pwd):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1036 """Extract the ZipInfo object 'member' to a physical
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1037 file on the path targetpath.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1038 """
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1039 # build the destination pathname, replacing
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1040 # forward slashes to platform specific separators.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1041 # Strip trailing path separator, unless it represents the root.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1042 if (targetpath[-1:] in (os.path.sep, os.path.altsep)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1043 and len(os.path.splitdrive(targetpath)[1]) > 1):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1044 targetpath = targetpath[:-1]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1045
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1046 # don't include leading "/" from file name if present
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1047 if member.filename[0] == '/':
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1048 targetpath = os.path.join(targetpath, member.filename[1:])
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1049 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1050 targetpath = os.path.join(targetpath, member.filename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1051
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1052 targetpath = os.path.normpath(targetpath)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1053
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1054 # Create all upper directories if necessary.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1055 upperdirs = os.path.dirname(targetpath)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1056 if upperdirs and not os.path.exists(upperdirs):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1057 os.makedirs(upperdirs)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1058
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1059 if member.filename[-1] == '/':
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1060 if not os.path.isdir(targetpath):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1061 os.mkdir(targetpath)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1062 return targetpath
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1063
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1064 source = self.open(member, pwd=pwd)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1065 target = open(targetpath, "wb")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1066 shutil.copyfileobj(source, target)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1067 source.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1068 target.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1069
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1070 return targetpath
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1071
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1072 def _writecheck(self, zinfo):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1073 """Check for errors before writing a file to the archive."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1074 if zinfo.filename in self.NameToInfo:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1075 if self.debug: # Warning for duplicate names
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1076 print("Duplicate name:", zinfo.filename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1077 if self.mode not in ("w", "a"):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1078 raise RuntimeError('write() requires mode "w" or "a"')
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1079 if not self.fp:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1080 raise RuntimeError(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1081 "Attempt to write ZIP archive that was already closed")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1082 if zinfo.compress_type == ZIP_DEFLATED and not zlib:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1083 raise RuntimeError(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1084 "Compression requires the (missing) zlib module")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1085 if zinfo.compress_type == ZIP_BZIP2 and not bz2:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1086 raise RuntimeError(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1087 "Compression requires the (missing) bz2 module")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1088 if zinfo.compress_type not in (ZIP_STORED, ZIP_DEFLATED, ZIP_BZIP2):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1089 raise RuntimeError("That compression method is not supported")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1090 if zinfo.file_size > ZIP64_LIMIT:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1091 if not self._allowZip64:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1092 raise LargeZipFile("Filesize would require ZIP64 extensions")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1093 if zinfo.header_offset > ZIP64_LIMIT:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1094 if not self._allowZip64:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1095 raise LargeZipFile(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1096 "Zipfile size would require ZIP64 extensions")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1097
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1098 def write(self, filename, arcname=None, compress_type=None):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1099 """Put the bytes from filename into the archive under the name
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1100 arcname."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1101 if not self.fp:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1102 raise RuntimeError(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1103 "Attempt to write to ZIP archive that was already closed")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1104
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1105 st = os.stat(filename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1106 isdir = stat.S_ISDIR(st.st_mode)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1107 mtime = time.localtime(st.st_mtime)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1108 date_time = mtime[0:6]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1109 # Create ZipInfo instance to store file information
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1110 if arcname is None:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1111 arcname = filename
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1112 arcname = os.path.normpath(os.path.splitdrive(arcname)[1])
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1113 while arcname[0] in (os.sep, os.altsep):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1114 arcname = arcname[1:]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1115 if isdir:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1116 arcname += '/'
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1117 zinfo = ZipInfo(arcname, date_time)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1118 zinfo.external_attr = (st[0] & 0xFFFF) << 16 # Unix attributes
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1119 if compress_type is None:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1120 zinfo.compress_type = self.compression
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1121 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1122 zinfo.compress_type = compress_type
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1123
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1124 zinfo.file_size = st.st_size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1125 zinfo.flag_bits = 0x00
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1126 zinfo.header_offset = self.fp.tell() # Start of header bytes
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1127
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1128 self._writecheck(zinfo)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1129 self._didModify = True
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1130
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1131 if isdir:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1132 zinfo.file_size = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1133 zinfo.compress_size = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1134 zinfo.CRC = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1135 self.filelist.append(zinfo)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1136 self.NameToInfo[zinfo.filename] = zinfo
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1137 self.fp.write(zinfo.FileHeader())
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1138 return
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1139
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1140 with open(filename, "rb") as fp:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1141 # Must overwrite CRC and sizes with correct data later
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1142 zinfo.CRC = CRC = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1143 zinfo.compress_size = compress_size = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1144 zinfo.file_size = file_size = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1145 self.fp.write(zinfo.FileHeader())
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1146 if zinfo.compress_type == ZIP_DEFLATED:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1147 cmpr = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1148 zlib.DEFLATED, -15)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1149 elif zinfo.compress_type == ZIP_BZIP2:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1150 cmpr = bz2.BZ2Compressor()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1151 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1152 cmpr = None
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1153 while 1:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1154 buf = fp.read(1024 * 8)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1155 if not buf:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1156 break
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1157 file_size = file_size + len(buf)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1158 CRC = crc32(buf, CRC) & 0xffffffff
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1159 if cmpr:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1160 buf = cmpr.compress(buf)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1161 compress_size = compress_size + len(buf)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1162 self.fp.write(buf)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1163 if cmpr:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1164 buf = cmpr.flush()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1165 compress_size = compress_size + len(buf)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1166 self.fp.write(buf)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1167 zinfo.compress_size = compress_size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1168 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1169 zinfo.compress_size = file_size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1170 zinfo.CRC = CRC
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1171 zinfo.file_size = file_size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1172 # Seek backwards and write CRC and file sizes
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1173 position = self.fp.tell() # Preserve current position in file
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1174 self.fp.seek(zinfo.header_offset + 14, 0)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1175 self.fp.write(struct.pack("<LLL", zinfo.CRC, zinfo.compress_size,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1176 zinfo.file_size))
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1177 self.fp.seek(position, 0)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1178 self.filelist.append(zinfo)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1179 self.NameToInfo[zinfo.filename] = zinfo
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1180
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1181 def writestr(self, zinfo_or_arcname, data, compress_type=None):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1182 """Write a file into the archive. The contents is 'data', which
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1183 may be either a 'str' or a 'bytes' instance; if it is a 'str',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1184 it is encoded as UTF-8 first.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1185 'zinfo_or_arcname' is either a ZipInfo instance or
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1186 the name of the file in the archive."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1187 if isinstance(data, str):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1188 data = data.encode("utf-8")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1189 if not isinstance(zinfo_or_arcname, ZipInfo):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1190 zinfo = ZipInfo(filename=zinfo_or_arcname,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1191 date_time=time.localtime(time.time())[:6])
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1192 zinfo.compress_type = self.compression
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1193 zinfo.external_attr = 0o600 << 16
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1194 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1195 zinfo = zinfo_or_arcname
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1196
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1197 if not self.fp:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1198 raise RuntimeError(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1199 "Attempt to write to ZIP archive that was already closed")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1200
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1201 zinfo.file_size = len(data) # Uncompressed size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1202 zinfo.header_offset = self.fp.tell() # Start of header data
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1203 if compress_type is not None:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1204 zinfo.compress_type = compress_type
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1205
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1206 self._writecheck(zinfo)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1207 self._didModify = True
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1208 zinfo.CRC = crc32(data) & 0xffffffff # CRC-32 checksum
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1209 if zinfo.compress_type == ZIP_DEFLATED:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1210 co = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1211 zlib.DEFLATED, -15)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1212 data = co.compress(data) + co.flush()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1213 zinfo.compress_size = len(data) # Compressed size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1214 elif zinfo.compress_type == ZIP_BZIP2:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1215 co = bz2.BZ2Compressor()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1216 data = co.compress(data) + co.flush()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1217 zinfo.compress_size = len(data) # Compressed size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1218 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1219 zinfo.compress_size = zinfo.file_size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1220 zinfo.header_offset = self.fp.tell() # Start of header data
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1221 self.fp.write(zinfo.FileHeader())
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1222 self.fp.write(data)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1223 self.fp.flush()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1224 if zinfo.flag_bits & 0x08:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1225 # Write CRC and file sizes after the file data
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1226 self.fp.write(struct.pack("<LLL", zinfo.CRC, zinfo.compress_size,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1227 zinfo.file_size))
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1228 self.filelist.append(zinfo)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1229 self.NameToInfo[zinfo.filename] = zinfo
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1230
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1231 def __del__(self):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1232 """Call the "close()" method in case the user forgot."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1233 self.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1234
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1235 def close(self):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1236 """Close the file, and for mode "w" and "a" write the ending
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1237 records."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1238 if self.fp is None:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1239 return
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1240
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1241 if self.mode in ("w", "a") and self._didModify: # write ending records
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1242 count = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1243 pos1 = self.fp.tell()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1244 for zinfo in self.filelist: # write central directory
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1245 count = count + 1
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1246 dt = zinfo.date_time
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1247 dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1248 dostime = dt[3] << 11 | dt[4] << 5 | (dt[5] // 2)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1249 extra = []
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1250 if zinfo.file_size > ZIP64_LIMIT \
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1251 or zinfo.compress_size > ZIP64_LIMIT:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1252 extra.append(zinfo.file_size)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1253 extra.append(zinfo.compress_size)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1254 file_size = 0xffffffff
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1255 compress_size = 0xffffffff
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1256 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1257 file_size = zinfo.file_size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1258 compress_size = zinfo.compress_size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1259
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1260 if zinfo.header_offset > ZIP64_LIMIT:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1261 extra.append(zinfo.header_offset)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1262 header_offset = 0xffffffff
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1263 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1264 header_offset = zinfo.header_offset
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1265
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1266 extra_data = zinfo.extra
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1267 if extra:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1268 # Append a ZIP64 field to the extra's
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1269 extra_data = struct.pack(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1270 '<HH' + 'Q'*len(extra),
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1271 1, 8*len(extra), *extra) + extra_data
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1272
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1273 extract_version = max(45, zinfo.extract_version)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1274 create_version = max(45, zinfo.create_version)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1275 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1276 extract_version = zinfo.extract_version
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1277 create_version = zinfo.create_version
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1278
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1279 try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1280 filename, flag_bits = zinfo._encodeFilenameFlags()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1281 centdir = struct.pack(structCentralDir,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1282 stringCentralDir, create_version,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1283 zinfo.create_system, extract_version, zinfo.reserved,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1284 flag_bits, zinfo.compress_type, dostime, dosdate,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1285 zinfo.CRC, compress_size, file_size,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1286 len(filename), len(extra_data), len(zinfo.comment),
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1287 0, zinfo.internal_attr, zinfo.external_attr,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1288 header_offset)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1289 except DeprecationWarning:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1290 print((structCentralDir, stringCentralDir, create_version,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1291 zinfo.create_system, extract_version, zinfo.reserved,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1292 zinfo.flag_bits, zinfo.compress_type, dostime, dosdate,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1293 zinfo.CRC, compress_size, file_size,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1294 len(zinfo.filename), len(extra_data), len(zinfo.comment),
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1295 0, zinfo.internal_attr, zinfo.external_attr,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1296 header_offset), file=sys.stderr)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1297 raise
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1298 self.fp.write(centdir)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1299 self.fp.write(filename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1300 self.fp.write(extra_data)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1301 self.fp.write(zinfo.comment)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1302
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1303 pos2 = self.fp.tell()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1304 # Write end-of-zip-archive record
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1305 centDirCount = count
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1306 centDirSize = pos2 - pos1
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1307 centDirOffset = pos1
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1308 if (centDirCount >= ZIP_FILECOUNT_LIMIT or
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1309 centDirOffset > ZIP64_LIMIT or
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1310 centDirSize > ZIP64_LIMIT):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1311 # Need to write the ZIP64 end-of-archive records
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1312 zip64endrec = struct.pack(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1313 structEndArchive64, stringEndArchive64,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1314 44, 45, 45, 0, 0, centDirCount, centDirCount,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1315 centDirSize, centDirOffset)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1316 self.fp.write(zip64endrec)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1317
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1318 zip64locrec = struct.pack(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1319 structEndArchive64Locator,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1320 stringEndArchive64Locator, 0, pos2, 1)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1321 self.fp.write(zip64locrec)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1322 centDirCount = min(centDirCount, 0xFFFF)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1323 centDirSize = min(centDirSize, 0xFFFFFFFF)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1324 centDirOffset = min(centDirOffset, 0xFFFFFFFF)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1325
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1326 # check for valid comment length
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1327 if len(self.comment) >= ZIP_MAX_COMMENT:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1328 if self.debug > 0:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1329 msg = 'Archive comment is too long; truncating to %d bytes' \
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1330 % ZIP_MAX_COMMENT
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1331 self.comment = self.comment[:ZIP_MAX_COMMENT]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1332
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1333 endrec = struct.pack(structEndArchive, stringEndArchive,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1334 0, 0, centDirCount, centDirCount,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1335 centDirSize, centDirOffset, len(self.comment))
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1336 self.fp.write(endrec)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1337 self.fp.write(self.comment)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1338 self.fp.flush()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1339
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1340 if not self._filePassed:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1341 self.fp.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1342 self.fp = None
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1343
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1344
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1345 class PyZipFile(ZipFile):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1346 """Class to create ZIP archives with Python library files and packages."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1347
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1348 def __init__(self, file, mode="r", compression=ZIP_STORED,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1349 allowZip64=False, optimize=-1):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1350 ZipFile.__init__(self, file, mode=mode, compression=compression,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1351 allowZip64=allowZip64)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1352 self._optimize = optimize
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1353
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1354 def writepy(self, pathname, basename=""):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1355 """Add all files from "pathname" to the ZIP archive.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1356
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1357 If pathname is a package directory, search the directory and
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1358 all package subdirectories recursively for all *.py and enter
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1359 the modules into the archive. If pathname is a plain
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1360 directory, listdir *.py and enter all modules. Else, pathname
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1361 must be a Python *.py file and the module will be put into the
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1362 archive. Added modules are always module.pyo or module.pyc.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1363 This method will compile the module.py into module.pyc if
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1364 necessary.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1365 """
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1366 dir, name = os.path.split(pathname)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1367 if os.path.isdir(pathname):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1368 initname = os.path.join(pathname, "__init__.py")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1369 if os.path.isfile(initname):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1370 # This is a package directory, add it
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1371 if basename:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1372 basename = "%s/%s" % (basename, name)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1373 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1374 basename = name
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1375 if self.debug:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1376 print("Adding package in", pathname, "as", basename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1377 fname, arcname = self._get_codename(initname[0:-3], basename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1378 if self.debug:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1379 print("Adding", arcname)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1380 self.write(fname, arcname)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1381 dirlist = os.listdir(pathname)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1382 dirlist.remove("__init__.py")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1383 # Add all *.py files and package subdirectories
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1384 for filename in dirlist:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1385 path = os.path.join(pathname, filename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1386 root, ext = os.path.splitext(filename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1387 if os.path.isdir(path):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1388 if os.path.isfile(os.path.join(path, "__init__.py")):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1389 # This is a package directory, add it
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1390 self.writepy(path, basename) # Recursive call
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1391 elif ext == ".py":
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1392 fname, arcname = self._get_codename(path[0:-3],
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1393 basename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1394 if self.debug:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1395 print("Adding", arcname)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1396 self.write(fname, arcname)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1397 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1398 # This is NOT a package directory, add its files at top level
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1399 if self.debug:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1400 print("Adding files from directory", pathname)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1401 for filename in os.listdir(pathname):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1402 path = os.path.join(pathname, filename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1403 root, ext = os.path.splitext(filename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1404 if ext == ".py":
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1405 fname, arcname = self._get_codename(path[0:-3],
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1406 basename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1407 if self.debug:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1408 print("Adding", arcname)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1409 self.write(fname, arcname)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1410 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1411 if pathname[-3:] != ".py":
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1412 raise RuntimeError(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1413 'Files added with writepy() must end with ".py"')
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1414 fname, arcname = self._get_codename(pathname[0:-3], basename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1415 if self.debug:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1416 print("Adding file", arcname)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1417 self.write(fname, arcname)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1418
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1419 def _get_codename(self, pathname, basename):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1420 """Return (filename, archivename) for the path.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1421
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1422 Given a module name path, return the correct file path and
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1423 archive name, compiling if necessary. For example, given
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1424 /python/lib/string, return (/python/lib/string.pyc, string).
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1425 """
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1426 def _compile(file, optimize=-1):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1427 import py_compile
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1428 if self.debug:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1429 print("Compiling", file)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1430 try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1431 py_compile.compile(file, doraise=True, optimize=optimize)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1432 except py_compile.PyCompileError as error:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1433 print(err.msg)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1434 return False
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1435 return True
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1436
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1437 file_py = pathname + ".py"
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1438 file_pyc = pathname + ".pyc"
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1439 file_pyo = pathname + ".pyo"
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1440 pycache_pyc = imp.cache_from_source(file_py, True)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1441 pycache_pyo = imp.cache_from_source(file_py, False)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1442 if self._optimize == -1:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1443 # legacy mode: use whatever file is present
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1444 if (os.path.isfile(file_pyo) and
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1445 os.stat(file_pyo).st_mtime >= os.stat(file_py).st_mtime):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1446 # Use .pyo file.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1447 arcname = fname = file_pyo
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1448 elif (os.path.isfile(file_pyc) and
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1449 os.stat(file_pyc).st_mtime >= os.stat(file_py).st_mtime):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1450 # Use .pyc file.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1451 arcname = fname = file_pyc
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1452 elif (os.path.isfile(pycache_pyc) and
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1453 os.stat(pycache_pyc).st_mtime >= os.stat(file_py).st_mtime):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1454 # Use the __pycache__/*.pyc file, but write it to the legacy pyc
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1455 # file name in the archive.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1456 fname = pycache_pyc
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1457 arcname = file_pyc
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1458 elif (os.path.isfile(pycache_pyo) and
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1459 os.stat(pycache_pyo).st_mtime >= os.stat(file_py).st_mtime):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1460 # Use the __pycache__/*.pyo file, but write it to the legacy pyo
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1461 # file name in the archive.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1462 fname = pycache_pyo
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1463 arcname = file_pyo
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1464 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1465 # Compile py into PEP 3147 pyc file.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1466 if _compile(file_py):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1467 fname = (pycache_pyc if __debug__ else pycache_pyo)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1468 arcname = (file_pyc if __debug__ else file_pyo)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1469 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1470 fname = arcname = file_py
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1471 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1472 # new mode: use given optimization level
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1473 if self._optimize == 0:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1474 fname = pycache_pyc
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1475 arcname = file_pyc
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1476 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1477 fname = pycache_pyo
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1478 arcname = file_pyo
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1479 if not (os.path.isfile(fname) and
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1480 os.stat(fname).st_mtime >= os.stat(file_py).st_mtime):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1481 if not _compile(file_py, optimize=self._optimize):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1482 fname = arcname = file_py
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1483 archivename = os.path.split(arcname)[1]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1484 if basename:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1485 archivename = "%s/%s" % (basename, archivename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1486 return (fname, archivename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1487
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1488
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1489 def main(args = None):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1490 import textwrap
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1491 USAGE=textwrap.dedent("""\
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1492 Usage:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1493 zipfile.py -l zipfile.zip # Show listing of a zipfile
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1494 zipfile.py -t zipfile.zip # Test if a zipfile is valid
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1495 zipfile.py -e zipfile.zip target # Extract zipfile into target dir
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1496 zipfile.py -c zipfile.zip src ... # Create zipfile from sources
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1497 """)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1498 if args is None:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1499 args = sys.argv[1:]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1500
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1501 if not args or args[0] not in ('-l', '-c', '-e', '-t'):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1502 print(USAGE)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1503 sys.exit(1)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1504
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1505 if args[0] == '-l':
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1506 if len(args) != 2:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1507 print(USAGE)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1508 sys.exit(1)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1509 zf = ZipFile(args[1], 'r')
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1510 zf.printdir()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1511 zf.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1512
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1513 elif args[0] == '-t':
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1514 if len(args) != 2:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1515 print(USAGE)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1516 sys.exit(1)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1517 zf = ZipFile(args[1], 'r')
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1518 badfile = zf.testzip()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1519 if badfile:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1520 print("The following enclosed file is corrupted: {!r}".format(badfile))
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1521 print("Done testing")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1522
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1523 elif args[0] == '-e':
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1524 if len(args) != 3:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1525 print(USAGE)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1526 sys.exit(1)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1527
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1528 zf = ZipFile(args[1], 'r')
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1529 out = args[2]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1530 for path in zf.namelist():
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1531 if path.startswith('./'):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1532 tgt = os.path.join(out, path[2:])
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1533 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1534 tgt = os.path.join(out, path)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1535
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1536 tgtdir = os.path.dirname(tgt)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1537 if not os.path.exists(tgtdir):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1538 os.makedirs(tgtdir)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1539 with open(tgt, 'wb') as fp:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1540 fp.write(zf.read(path))
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1541 zf.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1542
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1543 elif args[0] == '-c':
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1544 if len(args) < 3:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1545 print(USAGE)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1546 sys.exit(1)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1547
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1548 def addToZip(zf, path, zippath):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1549 if os.path.isfile(path):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1550 zf.write(path, zippath, ZIP_DEFLATED)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1551 elif os.path.isdir(path):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1552 for nm in os.listdir(path):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1553 addToZip(zf,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1554 os.path.join(path, nm), os.path.join(zippath, nm))
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1555 # else: ignore
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1556
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1557 zf = ZipFile(args[1], 'w', allowZip64=True)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1558 for src in args[2:]:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1559 addToZip(zf, src, os.path.basename(src))
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1560
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1561 zf.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1562
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1563 if __name__ == "__main__":
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1564 main()