annotate zipfiles/zipfile32.py @ 193:a76cdc26ba9d

Added conf. var. match and match='regexp' for non-archives Specify match='regexp', and your tests and dummies will be treated as regular expressions describing test case identifiers. Every file that is in a suitable location and whose name matches {testcase,dummy}inname and the given regexp will be treated as a file with test case input data. You are free to use backreferences in the regexps, but group numbering starts at two rather than one. If you want test groups, you can get them magically created for you by putting a part of the test ID in a group in the regexp sense and specifying the tests variable as a pair consisting of the regexp itself and the number of this regexp group (remember group numbers start at two).
author Oleg Oshmyan <chortos@inbox.lv>
date Thu, 11 Aug 2011 23:20:52 +0300
parents 8196d2c0d6f8
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))
183
8196d2c0d6f8 Updated zipfiles for Python 3.2.1
Oleg Oshmyan <chortos@inbox.lv>
parents: 153
diff changeset
256 commentSize = endrec[_ECD_COMMENT_SIZE] #as claimed by the zip file
8196d2c0d6f8 Updated zipfiles for Python 3.2.1
Oleg Oshmyan <chortos@inbox.lv>
parents: 153
diff changeset
257 comment = data[start+sizeEndCentDir:start+sizeEndCentDir+commentSize]
8196d2c0d6f8 Updated zipfiles for Python 3.2.1
Oleg Oshmyan <chortos@inbox.lv>
parents: 153
diff changeset
258 endrec.append(comment)
8196d2c0d6f8 Updated zipfiles for Python 3.2.1
Oleg Oshmyan <chortos@inbox.lv>
parents: 153
diff changeset
259 endrec.append(maxCommentStart + start)
97
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
260
183
8196d2c0d6f8 Updated zipfiles for Python 3.2.1
Oleg Oshmyan <chortos@inbox.lv>
parents: 153
diff changeset
261 # Try to read the "Zip64 end of central directory" structure
8196d2c0d6f8 Updated zipfiles for Python 3.2.1
Oleg Oshmyan <chortos@inbox.lv>
parents: 153
diff changeset
262 return _EndRecData64(fpin, maxCommentStart + start - filesize,
8196d2c0d6f8 Updated zipfiles for Python 3.2.1
Oleg Oshmyan <chortos@inbox.lv>
parents: 153
diff changeset
263 endrec)
97
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
264
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
265 # 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
266 return
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
267
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
268
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
269 class ZipInfo (object):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
270 """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
271
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
272 __slots__ = (
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
273 'orig_filename',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
274 'filename',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
275 'date_time',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
276 'compress_type',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
277 'comment',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
278 'extra',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
279 'create_system',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
280 'create_version',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
281 'extract_version',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
282 'reserved',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
283 'flag_bits',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
284 'volume',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
285 'internal_attr',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
286 'external_attr',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
287 'header_offset',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
288 'CRC',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
289 'compress_size',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
290 'file_size',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
291 '_raw_time',
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
292 )
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
293
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
294 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
295 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
296
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
297 # 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
298 # 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
299 null_byte = filename.find(chr(0))
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
300 if null_byte >= 0:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
301 filename = filename[0:null_byte]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
302 # 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
303 # 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
304 # ZIP format specification.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
305 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
306 filename = filename.replace(os.sep, "/")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
307
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
308 self.filename = filename # Normalized file name
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
309 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
310 # Standard values:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
311 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
312 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
313 self.extra = b"" # ZIP extra data
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
314 if sys.platform == 'win32':
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
315 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
316 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
317 # Assume everything else is unix-y
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
318 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
319 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
320 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
321 self.reserved = 0 # Must be zero
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
322 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
323 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
324 self.internal_attr = 0 # Internal attributes
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
325 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
326 # 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
327 # 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
328 # 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
329 # 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
330 # 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
331
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
332 def FileHeader(self):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
333 """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
334 dt = self.date_time
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
335 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
336 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
337 if self.flag_bits & 0x08:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
338 # 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
339 CRC = compress_size = file_size = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
340 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
341 CRC = self.CRC
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
342 compress_size = self.compress_size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
343 file_size = self.file_size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
344
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
345 extra = self.extra
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 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
348 # 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
349 # fall back to the ZIP64 extension
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
350 fmt = '<HHQQ'
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
351 extra = extra + struct.pack(fmt,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
352 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
353 file_size = 0xffffffff
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
354 compress_size = 0xffffffff
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
355 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
356 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
357
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
358 filename, flag_bits = self._encodeFilenameFlags()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
359 header = struct.pack(structFileHeader, stringFileHeader,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
360 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
361 self.compress_type, dostime, dosdate, CRC,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
362 compress_size, file_size,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
363 len(filename), len(extra))
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
364 return header + filename + extra
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
365
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
366 def _encodeFilenameFlags(self):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
367 try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
368 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
369 except UnicodeEncodeError:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
370 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
371
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
372 def _decodeExtra(self):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
373 # Try to decode the extra field.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
374 extra = self.extra
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
375 unpack = struct.unpack
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
376 while extra:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
377 tp, ln = unpack('<HH', extra[:4])
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
378 if tp == 1:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
379 if ln >= 24:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
380 counts = unpack('<QQQ', extra[4:28])
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
381 elif ln == 16:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
382 counts = unpack('<QQ', extra[4:20])
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
383 elif ln == 8:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
384 counts = unpack('<Q', extra[4:12])
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
385 elif ln == 0:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
386 counts = ()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
387 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
388 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
389
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
390 idx = 0
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 # 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
393 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
394 self.file_size = counts[idx]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
395 idx += 1
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
396
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
397 if self.compress_size == 0xFFFFFFFF:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
398 self.compress_size = counts[idx]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
399 idx += 1
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
400
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
401 if self.header_offset == 0xffffffff:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
402 old = self.header_offset
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
403 self.header_offset = counts[idx]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
404 idx+=1
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
405
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
406 extra = extra[ln+4:]
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
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
409 class _ZipDecrypter:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
410 """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
411
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
412 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
413 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
414 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
415
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
416 Usage:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
417 zd = _ZipDecrypter(mypwd)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
418 plain_char = zd(cypher_char)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
419 plain_text = map(zd, cypher_text)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
420 """
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
421
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
422 def _GenerateCRCTable():
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
423 """Generate a CRC-32 table.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
424
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
425 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
426 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
427 relying on binascii.crc32().
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
428 """
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
429 poly = 0xedb88320
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
430 table = [0] * 256
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
431 for i in range(256):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
432 crc = i
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
433 for j in range(8):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
434 if crc & 1:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
435 crc = ((crc >> 1) & 0x7FFFFFFF) ^ poly
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
436 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
437 crc = ((crc >> 1) & 0x7FFFFFFF)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
438 table[i] = crc
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
439 return table
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
440 crctable = _GenerateCRCTable()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
441
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
442 def _crc32(self, ch, crc):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
443 """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
444 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
445
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
446 def __init__(self, pwd):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
447 self.key0 = 305419896
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
448 self.key1 = 591751049
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
449 self.key2 = 878082192
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
450 for p in pwd:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
451 self._UpdateKeys(p)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
452
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
453 def _UpdateKeys(self, c):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
454 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
455 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
456 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
457 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
458
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
459 def __call__(self, c):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
460 """Decrypt a single character."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
461 assert isinstance(c, int)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
462 k = self.key2 | 2
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
463 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
464 self._UpdateKeys(c)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
465 return c
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
466
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
467 class ZipExtFile(io.BufferedIOBase):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
468 """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
469 Is returned by ZipFile.open().
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
470 """
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
471
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
472 # Max size supported by decompressor.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
473 MAX_N = 1 << 31 - 1
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
474
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
475 # 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
476 MIN_READ_SIZE = 4096
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
477
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
478 # 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
479 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
480
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
481 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
482 close_fileobj=False):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
483 self._fileobj = fileobj
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
484 self._decrypter = decrypter
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
485 self._close_fileobj = close_fileobj
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
486
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
487 self._compress_type = zipinfo.compress_type
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
488 self._compress_size = zipinfo.compress_size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
489 self._compress_left = zipinfo.compress_size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
490
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
491 if self._compress_type == ZIP_DEFLATED:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
492 self._decompressor = zlib.decompressobj(-15)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
493 elif self._compress_type == ZIP_BZIP2:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
494 self._decompressor = bz2.BZ2Decompressor()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
495 self.MIN_READ_SIZE = 900000
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
496 self._unconsumed = b''
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
497
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
498 self._readbuffer = b''
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
499 self._offset = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
500
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
501 self._universal = 'U' in mode
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
502 self.newlines = None
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
503
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
504 # 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
505 # are for the encryption/password information.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
506 if self._decrypter is not None:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
507 self._compress_left -= 12
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
508
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
509 self.mode = mode
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
510 self.name = zipinfo.filename
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
511
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
512 if hasattr(zipinfo, 'CRC'):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
513 self._expected_crc = zipinfo.CRC
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
514 self._running_crc = crc32(b'') & 0xffffffff
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
515 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
516 self._expected_crc = None
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
517
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
518 def readline(self, limit=-1):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
519 """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
520
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
521 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
522 """
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
523
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
524 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
525 # 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
526 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
527 if i > 0:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
528 line = self._readbuffer[self._offset: i]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
529 self._offset = i
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
530 return line
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
531
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
532 if not self._universal:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
533 return io.BufferedIOBase.readline(self, limit)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
534
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
535 line = b''
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
536 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
537 readahead = self.peek(2)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
538 if readahead == b'':
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
539 return line
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
540
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
541 #
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
542 # 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
543 #
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
544 # 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
545 # 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
546 # '\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
547 # 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
548 #
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
549 match = self.PATTERN.search(readahead)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
550 newline = match.group('newline')
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
551 if newline is not None:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
552 if self.newlines is None:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
553 self.newlines = []
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
554 if newline not in self.newlines:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
555 self.newlines.append(newline)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
556 self._offset += len(newline)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
557 return line + b'\n'
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
558
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
559 chunk = match.group('chunk')
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
560 if limit >= 0:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
561 chunk = chunk[: limit - len(line)]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
562
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
563 self._offset += len(chunk)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
564 line += chunk
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
565
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
566 return line
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 def peek(self, n=1):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
569 """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
570 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
571 chunk = self.read(n)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
572 self._offset -= len(chunk)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
573
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
574 # 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
575 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
576
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
577 def readable(self):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
578 return True
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
579
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
580 def read(self, n=-1):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
581 """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
582 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
583 """
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
584 buf = b''
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
585 if n is None:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
586 n = -1
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
587 while True:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
588 if n < 0:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
589 data = self.read1(n)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
590 elif n > len(buf):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
591 data = self.read1(n - len(buf))
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
592 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
593 return buf
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
594 if len(data) == 0:
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 buf += data
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
597
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
598 def _update_crc(self, newdata, eof):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
599 # 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
600 if self._expected_crc is None:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
601 # 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
602 return
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
603 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
604 # 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
605 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
606 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
607
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
608 def read1(self, n):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
609 """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
610
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
611 # 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
612 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
613 n = self.MAX_N
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
614
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
615 # Bytes available in read buffer.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
616 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
617
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
618 # Read from file.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
619 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
620 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
621 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
622 nbytes = min(nbytes, self._compress_left)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
623
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
624 data = self._fileobj.read(nbytes)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
625 self._compress_left -= len(data)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
626
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
627 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
628 data = bytes(map(self._decrypter, data))
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
629
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
630 if self._compress_type == ZIP_STORED:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
631 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
632 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
633 self._offset = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
634 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
635 # Prepare deflated bytes for decompression.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
636 self._unconsumed += data
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
637
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
638 # Handle unconsumed data.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
639 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
640 self._compress_type == ZIP_DEFLATED):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
641 data = self._decompressor.decompress(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
642 self._unconsumed,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
643 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
644 )
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
645
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
646 self._unconsumed = self._decompressor.unconsumed_tail
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
647 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
648 if eof:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
649 data += self._decompressor.flush()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
650
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
651 self._update_crc(data, eof=eof)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
652 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
653 self._offset = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
654 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
655 self._compress_type == ZIP_BZIP2):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
656 try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
657 data = self._decompressor.decompress(self._unconsumed)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
658 except EOFError:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
659 eof = self._compress_left
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
660 data = b''
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
661 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
662 eof = False
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
663 self._unconsumed = b''
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
664
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
665 self._update_crc(data, eof=eof)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
666 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
667 self._offset = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
668
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
669 # Read from buffer.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
670 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
671 self._offset += len(data)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
672 return data
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
673
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
674 def close(self):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
675 try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
676 if self._close_fileobj:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
677 self._fileobj.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
678 finally:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
679 super().close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
680
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
681
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
682 class ZipFile:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
683 """ 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
684
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
685 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
686
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
687 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
688 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
689 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
690 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
691 or ZIP_BZIP2 (requires bz2).
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
692 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
693 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
694 be necessary.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
695
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
696 """
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 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
699
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
700 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
701 """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
702 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
703 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
704
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
705 if compression == ZIP_STORED:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
706 pass
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
707 elif compression == ZIP_DEFLATED:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
708 if not zlib:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
709 raise RuntimeError(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
710 "Compression requires the (missing) zlib module")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
711 elif compression == ZIP_BZIP2:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
712 if not bz2:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
713 raise RuntimeError(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
714 "Compression requires the (missing) bz2 module")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
715 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
716 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
717
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
718 self._allowZip64 = allowZip64
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
719 self._didModify = False
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
720 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
721 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
722 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
723 self.compression = compression # Method of compression
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
724 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
725 self.pwd = None
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
726 self.comment = b''
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
727
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
728 # 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
729 if isinstance(file, str):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
730 # No, it's a filename
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
731 self._filePassed = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
732 self.filename = file
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
733 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
734 try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
735 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
736 except IOError:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
737 if mode == 'a':
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
738 mode = key = 'w'
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
739 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
740 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
741 raise
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 self._filePassed = 1
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
744 self.fp = file
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
745 self.filename = getattr(file, 'name', None)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
746
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
747 if key == 'r':
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
748 self._GetContents()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
749 elif key == 'w':
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
750 # 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
751 # 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
752 self._didModify = True
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
753 elif key == 'a':
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
754 try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
755 # 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
756 self._RealGetContents()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
757 # 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
758 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
759 except BadZipFile:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
760 # 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
761 self.fp.seek(0, 2)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
762
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
763 # 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
764 # 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
765 self._didModify = True
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
766 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
767 if not self._filePassed:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
768 self.fp.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
769 self.fp = None
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
770 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
771
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
772 def __enter__(self):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
773 return self
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
774
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
775 def __exit__(self, type, value, traceback):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
776 self.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
777
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
778 def _GetContents(self):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
779 """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
780 is bad."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
781 try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
782 self._RealGetContents()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
783 except BadZipFile:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
784 if not self._filePassed:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
785 self.fp.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
786 self.fp = None
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
787 raise
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
788
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
789 def _RealGetContents(self):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
790 """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
791 fp = self.fp
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
792 try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
793 endrec = _EndRecData(fp)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
794 except IOError:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
795 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
796 if not endrec:
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 self.debug > 1:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
799 print(endrec)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
800 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
801 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
802 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
803
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
804 # "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
805 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
806 if endrec[_ECD_SIGNATURE] == stringEndArchive64:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
807 # 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
808 concat -= (sizeEndCentDir64 + sizeEndCentDir64Locator)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
809
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
810 if self.debug > 2:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
811 inferred = concat + offset_cd
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
812 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
813 # 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
814 self.start_dir = offset_cd + concat
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
815 fp.seek(self.start_dir, 0)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
816 data = fp.read(size_cd)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
817 fp = io.BytesIO(data)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
818 total = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
819 while total < size_cd:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
820 centdir = fp.read(sizeCentralDir)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
821 if centdir[0:4] != stringCentralDir:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
822 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
823 centdir = struct.unpack(structCentralDir, centdir)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
824 if self.debug > 2:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
825 print(centdir)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
826 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
827 flags = centdir[5]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
828 if flags & 0x800:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
829 # UTF-8 file names extension
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
830 filename = filename.decode('utf-8')
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
831 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
832 # Historical ZIP filename encoding
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
833 filename = filename.decode('cp437')
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
834 # 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
835 x = ZipInfo(filename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
836 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
837 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
838 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
839 (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
840 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
841 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
842 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
843 # 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
844 x._raw_time = t
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
845 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
846 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
847
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
848 x._decodeExtra()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
849 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
850 self.filelist.append(x)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
851 self.NameToInfo[x.filename] = x
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
852
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
853 # 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
854 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
855 + centdir[_CD_EXTRA_FIELD_LENGTH]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
856 + centdir[_CD_COMMENT_LENGTH])
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
857
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
858 if self.debug > 2:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
859 print("total", total)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
860
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
861
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
862 def namelist(self):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
863 """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
864 l = []
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
865 for data in self.filelist:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
866 l.append(data.filename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
867 return l
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
868
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
869 def infolist(self):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
870 """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
871 archive."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
872 return self.filelist
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
873
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
874 def printdir(self, file=None):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
875 """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
876 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
877 file=file)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
878 for zinfo in self.filelist:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
879 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
880 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
881 file=file)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
882
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
883 def testzip(self):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
884 """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
885 chunk_size = 2 ** 20
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
886 for zinfo in self.filelist:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
887 try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
888 # 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
889 # MemoryError with very large embedded files.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
890 f = self.open(zinfo.filename, "r")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
891 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
892 pass
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
893 except BadZipFile:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
894 return zinfo.filename
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
895
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
896 def getinfo(self, name):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
897 """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
898 info = self.NameToInfo.get(name)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
899 if info is None:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
900 raise KeyError(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
901 '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
902
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
903 return info
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 def setpassword(self, pwd):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
906 """Set default password for encrypted files."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
907 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
908 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
909 if pwd:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
910 self.pwd = pwd
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
911 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
912 self.pwd = None
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
913
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
914 def read(self, name, pwd=None):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
915 """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
916 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
917 return fp.read()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
918
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
919 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
920 """Return file-like object for 'name'."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
921 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
922 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
923 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
924 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
925 if not self.fp:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
926 raise RuntimeError(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
927 "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
928
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
929 # 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
930 # 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
931 if self._filePassed:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
932 zef_file = self.fp
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
933 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
934 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
935
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
936 # 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
937 if isinstance(name, ZipInfo):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
938 # 'name' is already an info object
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
939 zinfo = name
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
940 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
941 # Get info object for name
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
942 try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
943 zinfo = self.getinfo(name)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
944 except KeyError:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
945 if not self._filePassed:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
946 zef_file.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
947 raise
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
948 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
949
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
950 # Skip the file header:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
951 fheader = zef_file.read(sizeFileHeader)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
952 if fheader[0:4] != stringFileHeader:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
953 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
954
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
955 fheader = struct.unpack(structFileHeader, fheader)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
956 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
957 if fheader[_FH_EXTRA_FIELD_LENGTH]:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
958 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
959
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
960 if zinfo.flag_bits & 0x800:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
961 # UTF-8 filename
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
962 fname_str = fname.decode("utf-8")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
963 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
964 fname_str = fname.decode("cp437")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
965
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
966 if fname_str != zinfo.orig_filename:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
967 if not self._filePassed:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
968 zef_file.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
969 raise BadZipFile(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
970 '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
971 % (zinfo.orig_filename, fname))
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
972
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
973 # check for encrypted flag & handle password
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
974 is_encrypted = zinfo.flag_bits & 0x1
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
975 zd = None
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
976 if is_encrypted:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
977 if not pwd:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
978 pwd = self.pwd
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 if not self._filePassed:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
981 zef_file.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
982 raise RuntimeError("File %s is encrypted, "
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
983 "password required for extraction" % name)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
984
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
985 zd = _ZipDecrypter(pwd)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
986 # 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
987 # 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
988 # 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
989 # 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
990 # 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
991 header = zef_file.read(12)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
992 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
993 if zinfo.flag_bits & 0x8:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
994 # 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
995 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
996 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
997 # compare against the CRC otherwise
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
998 check_byte = (zinfo.CRC >> 24) & 0xff
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
999 if h[11] != check_byte:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1000 if not self._filePassed:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1001 zef_file.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1002 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
1003
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1004 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
1005 close_fileobj=not self._filePassed)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1006
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1007 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
1008 """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
1009 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
1010 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
1011 specify a different directory using `path'.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1012 """
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1013 if not isinstance(member, ZipInfo):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1014 member = self.getinfo(member)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1015
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1016 if path is None:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1017 path = os.getcwd()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1018
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1019 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
1020
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1021 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
1022 """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
1023 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
1024 `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
1025 by namelist().
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1026 """
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1027 if members is None:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1028 members = self.namelist()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1029
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1030 for zipinfo in members:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1031 self.extract(zipinfo, path, pwd)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1032
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1033 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
1034 """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
1035 file on the path targetpath.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1036 """
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1037 # build the destination pathname, replacing
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1038 # forward slashes to platform specific separators.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1039 # 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
1040 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
1041 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
1042 targetpath = targetpath[:-1]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1043
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1044 # 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
1045 if member.filename[0] == '/':
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1046 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
1047 else:
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)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1049
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1050 targetpath = os.path.normpath(targetpath)
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 # Create all upper directories if necessary.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1053 upperdirs = os.path.dirname(targetpath)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1054 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
1055 os.makedirs(upperdirs)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1056
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1057 if member.filename[-1] == '/':
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1058 if not os.path.isdir(targetpath):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1059 os.mkdir(targetpath)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1060 return targetpath
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1061
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1062 source = self.open(member, pwd=pwd)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1063 target = open(targetpath, "wb")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1064 shutil.copyfileobj(source, target)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1065 source.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1066 target.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1067
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1068 return targetpath
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 def _writecheck(self, zinfo):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1071 """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
1072 if zinfo.filename in self.NameToInfo:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1073 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
1074 print("Duplicate name:", zinfo.filename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1075 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
1076 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
1077 if not self.fp:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1078 raise RuntimeError(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1079 "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
1080 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
1081 raise RuntimeError(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1082 "Compression requires the (missing) zlib module")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1083 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
1084 raise RuntimeError(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1085 "Compression requires the (missing) bz2 module")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1086 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
1087 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
1088 if zinfo.file_size > ZIP64_LIMIT:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1089 if not self._allowZip64:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1090 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
1091 if zinfo.header_offset > ZIP64_LIMIT:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1092 if not self._allowZip64:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1093 raise LargeZipFile(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1094 "Zipfile size would require ZIP64 extensions")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1095
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1096 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
1097 """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
1098 arcname."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1099 if not self.fp:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1100 raise RuntimeError(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1101 "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
1102
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1103 st = os.stat(filename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1104 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
1105 mtime = time.localtime(st.st_mtime)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1106 date_time = mtime[0:6]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1107 # 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
1108 if arcname is None:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1109 arcname = filename
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1110 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
1111 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
1112 arcname = arcname[1:]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1113 if isdir:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1114 arcname += '/'
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1115 zinfo = ZipInfo(arcname, date_time)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1116 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
1117 if compress_type is None:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1118 zinfo.compress_type = self.compression
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1119 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1120 zinfo.compress_type = compress_type
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1121
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1122 zinfo.file_size = st.st_size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1123 zinfo.flag_bits = 0x00
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1124 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
1125
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1126 self._writecheck(zinfo)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1127 self._didModify = True
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1128
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1129 if isdir:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1130 zinfo.file_size = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1131 zinfo.compress_size = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1132 zinfo.CRC = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1133 self.filelist.append(zinfo)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1134 self.NameToInfo[zinfo.filename] = zinfo
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1135 self.fp.write(zinfo.FileHeader())
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1136 return
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1137
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1138 with open(filename, "rb") as fp:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1139 # 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
1140 zinfo.CRC = CRC = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1141 zinfo.compress_size = compress_size = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1142 zinfo.file_size = file_size = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1143 self.fp.write(zinfo.FileHeader())
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1144 if zinfo.compress_type == ZIP_DEFLATED:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1145 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
1146 zlib.DEFLATED, -15)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1147 elif zinfo.compress_type == ZIP_BZIP2:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1148 cmpr = bz2.BZ2Compressor()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1149 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1150 cmpr = None
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1151 while 1:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1152 buf = fp.read(1024 * 8)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1153 if not buf:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1154 break
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1155 file_size = file_size + len(buf)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1156 CRC = crc32(buf, CRC) & 0xffffffff
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1157 if cmpr:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1158 buf = cmpr.compress(buf)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1159 compress_size = compress_size + len(buf)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1160 self.fp.write(buf)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1161 if cmpr:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1162 buf = cmpr.flush()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1163 compress_size = compress_size + len(buf)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1164 self.fp.write(buf)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1165 zinfo.compress_size = compress_size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1166 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1167 zinfo.compress_size = file_size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1168 zinfo.CRC = CRC
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1169 zinfo.file_size = file_size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1170 # 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
1171 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
1172 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
1173 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
1174 zinfo.file_size))
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1175 self.fp.seek(position, 0)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1176 self.filelist.append(zinfo)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1177 self.NameToInfo[zinfo.filename] = zinfo
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1178
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1179 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
1180 """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
1181 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
1182 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
1183 '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
1184 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
1185 if isinstance(data, str):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1186 data = data.encode("utf-8")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1187 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
1188 zinfo = ZipInfo(filename=zinfo_or_arcname,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1189 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
1190 zinfo.compress_type = self.compression
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1191 zinfo.external_attr = 0o600 << 16
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1192 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1193 zinfo = zinfo_or_arcname
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1194
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1195 if not self.fp:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1196 raise RuntimeError(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1197 "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
1198
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1199 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
1200 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
1201 if compress_type is not None:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1202 zinfo.compress_type = compress_type
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1203
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1204 self._writecheck(zinfo)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1205 self._didModify = True
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1206 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
1207 if zinfo.compress_type == ZIP_DEFLATED:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1208 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
1209 zlib.DEFLATED, -15)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1210 data = co.compress(data) + co.flush()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1211 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
1212 elif zinfo.compress_type == ZIP_BZIP2:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1213 co = bz2.BZ2Compressor()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1214 data = co.compress(data) + co.flush()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1215 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
1216 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1217 zinfo.compress_size = zinfo.file_size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1218 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
1219 self.fp.write(zinfo.FileHeader())
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1220 self.fp.write(data)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1221 self.fp.flush()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1222 if zinfo.flag_bits & 0x08:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1223 # 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
1224 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
1225 zinfo.file_size))
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1226 self.filelist.append(zinfo)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1227 self.NameToInfo[zinfo.filename] = zinfo
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1228
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1229 def __del__(self):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1230 """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
1231 self.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1232
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1233 def close(self):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1234 """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
1235 records."""
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1236 if self.fp is None:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1237 return
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1238
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1239 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
1240 count = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1241 pos1 = self.fp.tell()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1242 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
1243 count = count + 1
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1244 dt = zinfo.date_time
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1245 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
1246 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
1247 extra = []
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1248 if zinfo.file_size > ZIP64_LIMIT \
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1249 or zinfo.compress_size > ZIP64_LIMIT:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1250 extra.append(zinfo.file_size)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1251 extra.append(zinfo.compress_size)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1252 file_size = 0xffffffff
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1253 compress_size = 0xffffffff
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1254 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1255 file_size = zinfo.file_size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1256 compress_size = zinfo.compress_size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1257
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1258 if zinfo.header_offset > ZIP64_LIMIT:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1259 extra.append(zinfo.header_offset)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1260 header_offset = 0xffffffff
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1261 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1262 header_offset = zinfo.header_offset
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1263
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1264 extra_data = zinfo.extra
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1265 if extra:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1266 # 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
1267 extra_data = struct.pack(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1268 '<HH' + 'Q'*len(extra),
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1269 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
1270
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1271 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
1272 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
1273 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1274 extract_version = zinfo.extract_version
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1275 create_version = zinfo.create_version
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1276
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1277 try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1278 filename, flag_bits = zinfo._encodeFilenameFlags()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1279 centdir = struct.pack(structCentralDir,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1280 stringCentralDir, create_version,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1281 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
1282 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
1283 zinfo.CRC, compress_size, file_size,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1284 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
1285 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
1286 header_offset)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1287 except DeprecationWarning:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1288 print((structCentralDir, stringCentralDir, create_version,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1289 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
1290 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
1291 zinfo.CRC, compress_size, file_size,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1292 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
1293 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
1294 header_offset), file=sys.stderr)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1295 raise
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1296 self.fp.write(centdir)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1297 self.fp.write(filename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1298 self.fp.write(extra_data)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1299 self.fp.write(zinfo.comment)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1300
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1301 pos2 = self.fp.tell()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1302 # Write end-of-zip-archive record
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1303 centDirCount = count
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1304 centDirSize = pos2 - pos1
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1305 centDirOffset = pos1
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1306 if (centDirCount >= ZIP_FILECOUNT_LIMIT or
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1307 centDirOffset > ZIP64_LIMIT or
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1308 centDirSize > ZIP64_LIMIT):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1309 # 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
1310 zip64endrec = struct.pack(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1311 structEndArchive64, stringEndArchive64,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1312 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
1313 centDirSize, centDirOffset)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1314 self.fp.write(zip64endrec)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1315
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1316 zip64locrec = struct.pack(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1317 structEndArchive64Locator,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1318 stringEndArchive64Locator, 0, pos2, 1)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1319 self.fp.write(zip64locrec)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1320 centDirCount = min(centDirCount, 0xFFFF)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1321 centDirSize = min(centDirSize, 0xFFFFFFFF)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1322 centDirOffset = min(centDirOffset, 0xFFFFFFFF)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1323
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1324 # check for valid comment length
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1325 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
1326 if self.debug > 0:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1327 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
1328 % ZIP_MAX_COMMENT
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1329 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
1330
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1331 endrec = struct.pack(structEndArchive, stringEndArchive,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1332 0, 0, centDirCount, centDirCount,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1333 centDirSize, centDirOffset, len(self.comment))
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1334 self.fp.write(endrec)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1335 self.fp.write(self.comment)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1336 self.fp.flush()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1337
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1338 if not self._filePassed:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1339 self.fp.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1340 self.fp = None
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1341
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1342
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1343 class PyZipFile(ZipFile):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1344 """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
1345
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1346 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
1347 allowZip64=False, optimize=-1):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1348 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
1349 allowZip64=allowZip64)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1350 self._optimize = optimize
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1351
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1352 def writepy(self, pathname, basename=""):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1353 """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
1354
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1355 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
1356 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
1357 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
1358 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
1359 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
1360 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
1361 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
1362 necessary.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1363 """
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1364 dir, name = os.path.split(pathname)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1365 if os.path.isdir(pathname):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1366 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
1367 if os.path.isfile(initname):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1368 # 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
1369 if basename:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1370 basename = "%s/%s" % (basename, name)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1371 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1372 basename = name
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1373 if self.debug:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1374 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
1375 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
1376 if self.debug:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1377 print("Adding", arcname)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1378 self.write(fname, arcname)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1379 dirlist = os.listdir(pathname)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1380 dirlist.remove("__init__.py")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1381 # 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
1382 for filename in dirlist:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1383 path = os.path.join(pathname, filename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1384 root, ext = os.path.splitext(filename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1385 if os.path.isdir(path):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1386 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
1387 # 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
1388 self.writepy(path, basename) # Recursive call
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1389 elif ext == ".py":
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1390 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
1391 basename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1392 if self.debug:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1393 print("Adding", arcname)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1394 self.write(fname, arcname)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1395 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1396 # 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
1397 if self.debug:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1398 print("Adding files from directory", pathname)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1399 for filename in os.listdir(pathname):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1400 path = os.path.join(pathname, filename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1401 root, ext = os.path.splitext(filename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1402 if ext == ".py":
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1403 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
1404 basename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1405 if self.debug:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1406 print("Adding", arcname)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1407 self.write(fname, arcname)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1408 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1409 if pathname[-3:] != ".py":
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1410 raise RuntimeError(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1411 '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
1412 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
1413 if self.debug:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1414 print("Adding file", arcname)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1415 self.write(fname, arcname)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1416
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1417 def _get_codename(self, pathname, basename):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1418 """Return (filename, archivename) for the path.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1419
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1420 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
1421 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
1422 /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
1423 """
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1424 def _compile(file, optimize=-1):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1425 import py_compile
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1426 if self.debug:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1427 print("Compiling", file)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1428 try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1429 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
1430 except py_compile.PyCompileError as error:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1431 print(err.msg)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1432 return False
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1433 return True
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1434
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1435 file_py = pathname + ".py"
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1436 file_pyc = pathname + ".pyc"
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1437 file_pyo = pathname + ".pyo"
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1438 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
1439 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
1440 if self._optimize == -1:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1441 # 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
1442 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
1443 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
1444 # Use .pyo file.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1445 arcname = fname = file_pyo
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1446 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
1447 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
1448 # Use .pyc file.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1449 arcname = fname = file_pyc
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1450 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
1451 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
1452 # 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
1453 # file name in the archive.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1454 fname = pycache_pyc
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1455 arcname = file_pyc
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1456 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
1457 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
1458 # 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
1459 # file name in the archive.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1460 fname = pycache_pyo
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1461 arcname = file_pyo
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1462 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1463 # 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
1464 if _compile(file_py):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1465 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
1466 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
1467 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1468 fname = arcname = file_py
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 # new mode: use given optimization level
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1471 if self._optimize == 0:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1472 fname = pycache_pyc
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1473 arcname = file_pyc
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1474 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1475 fname = pycache_pyo
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1476 arcname = file_pyo
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1477 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
1478 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
1479 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
1480 fname = arcname = file_py
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1481 archivename = os.path.split(arcname)[1]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1482 if basename:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1483 archivename = "%s/%s" % (basename, archivename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1484 return (fname, archivename)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1485
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1486
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1487 def main(args = None):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1488 import textwrap
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1489 USAGE=textwrap.dedent("""\
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1490 Usage:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1491 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
1492 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
1493 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
1494 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
1495 """)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1496 if args is None:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1497 args = sys.argv[1:]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1498
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1499 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
1500 print(USAGE)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1501 sys.exit(1)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1502
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1503 if args[0] == '-l':
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1504 if len(args) != 2:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1505 print(USAGE)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1506 sys.exit(1)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1507 zf = ZipFile(args[1], 'r')
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1508 zf.printdir()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1509 zf.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1510
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1511 elif args[0] == '-t':
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1512 if len(args) != 2:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1513 print(USAGE)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1514 sys.exit(1)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1515 zf = ZipFile(args[1], 'r')
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1516 badfile = zf.testzip()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1517 if badfile:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1518 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
1519 print("Done testing")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1520
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1521 elif args[0] == '-e':
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1522 if len(args) != 3:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1523 print(USAGE)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1524 sys.exit(1)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1525
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1526 zf = ZipFile(args[1], 'r')
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1527 out = args[2]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1528 for path in zf.namelist():
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1529 if path.startswith('./'):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1530 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
1531 else:
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)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1533
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1534 tgtdir = os.path.dirname(tgt)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1535 if not os.path.exists(tgtdir):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1536 os.makedirs(tgtdir)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1537 with open(tgt, 'wb') as fp:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1538 fp.write(zf.read(path))
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1539 zf.close()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1540
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1541 elif args[0] == '-c':
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1542 if len(args) < 3:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1543 print(USAGE)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1544 sys.exit(1)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1545
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1546 def addToZip(zf, path, zippath):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1547 if os.path.isfile(path):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1548 zf.write(path, zippath, ZIP_DEFLATED)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1549 elif os.path.isdir(path):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1550 for nm in os.listdir(path):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1551 addToZip(zf,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1552 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
1553 # else: ignore
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1554
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1555 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
1556 for src in args[2:]:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1557 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
1558
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1559 zf.close()
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 if __name__ == "__main__":
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1562 main()