blob: 3448c61795d7abaabe514e6f9d22494d1d106263 [file] [log] [blame]
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001"""
2Read and write ZIP files.
Guido van Rossumd6ca5462007-05-22 01:29:33 +00003
4XXX references to utf-8 need further investigation.
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005"""
Antoine Pitroua32f9a22010-01-27 21:18:57 +00006import io
Barry Warsaw28a691b2010-04-17 00:19:56 +00007import os
Antoine Pitroua32f9a22010-01-27 21:18:57 +00008import re
Barry Warsaw28a691b2010-04-17 00:19:56 +00009import imp
10import sys
11import time
12import stat
13import shutil
14import struct
15import binascii
16
Guido van Rossum32abe6f2000-03-31 17:30:02 +000017
18try:
Tim Peterse1190062001-01-15 03:34:38 +000019 import zlib # We may need its compression method
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000020 crc32 = zlib.crc32
Guido van Rossum9c673f32001-04-10 15:37:12 +000021except ImportError:
Guido van Rossum32abe6f2000-03-31 17:30:02 +000022 zlib = None
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000023 crc32 = binascii.crc32
Guido van Rossum32abe6f2000-03-31 17:30:02 +000024
Martin v. Löwisf6b16a42012-05-01 07:58:44 +020025try:
26 import bz2 # We may need its compression method
27except ImportError:
28 bz2 = None
29
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +020030try:
31 import lzma # We may need its compression method
32except ImportError:
33 lzma = None
34
Martin v. Löwisf6b16a42012-05-01 07:58:44 +020035__all__ = ["BadZipFile", "BadZipfile", "error",
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +020036 "ZIP_STORED", "ZIP_DEFLATED", "ZIP_BZIP2", "ZIP_LZMA",
Georg Brandl4d540882010-10-28 06:42:33 +000037 "is_zipfile", "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile"]
Skip Montanaro40fc1602001-03-01 04:27:19 +000038
Georg Brandl4d540882010-10-28 06:42:33 +000039class BadZipFile(Exception):
Guido van Rossum32abe6f2000-03-31 17:30:02 +000040 pass
Thomas Wouters0e3f5912006-08-11 14:57:12 +000041
42
43class LargeZipFile(Exception):
44 """
45 Raised when writing a zipfile, the zipfile requires ZIP64 extensions
46 and those extensions are disabled.
47 """
48
Georg Brandl4d540882010-10-28 06:42:33 +000049error = BadZipfile = BadZipFile # Pre-3.2 compatibility names
50
Guido van Rossum32abe6f2000-03-31 17:30:02 +000051
Amaury Forgeot d'Arc0c3f8a42009-01-17 16:42:26 +000052ZIP64_LIMIT = (1 << 31) - 1
Martin v. Löwisb09b8442008-07-03 14:13:42 +000053ZIP_FILECOUNT_LIMIT = 1 << 16
54ZIP_MAX_COMMENT = (1 << 16) - 1
Thomas Wouters0e3f5912006-08-11 14:57:12 +000055
Guido van Rossum32abe6f2000-03-31 17:30:02 +000056# constants for Zip file compression methods
57ZIP_STORED = 0
58ZIP_DEFLATED = 8
Martin v. Löwisf6b16a42012-05-01 07:58:44 +020059ZIP_BZIP2 = 12
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +020060ZIP_LZMA = 14
Guido van Rossum32abe6f2000-03-31 17:30:02 +000061# Other ZIP compression methods not supported
62
Martin v. Löwisf6b16a42012-05-01 07:58:44 +020063DEFAULT_VERSION = 20
64ZIP64_VERSION = 45
65BZIP2_VERSION = 46
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +020066LZMA_VERSION = 63
Martin v. Löwisd099b562012-05-01 14:08:22 +020067# we recognize (but not necessarily support) all features up to that version
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +020068MAX_EXTRACT_VERSION = 63
Martin v. Löwisf6b16a42012-05-01 07:58:44 +020069
Martin v. Löwisb09b8442008-07-03 14:13:42 +000070# Below are some formats and associated data for reading/writing headers using
71# the struct module. The names and structures of headers/records are those used
72# in the PKWARE description of the ZIP file format:
73# http://www.pkware.com/documents/casestudies/APPNOTE.TXT
74# (URL valid as of January 2008)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000075
Martin v. Löwisb09b8442008-07-03 14:13:42 +000076# The "end of central directory" structure, magic number, size, and indices
77# (section V.I in the format document)
Georg Brandl2ee470f2008-07-16 12:55:28 +000078structEndArchive = b"<4s4H2LH"
79stringEndArchive = b"PK\005\006"
80sizeEndCentDir = struct.calcsize(structEndArchive)
Martin v. Löwisb09b8442008-07-03 14:13:42 +000081
82_ECD_SIGNATURE = 0
83_ECD_DISK_NUMBER = 1
84_ECD_DISK_START = 2
85_ECD_ENTRIES_THIS_DISK = 3
86_ECD_ENTRIES_TOTAL = 4
87_ECD_SIZE = 5
88_ECD_OFFSET = 6
89_ECD_COMMENT_SIZE = 7
90# These last two indices are not part of the structure as defined in the
91# spec, but they are used internally by this module as a convenience
92_ECD_COMMENT = 8
93_ECD_LOCATION = 9
94
95# The "central directory" structure, magic number, size, and indices
96# of entries in the structure (section V.F in the format document)
97structCentralDir = "<4s4B4HL2L5H2L"
Georg Brandl2ee470f2008-07-16 12:55:28 +000098stringCentralDir = b"PK\001\002"
Martin v. Löwisb09b8442008-07-03 14:13:42 +000099sizeCentralDir = struct.calcsize(structCentralDir)
100
Fred Drake3e038e52001-02-28 17:56:26 +0000101# indexes of entries in the central directory structure
102_CD_SIGNATURE = 0
103_CD_CREATE_VERSION = 1
104_CD_CREATE_SYSTEM = 2
105_CD_EXTRACT_VERSION = 3
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000106_CD_EXTRACT_SYSTEM = 4
Fred Drake3e038e52001-02-28 17:56:26 +0000107_CD_FLAG_BITS = 5
108_CD_COMPRESS_TYPE = 6
109_CD_TIME = 7
110_CD_DATE = 8
111_CD_CRC = 9
112_CD_COMPRESSED_SIZE = 10
113_CD_UNCOMPRESSED_SIZE = 11
114_CD_FILENAME_LENGTH = 12
115_CD_EXTRA_FIELD_LENGTH = 13
116_CD_COMMENT_LENGTH = 14
117_CD_DISK_NUMBER_START = 15
118_CD_INTERNAL_FILE_ATTRIBUTES = 16
119_CD_EXTERNAL_FILE_ATTRIBUTES = 17
120_CD_LOCAL_HEADER_OFFSET = 18
121
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000122# The "local file header" structure, magic number, size, and indices
123# (section V.A in the format document)
124structFileHeader = "<4s2B4HL2L2H"
Georg Brandl2ee470f2008-07-16 12:55:28 +0000125stringFileHeader = b"PK\003\004"
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000126sizeFileHeader = struct.calcsize(structFileHeader)
127
Fred Drake3e038e52001-02-28 17:56:26 +0000128_FH_SIGNATURE = 0
129_FH_EXTRACT_VERSION = 1
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000130_FH_EXTRACT_SYSTEM = 2
Fred Drake3e038e52001-02-28 17:56:26 +0000131_FH_GENERAL_PURPOSE_FLAG_BITS = 3
132_FH_COMPRESSION_METHOD = 4
133_FH_LAST_MOD_TIME = 5
134_FH_LAST_MOD_DATE = 6
135_FH_CRC = 7
136_FH_COMPRESSED_SIZE = 8
137_FH_UNCOMPRESSED_SIZE = 9
138_FH_FILENAME_LENGTH = 10
139_FH_EXTRA_FIELD_LENGTH = 11
140
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000141# The "Zip64 end of central directory locator" structure, magic number, and size
Georg Brandl2ee470f2008-07-16 12:55:28 +0000142structEndArchive64Locator = "<4sLQL"
143stringEndArchive64Locator = b"PK\x06\x07"
144sizeEndCentDir64Locator = struct.calcsize(structEndArchive64Locator)
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000145
146# The "Zip64 end of central directory" record, magic number, size, and indices
147# (section V.G in the format document)
Georg Brandl2ee470f2008-07-16 12:55:28 +0000148structEndArchive64 = "<4sQ2H2L4Q"
149stringEndArchive64 = b"PK\x06\x06"
150sizeEndCentDir64 = struct.calcsize(structEndArchive64)
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000151
152_CD64_SIGNATURE = 0
153_CD64_DIRECTORY_RECSIZE = 1
154_CD64_CREATE_VERSION = 2
155_CD64_EXTRACT_VERSION = 3
156_CD64_DISK_NUMBER = 4
157_CD64_DISK_NUMBER_START = 5
158_CD64_NUMBER_ENTRIES_THIS_DISK = 6
159_CD64_NUMBER_ENTRIES_TOTAL = 7
160_CD64_DIRECTORY_SIZE = 8
161_CD64_OFFSET_START_CENTDIR = 9
162
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000163def _check_zipfile(fp):
Guido van Rossum32abe6f2000-03-31 17:30:02 +0000164 try:
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000165 if _EndRecData(fp):
166 return True # file has correct magic number
Fred Drake7e473802001-05-11 19:52:57 +0000167 except IOError:
Guido van Rossum32abe6f2000-03-31 17:30:02 +0000168 pass
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000169 return False
Guido van Rossum32abe6f2000-03-31 17:30:02 +0000170
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000171def is_zipfile(filename):
172 """Quickly see if a file is a ZIP file by checking the magic number.
173
174 The filename argument may be a file or file-like object too.
175 """
176 result = False
177 try:
178 if hasattr(filename, "read"):
179 result = _check_zipfile(fp=filename)
180 else:
181 with open(filename, "rb") as fp:
182 result = _check_zipfile(fp)
183 except IOError:
184 pass
185 return result
186
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000187def _EndRecData64(fpin, offset, endrec):
188 """
189 Read the ZIP64 end-of-archive records and use that to update endrec
190 """
Georg Brandl268e4d42010-10-14 06:59:45 +0000191 try:
192 fpin.seek(offset - sizeEndCentDir64Locator, 2)
193 except IOError:
194 # If the seek fails, the file is not large enough to contain a ZIP64
195 # end-of-archive record, so just return the end record we were given.
196 return endrec
197
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000198 data = fpin.read(sizeEndCentDir64Locator)
Serhiy Storchakad2b15272013-01-31 15:27:07 +0200199 if len(data) != sizeEndCentDir64Locator:
200 return endrec
Georg Brandl2ee470f2008-07-16 12:55:28 +0000201 sig, diskno, reloff, disks = struct.unpack(structEndArchive64Locator, data)
202 if sig != stringEndArchive64Locator:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000203 return endrec
204
205 if diskno != 0 or disks != 1:
Éric Araujoae2d8322010-10-28 13:49:17 +0000206 raise BadZipFile("zipfiles that span multiple disks are not supported")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000207
208 # Assume no 'zip64 extensible data'
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000209 fpin.seek(offset - sizeEndCentDir64Locator - sizeEndCentDir64, 2)
210 data = fpin.read(sizeEndCentDir64)
Serhiy Storchakad2b15272013-01-31 15:27:07 +0200211 if len(data) != sizeEndCentDir64:
212 return endrec
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000213 sig, sz, create_version, read_version, disk_num, disk_dir, \
214 dircount, dircount2, dirsize, diroffset = \
Georg Brandl2ee470f2008-07-16 12:55:28 +0000215 struct.unpack(structEndArchive64, data)
216 if sig != stringEndArchive64:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000217 return endrec
218
219 # Update the original endrec using data from the ZIP64 record
Antoine Pitrou9e4fdf42008-09-05 23:43:02 +0000220 endrec[_ECD_SIGNATURE] = sig
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000221 endrec[_ECD_DISK_NUMBER] = disk_num
222 endrec[_ECD_DISK_START] = disk_dir
223 endrec[_ECD_ENTRIES_THIS_DISK] = dircount
224 endrec[_ECD_ENTRIES_TOTAL] = dircount2
225 endrec[_ECD_SIZE] = dirsize
226 endrec[_ECD_OFFSET] = diroffset
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000227 return endrec
228
229
Martin v. Löwis6f6873b2002-10-13 13:54:50 +0000230def _EndRecData(fpin):
231 """Return data from the "End of Central Directory" record, or None.
232
233 The data is a list of the nine items in the ZIP "End of central dir"
234 record followed by a tenth item, the file seek offset of this record."""
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000235
236 # Determine file size
237 fpin.seek(0, 2)
238 filesize = fpin.tell()
239
240 # Check to see if this is ZIP file with no archive comment (the
241 # "end of central directory" structure should be the last item in the
242 # file if this is the case).
Amaury Forgeot d'Arcbc347802009-07-28 22:18:57 +0000243 try:
244 fpin.seek(-sizeEndCentDir, 2)
245 except IOError:
246 return None
Martin v. Löwis6f6873b2002-10-13 13:54:50 +0000247 data = fpin.read()
Serhiy Storchakad2b15272013-01-31 15:27:07 +0200248 if (len(data) == sizeEndCentDir and
249 data[0:4] == stringEndArchive and
250 data[-2:] == b"\000\000"):
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000251 # the signature is correct and there's no comment, unpack structure
Georg Brandl2ee470f2008-07-16 12:55:28 +0000252 endrec = struct.unpack(structEndArchive, data)
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000253 endrec=list(endrec)
254
255 # Append a blank comment and record start offset
256 endrec.append(b"")
257 endrec.append(filesize - sizeEndCentDir)
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000258
Amaury Forgeot d'Arcd3fb4bb2009-01-18 00:29:02 +0000259 # Try to read the "Zip64 end of central directory" structure
260 return _EndRecData64(fpin, -sizeEndCentDir, endrec)
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000261
262 # Either this is not a ZIP file, or it is a ZIP file with an archive
263 # comment. Search the end of the file for the "end of central directory"
264 # record signature. The comment is the last item in the ZIP file and may be
265 # up to 64K long. It is assumed that the "end of central directory" magic
266 # number does not appear in the comment.
267 maxCommentStart = max(filesize - (1 << 16) - sizeEndCentDir, 0)
268 fpin.seek(maxCommentStart, 0)
Martin v. Löwis6f6873b2002-10-13 13:54:50 +0000269 data = fpin.read()
Georg Brandl2ee470f2008-07-16 12:55:28 +0000270 start = data.rfind(stringEndArchive)
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000271 if start >= 0:
272 # found the magic number; attempt to unpack and interpret
273 recData = data[start:start+sizeEndCentDir]
Serhiy Storchakad2b15272013-01-31 15:27:07 +0200274 if len(recData) != sizeEndCentDir:
275 # Zip file is corrupted.
276 return None
Georg Brandl2ee470f2008-07-16 12:55:28 +0000277 endrec = list(struct.unpack(structEndArchive, recData))
R David Murray4fbb9db2011-06-09 15:50:51 -0400278 commentSize = endrec[_ECD_COMMENT_SIZE] #as claimed by the zip file
279 comment = data[start+sizeEndCentDir:start+sizeEndCentDir+commentSize]
280 endrec.append(comment)
281 endrec.append(maxCommentStart + start)
Amaury Forgeot d'Arcd3fb4bb2009-01-18 00:29:02 +0000282
R David Murray4fbb9db2011-06-09 15:50:51 -0400283 # Try to read the "Zip64 end of central directory" structure
284 return _EndRecData64(fpin, maxCommentStart + start - filesize,
285 endrec)
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000286
287 # Unable to find a valid end of central directory structure
Serhiy Storchakad2b15272013-01-31 15:27:07 +0200288 return None
Martin v. Löwis6f6873b2002-10-13 13:54:50 +0000289
Fred Drake484d7352000-10-02 21:14:52 +0000290
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000291class ZipInfo (object):
Fred Drake484d7352000-10-02 21:14:52 +0000292 """Class with attributes describing each file in the ZIP archive."""
293
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000294 __slots__ = (
295 'orig_filename',
296 'filename',
297 'date_time',
298 'compress_type',
299 'comment',
300 'extra',
301 'create_system',
302 'create_version',
303 'extract_version',
304 'reserved',
305 'flag_bits',
306 'volume',
307 'internal_attr',
308 'external_attr',
309 'header_offset',
310 'CRC',
311 'compress_size',
312 'file_size',
Christian Heimesfdab48e2008-01-20 09:06:41 +0000313 '_raw_time',
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000314 )
315
Guido van Rossum32abe6f2000-03-31 17:30:02 +0000316 def __init__(self, filename="NoName", date_time=(1980,1,1,0,0,0)):
Greg Ward8e36d282003-06-18 00:53:06 +0000317 self.orig_filename = filename # Original file name in archive
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000318
319 # Terminate the file name at the first null byte. Null bytes in file
320 # names are used as tricks by viruses in archives.
Greg Ward8e36d282003-06-18 00:53:06 +0000321 null_byte = filename.find(chr(0))
322 if null_byte >= 0:
323 filename = filename[0:null_byte]
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000324 # This is used to ensure paths in generated ZIP files always use
325 # forward slashes as the directory separator, as required by the
326 # ZIP format specification.
327 if os.sep != "/" and os.sep in filename:
Greg Ward8e36d282003-06-18 00:53:06 +0000328 filename = filename.replace(os.sep, "/")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000329
Greg Ward8e36d282003-06-18 00:53:06 +0000330 self.filename = filename # Normalized file name
Tim Peterse1190062001-01-15 03:34:38 +0000331 self.date_time = date_time # year, month, day, hour, min, sec
Senthil Kumaran29fa9d42011-10-20 01:46:00 +0800332
333 if date_time[0] < 1980:
334 raise ValueError('ZIP does not support timestamps before 1980')
335
Guido van Rossum32abe6f2000-03-31 17:30:02 +0000336 # Standard values:
Tim Peterse1190062001-01-15 03:34:38 +0000337 self.compress_type = ZIP_STORED # Type of compression for the file
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000338 self.comment = b"" # Comment for each file
339 self.extra = b"" # ZIP extra data
Martin v. Löwis00756902006-02-05 17:09:41 +0000340 if sys.platform == 'win32':
341 self.create_system = 0 # System which created ZIP archive
342 else:
343 # Assume everything else is unix-y
344 self.create_system = 3 # System which created ZIP archive
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200345 self.create_version = DEFAULT_VERSION # Version which created ZIP archive
346 self.extract_version = DEFAULT_VERSION # Version needed to extract archive
Tim Peterse1190062001-01-15 03:34:38 +0000347 self.reserved = 0 # Must be zero
348 self.flag_bits = 0 # ZIP flag bits
349 self.volume = 0 # Volume number of file header
350 self.internal_attr = 0 # Internal attributes
351 self.external_attr = 0 # External file attributes
Guido van Rossum32abe6f2000-03-31 17:30:02 +0000352 # Other attributes are set by class ZipFile:
Tim Peterse1190062001-01-15 03:34:38 +0000353 # header_offset Byte offset to the file header
Tim Peterse1190062001-01-15 03:34:38 +0000354 # CRC CRC-32 of the uncompressed file
355 # compress_size Size of the compressed file
356 # file_size Size of the uncompressed file
Guido van Rossum32abe6f2000-03-31 17:30:02 +0000357
Serhiy Storchaka182d7cd2013-01-15 00:31:39 +0200358 def FileHeader(self, zip64=None):
Fred Drake484d7352000-10-02 21:14:52 +0000359 """Return the per-file header as a string."""
Guido van Rossum32abe6f2000-03-31 17:30:02 +0000360 dt = self.date_time
361 dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2]
Tim Peters3caca232001-12-06 06:23:26 +0000362 dostime = dt[3] << 11 | dt[4] << 5 | (dt[5] // 2)
Guido van Rossum32abe6f2000-03-31 17:30:02 +0000363 if self.flag_bits & 0x08:
Tim Peterse1190062001-01-15 03:34:38 +0000364 # Set these to zero because we write them after the file data
365 CRC = compress_size = file_size = 0
Guido van Rossum32abe6f2000-03-31 17:30:02 +0000366 else:
Tim Peterse1190062001-01-15 03:34:38 +0000367 CRC = self.CRC
368 compress_size = self.compress_size
369 file_size = self.file_size
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000370
371 extra = self.extra
372
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200373 min_version = 0
Serhiy Storchaka182d7cd2013-01-15 00:31:39 +0200374 if zip64 is None:
375 zip64 = file_size > ZIP64_LIMIT or compress_size > ZIP64_LIMIT
376 if zip64:
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000377 fmt = '<HHQQ'
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000378 extra = extra + struct.pack(fmt,
379 1, struct.calcsize(fmt)-4, file_size, compress_size)
Serhiy Storchaka182d7cd2013-01-15 00:31:39 +0200380 if file_size > ZIP64_LIMIT or compress_size > ZIP64_LIMIT:
381 if not zip64:
382 raise LargeZipFile("Filesize would require ZIP64 extensions")
383 # File is larger than what fits into a 4 byte integer,
384 # fall back to the ZIP64 extension
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000385 file_size = 0xffffffff
386 compress_size = 0xffffffff
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200387 min_version = ZIP64_VERSION
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000388
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200389 if self.compress_type == ZIP_BZIP2:
390 min_version = max(BZIP2_VERSION, min_version)
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +0200391 elif self.compress_type == ZIP_LZMA:
392 min_version = max(LZMA_VERSION, min_version)
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200393
394 self.extract_version = max(min_version, self.extract_version)
395 self.create_version = max(min_version, self.create_version)
Martin v. Löwis8570f6a2008-05-05 17:44:38 +0000396 filename, flag_bits = self._encodeFilenameFlags()
Georg Brandl2ee470f2008-07-16 12:55:28 +0000397 header = struct.pack(structFileHeader, stringFileHeader,
Martin v. Löwis8570f6a2008-05-05 17:44:38 +0000398 self.extract_version, self.reserved, flag_bits,
Guido van Rossum32abe6f2000-03-31 17:30:02 +0000399 self.compress_type, dostime, dosdate, CRC,
400 compress_size, file_size,
Martin v. Löwis8570f6a2008-05-05 17:44:38 +0000401 len(filename), len(extra))
402 return header + filename + extra
403
404 def _encodeFilenameFlags(self):
405 try:
406 return self.filename.encode('ascii'), self.flag_bits
407 except UnicodeEncodeError:
408 return self.filename.encode('utf-8'), self.flag_bits | 0x800
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000409
410 def _decodeExtra(self):
411 # Try to decode the extra field.
412 extra = self.extra
413 unpack = struct.unpack
414 while extra:
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000415 tp, ln = unpack('<HH', extra[:4])
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000416 if tp == 1:
417 if ln >= 24:
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000418 counts = unpack('<QQQ', extra[4:28])
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000419 elif ln == 16:
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000420 counts = unpack('<QQ', extra[4:20])
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000421 elif ln == 8:
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000422 counts = unpack('<Q', extra[4:12])
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000423 elif ln == 0:
424 counts = ()
425 else:
Collin Winterce36ad82007-08-30 01:19:48 +0000426 raise RuntimeError("Corrupt extra field %s"%(ln,))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000427
428 idx = 0
429
430 # ZIP64 extension (large files and/or large archives)
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000431 if self.file_size in (0xffffffffffffffff, 0xffffffff):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000432 self.file_size = counts[idx]
433 idx += 1
434
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000435 if self.compress_size == 0xFFFFFFFF:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000436 self.compress_size = counts[idx]
437 idx += 1
438
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000439 if self.header_offset == 0xffffffff:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000440 old = self.header_offset
441 self.header_offset = counts[idx]
442 idx+=1
443
444 extra = extra[ln+4:]
Guido van Rossum32abe6f2000-03-31 17:30:02 +0000445
446
Thomas Wouterscf297e42007-02-23 15:07:44 +0000447class _ZipDecrypter:
448 """Class to handle decryption of files stored within a ZIP archive.
449
450 ZIP supports a password-based form of encryption. Even though known
451 plaintext attacks have been found against it, it is still useful
Christian Heimesfdab48e2008-01-20 09:06:41 +0000452 to be able to get data out of such a file.
Thomas Wouterscf297e42007-02-23 15:07:44 +0000453
454 Usage:
455 zd = _ZipDecrypter(mypwd)
456 plain_char = zd(cypher_char)
457 plain_text = map(zd, cypher_text)
458 """
459
460 def _GenerateCRCTable():
461 """Generate a CRC-32 table.
462
463 ZIP encryption uses the CRC32 one-byte primitive for scrambling some
464 internal keys. We noticed that a direct implementation is faster than
465 relying on binascii.crc32().
466 """
467 poly = 0xedb88320
468 table = [0] * 256
469 for i in range(256):
470 crc = i
471 for j in range(8):
472 if crc & 1:
473 crc = ((crc >> 1) & 0x7FFFFFFF) ^ poly
474 else:
475 crc = ((crc >> 1) & 0x7FFFFFFF)
476 table[i] = crc
477 return table
478 crctable = _GenerateCRCTable()
479
480 def _crc32(self, ch, crc):
481 """Compute the CRC32 primitive on one byte."""
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000482 return ((crc >> 8) & 0xffffff) ^ self.crctable[(crc ^ ch) & 0xff]
Thomas Wouterscf297e42007-02-23 15:07:44 +0000483
484 def __init__(self, pwd):
485 self.key0 = 305419896
486 self.key1 = 591751049
487 self.key2 = 878082192
488 for p in pwd:
489 self._UpdateKeys(p)
490
491 def _UpdateKeys(self, c):
492 self.key0 = self._crc32(c, self.key0)
493 self.key1 = (self.key1 + (self.key0 & 255)) & 4294967295
494 self.key1 = (self.key1 * 134775813 + 1) & 4294967295
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000495 self.key2 = self._crc32((self.key1 >> 24) & 255, self.key2)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000496
497 def __call__(self, c):
498 """Decrypt a single character."""
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000499 assert isinstance(c, int)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000500 k = self.key2 | 2
501 c = c ^ (((k * (k^1)) >> 8) & 255)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000502 self._UpdateKeys(c)
503 return c
504
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200505
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +0200506class LZMACompressor:
507
508 def __init__(self):
509 self._comp = None
510
511 def _init(self):
Nadeem Vawdaa425c3d2012-06-21 23:36:48 +0200512 props = lzma._encode_filter_properties({'id': lzma.FILTER_LZMA1})
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +0200513 self._comp = lzma.LZMACompressor(lzma.FORMAT_RAW, filters=[
Nadeem Vawdaa425c3d2012-06-21 23:36:48 +0200514 lzma._decode_filter_properties(lzma.FILTER_LZMA1, props)
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +0200515 ])
516 return struct.pack('<BBH', 9, 4, len(props)) + props
517
518 def compress(self, data):
519 if self._comp is None:
520 return self._init() + self._comp.compress(data)
521 return self._comp.compress(data)
522
523 def flush(self):
524 if self._comp is None:
525 return self._init() + self._comp.flush()
526 return self._comp.flush()
527
528
529class LZMADecompressor:
530
531 def __init__(self):
532 self._decomp = None
533 self._unconsumed = b''
534 self.eof = False
535
536 def decompress(self, data):
537 if self._decomp is None:
538 self._unconsumed += data
539 if len(self._unconsumed) <= 4:
540 return b''
541 psize, = struct.unpack('<H', self._unconsumed[2:4])
542 if len(self._unconsumed) <= 4 + psize:
543 return b''
544
545 self._decomp = lzma.LZMADecompressor(lzma.FORMAT_RAW, filters=[
Nadeem Vawdaa425c3d2012-06-21 23:36:48 +0200546 lzma._decode_filter_properties(lzma.FILTER_LZMA1,
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +0200547 self._unconsumed[4:4 + psize])
548 ])
549 data = self._unconsumed[4 + psize:]
550 del self._unconsumed
551
552 result = self._decomp.decompress(data)
553 self.eof = self._decomp.eof
554 return result
555
556
557compressor_names = {
558 0: 'store',
559 1: 'shrink',
560 2: 'reduce',
561 3: 'reduce',
562 4: 'reduce',
563 5: 'reduce',
564 6: 'implode',
565 7: 'tokenize',
566 8: 'deflate',
567 9: 'deflate64',
568 10: 'implode',
569 12: 'bzip2',
570 14: 'lzma',
571 18: 'terse',
572 19: 'lz77',
573 97: 'wavpack',
574 98: 'ppmd',
575}
576
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200577def _check_compression(compression):
578 if compression == ZIP_STORED:
579 pass
580 elif compression == ZIP_DEFLATED:
581 if not zlib:
582 raise RuntimeError(
583 "Compression requires the (missing) zlib module")
584 elif compression == ZIP_BZIP2:
585 if not bz2:
586 raise RuntimeError(
587 "Compression requires the (missing) bz2 module")
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +0200588 elif compression == ZIP_LZMA:
589 if not lzma:
590 raise RuntimeError(
591 "Compression requires the (missing) lzma module")
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200592 else:
593 raise RuntimeError("That compression method is not supported")
594
595
596def _get_compressor(compress_type):
597 if compress_type == ZIP_DEFLATED:
598 return zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION,
599 zlib.DEFLATED, -15)
600 elif compress_type == ZIP_BZIP2:
601 return bz2.BZ2Compressor()
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +0200602 elif compress_type == ZIP_LZMA:
603 return LZMACompressor()
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200604 else:
605 return None
606
607
608def _get_decompressor(compress_type):
Martin v. Löwisb3260f02012-05-01 08:38:01 +0200609 if compress_type == ZIP_STORED:
610 return None
611 elif compress_type == ZIP_DEFLATED:
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200612 return zlib.decompressobj(-15)
613 elif compress_type == ZIP_BZIP2:
614 return bz2.BZ2Decompressor()
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +0200615 elif compress_type == ZIP_LZMA:
616 return LZMADecompressor()
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200617 else:
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +0200618 descr = compressor_names.get(compress_type)
Martin v. Löwisb3260f02012-05-01 08:38:01 +0200619 if descr:
620 raise NotImplementedError("compression type %d (%s)" % (compress_type, descr))
621 else:
622 raise NotImplementedError("compression type %d" % (compress_type,))
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200623
624
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000625class ZipExtFile(io.BufferedIOBase):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000626 """File-like object for reading an archive member.
627 Is returned by ZipFile.open().
628 """
629
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000630 # Max size supported by decompressor.
631 MAX_N = 1 << 31 - 1
Guido van Rossumd8faa362007-04-27 19:54:29 +0000632
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000633 # Read from compressed files in 4k blocks.
634 MIN_READ_SIZE = 4096
Guido van Rossumd8faa362007-04-27 19:54:29 +0000635
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000636 # Search for universal newlines or line chunks.
637 PATTERN = re.compile(br'^(?P<chunk>[^\r\n]+)|(?P<newline>\n|\r\n?)')
638
Łukasz Langae94980a2010-11-22 23:31:26 +0000639 def __init__(self, fileobj, mode, zipinfo, decrypter=None,
640 close_fileobj=False):
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000641 self._fileobj = fileobj
642 self._decrypter = decrypter
Łukasz Langae94980a2010-11-22 23:31:26 +0000643 self._close_fileobj = close_fileobj
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000644
Ezio Melotti92b47432010-01-28 01:44:41 +0000645 self._compress_type = zipinfo.compress_type
Ezio Melotti92b47432010-01-28 01:44:41 +0000646 self._compress_left = zipinfo.compress_size
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200647 self._left = zipinfo.file_size
Ezio Melotti92b47432010-01-28 01:44:41 +0000648
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200649 self._decompressor = _get_decompressor(self._compress_type)
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000650
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200651 self._eof = False
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000652 self._readbuffer = b''
653 self._offset = 0
654
655 self._universal = 'U' in mode
656 self.newlines = None
657
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000658 # Adjust read size for encrypted files since the first 12 bytes
659 # are for the encryption/password information.
660 if self._decrypter is not None:
661 self._compress_left -= 12
662
663 self.mode = mode
Guido van Rossumd8faa362007-04-27 19:54:29 +0000664 self.name = zipinfo.filename
665
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +0000666 if hasattr(zipinfo, 'CRC'):
667 self._expected_crc = zipinfo.CRC
668 self._running_crc = crc32(b'') & 0xffffffff
669 else:
670 self._expected_crc = None
671
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000672 def readline(self, limit=-1):
673 """Read and return a line from the stream.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000674
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000675 If limit is specified, at most limit bytes will be read.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000676 """
Guido van Rossumd8faa362007-04-27 19:54:29 +0000677
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000678 if not self._universal and limit < 0:
679 # Shortcut common case - newline found in buffer.
680 i = self._readbuffer.find(b'\n', self._offset) + 1
681 if i > 0:
682 line = self._readbuffer[self._offset: i]
683 self._offset = i
684 return line
Guido van Rossumd8faa362007-04-27 19:54:29 +0000685
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000686 if not self._universal:
687 return io.BufferedIOBase.readline(self, limit)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000688
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000689 line = b''
690 while limit < 0 or len(line) < limit:
691 readahead = self.peek(2)
692 if readahead == b'':
693 return line
Guido van Rossumd8faa362007-04-27 19:54:29 +0000694
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000695 #
696 # Search for universal newlines or line chunks.
697 #
698 # The pattern returns either a line chunk or a newline, but not
699 # both. Combined with peek(2), we are assured that the sequence
700 # '\r\n' is always retrieved completely and never split into
701 # separate newlines - '\r', '\n' due to coincidental readaheads.
702 #
703 match = self.PATTERN.search(readahead)
704 newline = match.group('newline')
705 if newline is not None:
706 if self.newlines is None:
707 self.newlines = []
708 if newline not in self.newlines:
709 self.newlines.append(newline)
710 self._offset += len(newline)
711 return line + b'\n'
Guido van Rossumd8faa362007-04-27 19:54:29 +0000712
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000713 chunk = match.group('chunk')
714 if limit >= 0:
715 chunk = chunk[: limit - len(line)]
Guido van Rossumd8faa362007-04-27 19:54:29 +0000716
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000717 self._offset += len(chunk)
718 line += chunk
Guido van Rossumd8faa362007-04-27 19:54:29 +0000719
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000720 return line
721
722 def peek(self, n=1):
723 """Returns buffered bytes without advancing the position."""
724 if n > len(self._readbuffer) - self._offset:
725 chunk = self.read(n)
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200726 if len(chunk) > self._offset:
727 self._readbuffer = chunk + self._readbuffer[self._offset:]
728 self._offset = 0
729 else:
730 self._offset -= len(chunk)
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000731
732 # Return up to 512 bytes to reduce allocation overhead for tight loops.
733 return self._readbuffer[self._offset: self._offset + 512]
734
735 def readable(self):
736 return True
737
738 def read(self, n=-1):
739 """Read and return up to n bytes.
740 If the argument is omitted, None, or negative, data is read and returned until EOF is reached..
Guido van Rossumd8faa362007-04-27 19:54:29 +0000741 """
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200742 if n is None or n < 0:
743 buf = self._readbuffer[self._offset:]
744 self._readbuffer = b''
745 self._offset = 0
746 while not self._eof:
747 buf += self._read1(self.MAX_N)
748 return buf
Guido van Rossumd8faa362007-04-27 19:54:29 +0000749
Antoine Pitrou78157b32012-06-23 16:44:48 +0200750 end = n + self._offset
751 if end < len(self._readbuffer):
752 buf = self._readbuffer[self._offset:end]
753 self._offset = end
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200754 return buf
755
Antoine Pitrou78157b32012-06-23 16:44:48 +0200756 n = end - len(self._readbuffer)
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200757 buf = self._readbuffer[self._offset:]
758 self._readbuffer = b''
759 self._offset = 0
760 while n > 0 and not self._eof:
761 data = self._read1(n)
762 if n < len(data):
763 self._readbuffer = data
764 self._offset = n
765 buf += data[:n]
766 break
767 buf += data
768 n -= len(data)
769 return buf
770
771 def _update_crc(self, newdata):
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +0000772 # Update the CRC using the given data.
773 if self._expected_crc is None:
774 # No need to compute the CRC if we don't have a reference value
775 return
776 self._running_crc = crc32(newdata, self._running_crc) & 0xffffffff
777 # Check the CRC if we're at the end of the file
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200778 if self._eof and self._running_crc != self._expected_crc:
Georg Brandl4d540882010-10-28 06:42:33 +0000779 raise BadZipFile("Bad CRC-32 for file %r" % self.name)
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +0000780
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000781 def read1(self, n):
782 """Read up to n bytes with at most one read() system call."""
Guido van Rossumd8faa362007-04-27 19:54:29 +0000783
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200784 if n is None or n < 0:
785 buf = self._readbuffer[self._offset:]
786 self._readbuffer = b''
787 self._offset = 0
788 data = self._read1(self.MAX_N)
789 buf += data
790 return buf
Guido van Rossumd8faa362007-04-27 19:54:29 +0000791
Antoine Pitrou78157b32012-06-23 16:44:48 +0200792 end = n + self._offset
793 if end < len(self._readbuffer):
794 buf = self._readbuffer[self._offset:end]
795 self._offset = end
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200796 return buf
797
Antoine Pitrou78157b32012-06-23 16:44:48 +0200798 n = end - len(self._readbuffer)
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200799 buf = self._readbuffer[self._offset:]
800 self._readbuffer = b''
801 self._offset = 0
802 if n > 0:
803 data = self._read1(n)
804 if n < len(data):
805 self._readbuffer = data
806 self._offset = n
807 data = data[:n]
808 buf += data
809 return buf
810
811 def _read1(self, n):
812 # Read up to n compressed bytes with at most one read() system call,
813 # decrypt and decompress them.
814 if self._eof or n <= 0:
815 return b''
Guido van Rossumd8faa362007-04-27 19:54:29 +0000816
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000817 # Read from file.
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200818 if self._compress_type == ZIP_DEFLATED:
819 ## Handle unconsumed data.
820 data = self._decompressor.unconsumed_tail
821 if n > len(data):
822 data += self._read2(n - len(data))
823 else:
824 data = self._read2(n)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000825
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200826 if self._compress_type == ZIP_STORED:
827 self._eof = self._compress_left <= 0
828 elif self._compress_type == ZIP_DEFLATED:
829 n = max(n, self.MIN_READ_SIZE)
830 data = self._decompressor.decompress(data, n)
831 self._eof = (self._decompressor.eof or
832 self._compress_left <= 0 and
833 not self._decompressor.unconsumed_tail)
834 if self._eof:
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000835 data += self._decompressor.flush()
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200836 else:
837 data = self._decompressor.decompress(data)
838 self._eof = self._decompressor.eof or self._compress_left <= 0
Guido van Rossumd8faa362007-04-27 19:54:29 +0000839
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200840 data = data[:self._left]
841 self._left -= len(data)
842 if self._left <= 0:
843 self._eof = True
844 self._update_crc(data)
845 return data
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000846
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200847 def _read2(self, n):
848 if self._compress_left <= 0:
849 return b''
850
851 n = max(n, self.MIN_READ_SIZE)
852 n = min(n, self._compress_left)
853
854 data = self._fileobj.read(n)
855 self._compress_left -= len(data)
856
857 if self._decrypter is not None:
858 data = bytes(map(self._decrypter, data))
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000859 return data
Guido van Rossumd8faa362007-04-27 19:54:29 +0000860
Łukasz Langae94980a2010-11-22 23:31:26 +0000861 def close(self):
862 try:
863 if self._close_fileobj:
864 self._fileobj.close()
865 finally:
866 super().close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000867
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000868
Guido van Rossum32abe6f2000-03-31 17:30:02 +0000869class ZipFile:
Tim Petersa19a1682001-03-29 04:36:09 +0000870 """ Class with methods to open, read, write, close, list zip files.
871
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000872 z = ZipFile(file, mode="r", compression=ZIP_STORED, allowZip64=False)
Tim Petersa19a1682001-03-29 04:36:09 +0000873
Fred Drake3d9091e2001-03-26 15:49:24 +0000874 file: Either the path to the file, or a file-like object.
875 If it is a path, the file will be opened and closed by ZipFile.
876 mode: The mode can be either read "r", write "w" or append "a".
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +0200877 compression: ZIP_STORED (no compression), ZIP_DEFLATED (requires zlib),
878 ZIP_BZIP2 (requires bz2) or ZIP_LZMA (requires lzma).
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000879 allowZip64: if True ZipFile will create files with ZIP64 extensions when
880 needed, otherwise it will raise an exception when this would
881 be necessary.
882
Fred Drake3d9091e2001-03-26 15:49:24 +0000883 """
Fred Drake484d7352000-10-02 21:14:52 +0000884
Fred Drake90eac282001-02-28 05:29:34 +0000885 fp = None # Set here since __del__ checks it
Gregory P. Smith09aa7522013-02-03 00:36:32 -0800886 _windows_illegal_name_trans_table = None
Fred Drake90eac282001-02-28 05:29:34 +0000887
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000888 def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=False):
Fred Drake484d7352000-10-02 21:14:52 +0000889 """Open the ZIP file with mode read "r", write "w" or append "a"."""
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000890 if mode not in ("r", "w", "a"):
891 raise RuntimeError('ZipFile() requires mode "r", "w", or "a"')
892
Martin v. Löwisf6b16a42012-05-01 07:58:44 +0200893 _check_compression(compression)
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000894
895 self._allowZip64 = allowZip64
896 self._didModify = False
Tim Peterse1190062001-01-15 03:34:38 +0000897 self.debug = 0 # Level of printing: 0 through 3
898 self.NameToInfo = {} # Find file info given name
899 self.filelist = [] # List of ZipInfo instances for archive
900 self.compression = compression # Method of compression
Raymond Hettinger2ca7c192005-02-16 09:27:49 +0000901 self.mode = key = mode.replace('b', '')[0]
Thomas Wouterscf297e42007-02-23 15:07:44 +0000902 self.pwd = None
R David Murrayf50b38a2012-04-12 18:44:58 -0400903 self._comment = b''
Tim Petersa19a1682001-03-29 04:36:09 +0000904
Fred Drake3d9091e2001-03-26 15:49:24 +0000905 # Check if we were passed a file-like object
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000906 if isinstance(file, str):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000907 # No, it's a filename
Fred Drake3d9091e2001-03-26 15:49:24 +0000908 self._filePassed = 0
909 self.filename = file
910 modeDict = {'r' : 'rb', 'w': 'wb', 'a' : 'r+b'}
Thomas Wouterscf297e42007-02-23 15:07:44 +0000911 try:
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000912 self.fp = io.open(file, modeDict[mode])
Thomas Wouterscf297e42007-02-23 15:07:44 +0000913 except IOError:
914 if mode == 'a':
915 mode = key = 'w'
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000916 self.fp = io.open(file, modeDict[mode])
Thomas Wouterscf297e42007-02-23 15:07:44 +0000917 else:
918 raise
Fred Drake3d9091e2001-03-26 15:49:24 +0000919 else:
920 self._filePassed = 1
921 self.fp = file
922 self.filename = getattr(file, 'name', None)
Tim Petersa19a1682001-03-29 04:36:09 +0000923
Antoine Pitrou17babc52012-11-17 23:50:08 +0100924 try:
925 if key == 'r':
Martin v. Löwis6f6873b2002-10-13 13:54:50 +0000926 self._RealGetContents()
Antoine Pitrou17babc52012-11-17 23:50:08 +0100927 elif key == 'w':
Georg Brandl268e4d42010-10-14 06:59:45 +0000928 # set the modified flag so central directory gets written
929 # even if no files are added to the archive
930 self._didModify = True
Antoine Pitrou17babc52012-11-17 23:50:08 +0100931 elif key == 'a':
932 try:
933 # See if file is a zip file
934 self._RealGetContents()
935 # seek to start of directory and overwrite
936 self.fp.seek(self.start_dir, 0)
937 except BadZipFile:
938 # file is not a zip file, just append
939 self.fp.seek(0, 2)
940
941 # set the modified flag so central directory gets written
942 # even if no files are added to the archive
943 self._didModify = True
944 else:
945 raise RuntimeError('Mode must be "r", "w" or "a"')
946 except:
947 fp = self.fp
948 self.fp = None
Tim Peters7d3bad62001-04-04 18:56:49 +0000949 if not self._filePassed:
Antoine Pitrou17babc52012-11-17 23:50:08 +0100950 fp.close()
951 raise
Guido van Rossum32abe6f2000-03-31 17:30:02 +0000952
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000953 def __enter__(self):
954 return self
955
956 def __exit__(self, type, value, traceback):
957 self.close()
958
Tim Peters7d3bad62001-04-04 18:56:49 +0000959 def _RealGetContents(self):
Fred Drake484d7352000-10-02 21:14:52 +0000960 """Read in the table of contents for the ZIP file."""
Guido van Rossum32abe6f2000-03-31 17:30:02 +0000961 fp = self.fp
Georg Brandl268e4d42010-10-14 06:59:45 +0000962 try:
963 endrec = _EndRecData(fp)
964 except IOError:
Georg Brandl4d540882010-10-28 06:42:33 +0000965 raise BadZipFile("File is not a zip file")
Martin v. Löwis6f6873b2002-10-13 13:54:50 +0000966 if not endrec:
Georg Brandl4d540882010-10-28 06:42:33 +0000967 raise BadZipFile("File is not a zip file")
Guido van Rossum32abe6f2000-03-31 17:30:02 +0000968 if self.debug > 1:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000969 print(endrec)
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000970 size_cd = endrec[_ECD_SIZE] # bytes in central directory
971 offset_cd = endrec[_ECD_OFFSET] # offset of central directory
R David Murrayf50b38a2012-04-12 18:44:58 -0400972 self._comment = endrec[_ECD_COMMENT] # archive comment
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000973
Guido van Rossum32abe6f2000-03-31 17:30:02 +0000974 # "concat" is zero, unless zip was concatenated to another file
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000975 concat = endrec[_ECD_LOCATION] - size_cd - offset_cd
Antoine Pitrou9e4fdf42008-09-05 23:43:02 +0000976 if endrec[_ECD_SIGNATURE] == stringEndArchive64:
977 # If Zip64 extension structures are present, account for them
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000978 concat -= (sizeEndCentDir64 + sizeEndCentDir64Locator)
979
Guido van Rossum32abe6f2000-03-31 17:30:02 +0000980 if self.debug > 2:
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000981 inferred = concat + offset_cd
982 print("given, inferred, offset", offset_cd, inferred, concat)
Guido van Rossum32abe6f2000-03-31 17:30:02 +0000983 # self.start_dir: Position of start of central directory
984 self.start_dir = offset_cd + concat
985 fp.seek(self.start_dir, 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000986 data = fp.read(size_cd)
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000987 fp = io.BytesIO(data)
Guido van Rossum32abe6f2000-03-31 17:30:02 +0000988 total = 0
989 while total < size_cd:
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000990 centdir = fp.read(sizeCentralDir)
Serhiy Storchakad2b15272013-01-31 15:27:07 +0200991 if len(centdir) != sizeCentralDir:
992 raise BadZipFile("Truncated central directory")
Guido van Rossum32abe6f2000-03-31 17:30:02 +0000993 centdir = struct.unpack(structCentralDir, centdir)
Serhiy Storchakad2b15272013-01-31 15:27:07 +0200994 if centdir[_CD_SIGNATURE] != stringCentralDir:
995 raise BadZipFile("Bad magic number for central directory")
Guido van Rossum32abe6f2000-03-31 17:30:02 +0000996 if self.debug > 2:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000997 print(centdir)
Fred Drake3e038e52001-02-28 17:56:26 +0000998 filename = fp.read(centdir[_CD_FILENAME_LENGTH])
Martin v. Löwis8570f6a2008-05-05 17:44:38 +0000999 flags = centdir[5]
1000 if flags & 0x800:
1001 # UTF-8 file names extension
1002 filename = filename.decode('utf-8')
1003 else:
1004 # Historical ZIP filename encoding
1005 filename = filename.decode('cp437')
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001006 # Create ZipInfo instance to store file information
Martin v. Löwis8570f6a2008-05-05 17:44:38 +00001007 x = ZipInfo(filename)
Fred Drake3e038e52001-02-28 17:56:26 +00001008 x.extra = fp.read(centdir[_CD_EXTRA_FIELD_LENGTH])
1009 x.comment = fp.read(centdir[_CD_COMMENT_LENGTH])
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001010 x.header_offset = centdir[_CD_LOCAL_HEADER_OFFSET]
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001011 (x.create_version, x.create_system, x.extract_version, x.reserved,
1012 x.flag_bits, x.compress_type, t, d,
1013 x.CRC, x.compress_size, x.file_size) = centdir[1:12]
Martin v. Löwisd099b562012-05-01 14:08:22 +02001014 if x.extract_version > MAX_EXTRACT_VERSION:
1015 raise NotImplementedError("zip file version %.1f" %
1016 (x.extract_version / 10))
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001017 x.volume, x.internal_attr, x.external_attr = centdir[15:18]
1018 # Convert date/time code to (year, month, day, hour, min, sec)
Christian Heimesfdab48e2008-01-20 09:06:41 +00001019 x._raw_time = t
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001020 x.date_time = ( (d>>9)+1980, (d>>5)&0xF, d&0x1F,
Fred Drake414ca662000-06-13 18:49:53 +00001021 t>>11, (t>>5)&0x3F, (t&0x1F) * 2 )
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001022
1023 x._decodeExtra()
1024 x.header_offset = x.header_offset + concat
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001025 self.filelist.append(x)
1026 self.NameToInfo[x.filename] = x
Martin v. Löwisb09b8442008-07-03 14:13:42 +00001027
1028 # update total bytes read from central directory
1029 total = (total + sizeCentralDir + centdir[_CD_FILENAME_LENGTH]
1030 + centdir[_CD_EXTRA_FIELD_LENGTH]
1031 + centdir[_CD_COMMENT_LENGTH])
1032
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001033 if self.debug > 2:
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001034 print("total", total)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001035
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001036
1037 def namelist(self):
Fred Drake484d7352000-10-02 21:14:52 +00001038 """Return a list of file names in the archive."""
Ezio Melotti006917e2012-04-16 21:34:24 -06001039 return [data.filename for data in self.filelist]
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001040
1041 def infolist(self):
Fred Drake484d7352000-10-02 21:14:52 +00001042 """Return a list of class ZipInfo instances for files in the
1043 archive."""
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001044 return self.filelist
1045
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001046 def printdir(self, file=None):
Fred Drake484d7352000-10-02 21:14:52 +00001047 """Print a table of contents for the zip file."""
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001048 print("%-46s %19s %12s" % ("File Name", "Modified ", "Size"),
1049 file=file)
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001050 for zinfo in self.filelist:
Guido van Rossum7736b5b2008-01-15 21:44:53 +00001051 date = "%d-%02d-%02d %02d:%02d:%02d" % zinfo.date_time[:6]
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001052 print("%-46s %s %12d" % (zinfo.filename, date, zinfo.file_size),
1053 file=file)
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001054
1055 def testzip(self):
Fred Drake484d7352000-10-02 21:14:52 +00001056 """Read all the files and check the CRC."""
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001057 chunk_size = 2 ** 20
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001058 for zinfo in self.filelist:
1059 try:
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00001060 # Read by chunks, to avoid an OverflowError or a
1061 # MemoryError with very large embedded files.
Antoine Pitrou17babc52012-11-17 23:50:08 +01001062 with self.open(zinfo.filename, "r") as f:
1063 while f.read(chunk_size): # Check CRC-32
1064 pass
Georg Brandl4d540882010-10-28 06:42:33 +00001065 except BadZipFile:
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001066 return zinfo.filename
1067
1068 def getinfo(self, name):
Fred Drake484d7352000-10-02 21:14:52 +00001069 """Return the instance of ZipInfo given 'name'."""
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001070 info = self.NameToInfo.get(name)
1071 if info is None:
1072 raise KeyError(
1073 'There is no item named %r in the archive' % name)
1074
1075 return info
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001076
Thomas Wouterscf297e42007-02-23 15:07:44 +00001077 def setpassword(self, pwd):
1078 """Set default password for encrypted files."""
R. David Murray8d855d82010-12-21 21:53:37 +00001079 if pwd and not isinstance(pwd, bytes):
1080 raise TypeError("pwd: expected bytes, got %s" % type(pwd))
1081 if pwd:
1082 self.pwd = pwd
1083 else:
1084 self.pwd = None
Thomas Wouterscf297e42007-02-23 15:07:44 +00001085
R David Murrayf50b38a2012-04-12 18:44:58 -04001086 @property
1087 def comment(self):
1088 """The comment text associated with the ZIP file."""
1089 return self._comment
1090
1091 @comment.setter
1092 def comment(self, comment):
1093 if not isinstance(comment, bytes):
1094 raise TypeError("comment: expected bytes, got %s" % type(comment))
1095 # check for valid comment length
1096 if len(comment) >= ZIP_MAX_COMMENT:
1097 if self.debug:
1098 print('Archive comment is too long; truncating to %d bytes'
1099 % ZIP_MAX_COMMENT)
1100 comment = comment[:ZIP_MAX_COMMENT]
1101 self._comment = comment
1102 self._didModify = True
1103
Thomas Wouterscf297e42007-02-23 15:07:44 +00001104 def read(self, name, pwd=None):
Fred Drake484d7352000-10-02 21:14:52 +00001105 """Return file bytes (as a string) for name."""
Benjamin Petersond285bdb2010-10-31 17:57:22 +00001106 with self.open(name, "r", pwd) as fp:
1107 return fp.read()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001108
1109 def open(self, name, mode="r", pwd=None):
1110 """Return file-like object for 'name'."""
1111 if mode not in ("r", "U", "rU"):
Collin Winterce36ad82007-08-30 01:19:48 +00001112 raise RuntimeError('open() requires mode "r", "U", or "rU"')
R. David Murray8d855d82010-12-21 21:53:37 +00001113 if pwd and not isinstance(pwd, bytes):
1114 raise TypeError("pwd: expected bytes, got %s" % type(pwd))
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001115 if not self.fp:
Collin Winterce36ad82007-08-30 01:19:48 +00001116 raise RuntimeError(
1117 "Attempt to read ZIP archive that was already closed")
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001118
Guido van Rossumd8faa362007-04-27 19:54:29 +00001119 # Only open a new file for instances where we were not
1120 # given a file object in the constructor
1121 if self._filePassed:
1122 zef_file = self.fp
1123 else:
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001124 zef_file = io.open(self.filename, 'rb')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001125
Antoine Pitrou17babc52012-11-17 23:50:08 +01001126 try:
1127 # Make sure we have an info object
1128 if isinstance(name, ZipInfo):
1129 # 'name' is already an info object
1130 zinfo = name
1131 else:
1132 # Get info object for name
Łukasz Langaa9f054b2010-11-23 00:15:02 +00001133 zinfo = self.getinfo(name)
Antoine Pitrou17babc52012-11-17 23:50:08 +01001134 zef_file.seek(zinfo.header_offset, 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001135
Antoine Pitrou17babc52012-11-17 23:50:08 +01001136 # Skip the file header:
1137 fheader = zef_file.read(sizeFileHeader)
Serhiy Storchakad2b15272013-01-31 15:27:07 +02001138 if len(fheader) != sizeFileHeader:
1139 raise BadZipFile("Truncated file header")
1140 fheader = struct.unpack(structFileHeader, fheader)
1141 if fheader[_FH_SIGNATURE] != stringFileHeader:
Antoine Pitrou17babc52012-11-17 23:50:08 +01001142 raise BadZipFile("Bad magic number for file header")
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001143
Antoine Pitrou17babc52012-11-17 23:50:08 +01001144 fname = zef_file.read(fheader[_FH_FILENAME_LENGTH])
1145 if fheader[_FH_EXTRA_FIELD_LENGTH]:
1146 zef_file.read(fheader[_FH_EXTRA_FIELD_LENGTH])
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001147
Antoine Pitrou8572da52012-11-17 23:52:05 +01001148 if zinfo.flag_bits & 0x20:
1149 # Zip 2.7: compressed patched data
1150 raise NotImplementedError("compressed patched data (flag bit 5)")
Martin v. Löwis2a2ce322012-05-01 08:44:08 +02001151
Antoine Pitrou8572da52012-11-17 23:52:05 +01001152 if zinfo.flag_bits & 0x40:
1153 # strong encryption
1154 raise NotImplementedError("strong encryption (flag bit 6)")
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +02001155
Antoine Pitrou17babc52012-11-17 23:50:08 +01001156 if zinfo.flag_bits & 0x800:
1157 # UTF-8 filename
1158 fname_str = fname.decode("utf-8")
1159 else:
1160 fname_str = fname.decode("cp437")
Georg Brandl5ba11de2011-01-01 10:09:32 +00001161
Antoine Pitrou17babc52012-11-17 23:50:08 +01001162 if fname_str != zinfo.orig_filename:
1163 raise BadZipFile(
1164 'File name in directory %r and header %r differ.'
1165 % (zinfo.orig_filename, fname))
1166
1167 # check for encrypted flag & handle password
1168 is_encrypted = zinfo.flag_bits & 0x1
1169 zd = None
1170 if is_encrypted:
1171 if not pwd:
1172 pwd = self.pwd
1173 if not pwd:
1174 raise RuntimeError("File %s is encrypted, password "
1175 "required for extraction" % name)
1176
1177 zd = _ZipDecrypter(pwd)
1178 # The first 12 bytes in the cypher stream is an encryption header
1179 # used to strengthen the algorithm. The first 11 bytes are
1180 # completely random, while the 12th contains the MSB of the CRC,
1181 # or the MSB of the file time depending on the header type
1182 # and is used to check the correctness of the password.
1183 header = zef_file.read(12)
1184 h = list(map(zd, header[0:12]))
1185 if zinfo.flag_bits & 0x8:
1186 # compare against the file type from extended local headers
1187 check_byte = (zinfo._raw_time >> 8) & 0xff
1188 else:
1189 # compare against the CRC otherwise
1190 check_byte = (zinfo.CRC >> 24) & 0xff
1191 if h[11] != check_byte:
1192 raise RuntimeError("Bad password for file", name)
1193
1194 return ZipExtFile(zef_file, mode, zinfo, zd,
1195 close_fileobj=not self._filePassed)
1196 except:
Łukasz Langaa9f054b2010-11-23 00:15:02 +00001197 if not self._filePassed:
1198 zef_file.close()
Antoine Pitrou17babc52012-11-17 23:50:08 +01001199 raise
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001200
Christian Heimes790c8232008-01-07 21:14:23 +00001201 def extract(self, member, path=None, pwd=None):
1202 """Extract a member from the archive to the current working directory,
1203 using its full name. Its file information is extracted as accurately
1204 as possible. `member' may be a filename or a ZipInfo object. You can
1205 specify a different directory using `path'.
1206 """
1207 if not isinstance(member, ZipInfo):
1208 member = self.getinfo(member)
1209
1210 if path is None:
1211 path = os.getcwd()
1212
1213 return self._extract_member(member, path, pwd)
1214
1215 def extractall(self, path=None, members=None, pwd=None):
1216 """Extract all members from the archive to the current working
1217 directory. `path' specifies a different directory to extract to.
1218 `members' is optional and must be a subset of the list returned
1219 by namelist().
1220 """
1221 if members is None:
1222 members = self.namelist()
1223
1224 for zipinfo in members:
1225 self.extract(zipinfo, path, pwd)
1226
Gregory P. Smith09aa7522013-02-03 00:36:32 -08001227 @classmethod
1228 def _sanitize_windows_name(cls, arcname, pathsep):
1229 """Replace bad characters and remove trailing dots from parts."""
1230 table = cls._windows_illegal_name_trans_table
1231 if not table:
1232 illegal = ':<>|"?*'
1233 table = str.maketrans(illegal, '_' * len(illegal))
1234 cls._windows_illegal_name_trans_table = table
1235 arcname = arcname.translate(table)
1236 # remove trailing dots
1237 arcname = (x.rstrip('.') for x in arcname.split(pathsep))
1238 # rejoin, removing empty parts.
1239 arcname = pathsep.join(x for x in arcname if x)
1240 return arcname
1241
Christian Heimes790c8232008-01-07 21:14:23 +00001242 def _extract_member(self, member, targetpath, pwd):
1243 """Extract the ZipInfo object 'member' to a physical
1244 file on the path targetpath.
1245 """
1246 # build the destination pathname, replacing
1247 # forward slashes to platform specific separators.
Gregory P. Smithb47acbf2013-02-01 11:22:43 -08001248 arcname = member.filename.replace('/', os.path.sep)
Christian Heimes790c8232008-01-07 21:14:23 +00001249
Gregory P. Smithb47acbf2013-02-01 11:22:43 -08001250 if os.path.altsep:
1251 arcname = arcname.replace(os.path.altsep, os.path.sep)
1252 # interpret absolute pathname as relative, remove drive letter or
1253 # UNC path, redundant separators, "." and ".." components.
1254 arcname = os.path.splitdrive(arcname)[1]
Gregory P. Smith09aa7522013-02-03 00:36:32 -08001255 invalid_path_parts = ('', os.path.curdir, os.path.pardir)
Gregory P. Smithb47acbf2013-02-01 11:22:43 -08001256 arcname = os.path.sep.join(x for x in arcname.split(os.path.sep)
Gregory P. Smith09aa7522013-02-03 00:36:32 -08001257 if x not in invalid_path_parts)
Gregory P. Smithb47acbf2013-02-01 11:22:43 -08001258 if os.path.sep == '\\':
Serhiy Storchakae5e64442013-02-02 19:50:59 +02001259 # filter illegal characters on Windows
Gregory P. Smith09aa7522013-02-03 00:36:32 -08001260 arcname = self._sanitize_windows_name(arcname, os.path.sep)
Christian Heimes790c8232008-01-07 21:14:23 +00001261
Gregory P. Smithb47acbf2013-02-01 11:22:43 -08001262 targetpath = os.path.join(targetpath, arcname)
Christian Heimes790c8232008-01-07 21:14:23 +00001263 targetpath = os.path.normpath(targetpath)
1264
1265 # Create all upper directories if necessary.
1266 upperdirs = os.path.dirname(targetpath)
1267 if upperdirs and not os.path.exists(upperdirs):
1268 os.makedirs(upperdirs)
1269
Martin v. Löwis59e47792009-01-24 14:10:07 +00001270 if member.filename[-1] == '/':
Martin v. Löwis70ccd162009-05-24 19:47:22 +00001271 if not os.path.isdir(targetpath):
1272 os.mkdir(targetpath)
Martin v. Löwis59e47792009-01-24 14:10:07 +00001273 return targetpath
1274
Antoine Pitrou17babc52012-11-17 23:50:08 +01001275 with self.open(member, pwd=pwd) as source, \
1276 open(targetpath, "wb") as target:
1277 shutil.copyfileobj(source, target)
Christian Heimes790c8232008-01-07 21:14:23 +00001278
1279 return targetpath
1280
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001281 def _writecheck(self, zinfo):
Fred Drake484d7352000-10-02 21:14:52 +00001282 """Check for errors before writing a file to the archive."""
Raymond Hettinger54f02222002-06-01 14:18:47 +00001283 if zinfo.filename in self.NameToInfo:
Tim Peterse1190062001-01-15 03:34:38 +00001284 if self.debug: # Warning for duplicate names
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001285 print("Duplicate name:", zinfo.filename)
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001286 if self.mode not in ("w", "a"):
Collin Winterce36ad82007-08-30 01:19:48 +00001287 raise RuntimeError('write() requires mode "w" or "a"')
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001288 if not self.fp:
Collin Winterce36ad82007-08-30 01:19:48 +00001289 raise RuntimeError(
1290 "Attempt to write ZIP archive that was already closed")
Martin v. Löwisf6b16a42012-05-01 07:58:44 +02001291 _check_compression(zinfo.compress_type)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001292 if zinfo.file_size > ZIP64_LIMIT:
1293 if not self._allowZip64:
1294 raise LargeZipFile("Filesize would require ZIP64 extensions")
1295 if zinfo.header_offset > ZIP64_LIMIT:
1296 if not self._allowZip64:
Collin Winterce36ad82007-08-30 01:19:48 +00001297 raise LargeZipFile(
1298 "Zipfile size would require ZIP64 extensions")
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001299
1300 def write(self, filename, arcname=None, compress_type=None):
Fred Drake484d7352000-10-02 21:14:52 +00001301 """Put the bytes from filename into the archive under the name
1302 arcname."""
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001303 if not self.fp:
1304 raise RuntimeError(
1305 "Attempt to write to ZIP archive that was already closed")
1306
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001307 st = os.stat(filename)
Martin v. Löwis59e47792009-01-24 14:10:07 +00001308 isdir = stat.S_ISDIR(st.st_mode)
Raymond Hettinger32200ae2002-06-01 19:51:15 +00001309 mtime = time.localtime(st.st_mtime)
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001310 date_time = mtime[0:6]
1311 # Create ZipInfo instance to store file information
1312 if arcname is None:
Georg Brandl8f7c54e2006-02-20 08:40:38 +00001313 arcname = filename
1314 arcname = os.path.normpath(os.path.splitdrive(arcname)[1])
1315 while arcname[0] in (os.sep, os.altsep):
1316 arcname = arcname[1:]
Martin v. Löwis59e47792009-01-24 14:10:07 +00001317 if isdir:
1318 arcname += '/'
Georg Brandl8f7c54e2006-02-20 08:40:38 +00001319 zinfo = ZipInfo(arcname, date_time)
Guido van Rossume2a383d2007-01-15 16:59:06 +00001320 zinfo.external_attr = (st[0] & 0xFFFF) << 16 # Unix attributes
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001321 if compress_type is None:
Tim Peterse1190062001-01-15 03:34:38 +00001322 zinfo.compress_type = self.compression
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001323 else:
Tim Peterse1190062001-01-15 03:34:38 +00001324 zinfo.compress_type = compress_type
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001325
1326 zinfo.file_size = st.st_size
Finn Bock03a3bb82001-09-05 18:40:33 +00001327 zinfo.flag_bits = 0x00
Tim Peterse1190062001-01-15 03:34:38 +00001328 zinfo.header_offset = self.fp.tell() # Start of header bytes
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +02001329 if zinfo.compress_type == ZIP_LZMA:
1330 # Compressed data includes an end-of-stream (EOS) marker
1331 zinfo.flag_bits |= 0x02
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001332
1333 self._writecheck(zinfo)
1334 self._didModify = True
Martin v. Löwis59e47792009-01-24 14:10:07 +00001335
1336 if isdir:
1337 zinfo.file_size = 0
1338 zinfo.compress_size = 0
1339 zinfo.CRC = 0
1340 self.filelist.append(zinfo)
1341 self.NameToInfo[zinfo.filename] = zinfo
Serhiy Storchaka182d7cd2013-01-15 00:31:39 +02001342 self.fp.write(zinfo.FileHeader(False))
Martin v. Löwis59e47792009-01-24 14:10:07 +00001343 return
1344
Martin v. Löwisf6b16a42012-05-01 07:58:44 +02001345 cmpr = _get_compressor(zinfo.compress_type)
Benjamin Petersonfa0d7032009-06-01 22:42:33 +00001346 with open(filename, "rb") as fp:
1347 # Must overwrite CRC and sizes with correct data later
1348 zinfo.CRC = CRC = 0
1349 zinfo.compress_size = compress_size = 0
Serhiy Storchaka182d7cd2013-01-15 00:31:39 +02001350 # Compressed size can be larger than uncompressed size
1351 zip64 = self._allowZip64 and \
1352 zinfo.file_size * 1.05 > ZIP64_LIMIT
1353 self.fp.write(zinfo.FileHeader(zip64))
Serhiy Storchaka182d7cd2013-01-15 00:31:39 +02001354 file_size = 0
Benjamin Petersonfa0d7032009-06-01 22:42:33 +00001355 while 1:
1356 buf = fp.read(1024 * 8)
1357 if not buf:
1358 break
1359 file_size = file_size + len(buf)
1360 CRC = crc32(buf, CRC) & 0xffffffff
1361 if cmpr:
1362 buf = cmpr.compress(buf)
1363 compress_size = compress_size + len(buf)
1364 self.fp.write(buf)
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001365 if cmpr:
1366 buf = cmpr.flush()
1367 compress_size = compress_size + len(buf)
1368 self.fp.write(buf)
1369 zinfo.compress_size = compress_size
1370 else:
1371 zinfo.compress_size = file_size
1372 zinfo.CRC = CRC
1373 zinfo.file_size = file_size
Serhiy Storchaka182d7cd2013-01-15 00:31:39 +02001374 if not zip64 and self._allowZip64:
1375 if file_size > ZIP64_LIMIT:
1376 raise RuntimeError('File size has increased during compressing')
1377 if compress_size > ZIP64_LIMIT:
1378 raise RuntimeError('Compressed size larger than uncompressed size')
1379 # Seek backwards and write file header (which will now include
1380 # correct CRC and file sizes)
Tim Petersb64bec32001-09-18 02:26:39 +00001381 position = self.fp.tell() # Preserve current position in file
Serhiy Storchaka182d7cd2013-01-15 00:31:39 +02001382 self.fp.seek(zinfo.header_offset, 0)
1383 self.fp.write(zinfo.FileHeader(zip64))
Finn Bock03a3bb82001-09-05 18:40:33 +00001384 self.fp.seek(position, 0)
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001385 self.filelist.append(zinfo)
1386 self.NameToInfo[zinfo.filename] = zinfo
1387
Ronald Oussorenee5c8852010-02-07 20:24:02 +00001388 def writestr(self, zinfo_or_arcname, data, compress_type=None):
Guido van Rossum85825dc2007-08-27 17:03:28 +00001389 """Write a file into the archive. The contents is 'data', which
1390 may be either a 'str' or a 'bytes' instance; if it is a 'str',
1391 it is encoded as UTF-8 first.
1392 'zinfo_or_arcname' is either a ZipInfo instance or
Just van Rossumb083cb32002-12-12 12:23:32 +00001393 the name of the file in the archive."""
Guido van Rossum85825dc2007-08-27 17:03:28 +00001394 if isinstance(data, str):
1395 data = data.encode("utf-8")
Just van Rossumb083cb32002-12-12 12:23:32 +00001396 if not isinstance(zinfo_or_arcname, ZipInfo):
1397 zinfo = ZipInfo(filename=zinfo_or_arcname,
Guido van Rossum7736b5b2008-01-15 21:44:53 +00001398 date_time=time.localtime(time.time())[:6])
Just van Rossumb083cb32002-12-12 12:23:32 +00001399 zinfo.compress_type = self.compression
Antoine Pitrou6e1df8d2008-07-25 19:58:18 +00001400 zinfo.external_attr = 0o600 << 16
Just van Rossumb083cb32002-12-12 12:23:32 +00001401 else:
1402 zinfo = zinfo_or_arcname
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001403
1404 if not self.fp:
1405 raise RuntimeError(
1406 "Attempt to write to ZIP archive that was already closed")
1407
Guido van Rossum85825dc2007-08-27 17:03:28 +00001408 zinfo.file_size = len(data) # Uncompressed size
1409 zinfo.header_offset = self.fp.tell() # Start of header data
Ronald Oussorenee5c8852010-02-07 20:24:02 +00001410 if compress_type is not None:
1411 zinfo.compress_type = compress_type
Martin v. Löwis7fb79fc2012-05-13 10:06:36 +02001412 if zinfo.compress_type == ZIP_LZMA:
1413 # Compressed data includes an end-of-stream (EOS) marker
1414 zinfo.flag_bits |= 0x02
Ronald Oussorenee5c8852010-02-07 20:24:02 +00001415
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001416 self._writecheck(zinfo)
1417 self._didModify = True
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00001418 zinfo.CRC = crc32(data) & 0xffffffff # CRC-32 checksum
Martin v. Löwisf6b16a42012-05-01 07:58:44 +02001419 co = _get_compressor(zinfo.compress_type)
1420 if co:
Guido van Rossum85825dc2007-08-27 17:03:28 +00001421 data = co.compress(data) + co.flush()
1422 zinfo.compress_size = len(data) # Compressed size
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001423 else:
1424 zinfo.compress_size = zinfo.file_size
Serhiy Storchaka182d7cd2013-01-15 00:31:39 +02001425 zip64 = zinfo.file_size > ZIP64_LIMIT or \
1426 zinfo.compress_size > ZIP64_LIMIT
1427 if zip64 and not self._allowZip64:
1428 raise LargeZipFile("Filesize would require ZIP64 extensions")
1429 self.fp.write(zinfo.FileHeader(zip64))
Guido van Rossum85825dc2007-08-27 17:03:28 +00001430 self.fp.write(data)
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001431 if zinfo.flag_bits & 0x08:
Tim Peterse1190062001-01-15 03:34:38 +00001432 # Write CRC and file sizes after the file data
Serhiy Storchaka182d7cd2013-01-15 00:31:39 +02001433 fmt = '<LQQ' if zip64 else '<LLL'
1434 self.fp.write(struct.pack(fmt, zinfo.CRC, zinfo.compress_size,
Tim Peterse1190062001-01-15 03:34:38 +00001435 zinfo.file_size))
Serhiy Storchaka182d7cd2013-01-15 00:31:39 +02001436 self.fp.flush()
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001437 self.filelist.append(zinfo)
1438 self.NameToInfo[zinfo.filename] = zinfo
1439
1440 def __del__(self):
Fred Drake484d7352000-10-02 21:14:52 +00001441 """Call the "close()" method in case the user forgot."""
Tim Petersd15f8bb2001-11-28 23:16:40 +00001442 self.close()
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001443
1444 def close(self):
Fred Drake484d7352000-10-02 21:14:52 +00001445 """Close the file, and for mode "w" and "a" write the ending
1446 records."""
Tim Petersd15f8bb2001-11-28 23:16:40 +00001447 if self.fp is None:
1448 return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001449
Antoine Pitrou17babc52012-11-17 23:50:08 +01001450 try:
1451 if self.mode in ("w", "a") and self._didModify: # write ending records
1452 count = 0
1453 pos1 = self.fp.tell()
1454 for zinfo in self.filelist: # write central directory
1455 count = count + 1
1456 dt = zinfo.date_time
1457 dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2]
1458 dostime = dt[3] << 11 | dt[4] << 5 | (dt[5] // 2)
1459 extra = []
1460 if zinfo.file_size > ZIP64_LIMIT \
1461 or zinfo.compress_size > ZIP64_LIMIT:
1462 extra.append(zinfo.file_size)
1463 extra.append(zinfo.compress_size)
1464 file_size = 0xffffffff
1465 compress_size = 0xffffffff
1466 else:
1467 file_size = zinfo.file_size
1468 compress_size = zinfo.compress_size
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001469
Antoine Pitrou17babc52012-11-17 23:50:08 +01001470 if zinfo.header_offset > ZIP64_LIMIT:
1471 extra.append(zinfo.header_offset)
1472 header_offset = 0xffffffff
1473 else:
1474 header_offset = zinfo.header_offset
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001475
Antoine Pitrou17babc52012-11-17 23:50:08 +01001476 extra_data = zinfo.extra
Antoine Pitrou8572da52012-11-17 23:52:05 +01001477 min_version = 0
Antoine Pitrou17babc52012-11-17 23:50:08 +01001478 if extra:
1479 # Append a ZIP64 field to the extra's
1480 extra_data = struct.pack(
1481 '<HH' + 'Q'*len(extra),
1482 1, 8*len(extra), *extra) + extra_data
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001483
Antoine Pitrou8572da52012-11-17 23:52:05 +01001484 min_version = ZIP64_VERSION
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001485
Antoine Pitrou8572da52012-11-17 23:52:05 +01001486 if zinfo.compress_type == ZIP_BZIP2:
1487 min_version = max(BZIP2_VERSION, min_version)
1488 elif zinfo.compress_type == ZIP_LZMA:
1489 min_version = max(LZMA_VERSION, min_version)
Martin v. Löwisf6b16a42012-05-01 07:58:44 +02001490
Antoine Pitrou8572da52012-11-17 23:52:05 +01001491 extract_version = max(min_version, zinfo.extract_version)
1492 create_version = max(min_version, zinfo.create_version)
Antoine Pitrou17babc52012-11-17 23:50:08 +01001493 try:
1494 filename, flag_bits = zinfo._encodeFilenameFlags()
1495 centdir = struct.pack(structCentralDir,
1496 stringCentralDir, create_version,
1497 zinfo.create_system, extract_version, zinfo.reserved,
1498 flag_bits, zinfo.compress_type, dostime, dosdate,
1499 zinfo.CRC, compress_size, file_size,
1500 len(filename), len(extra_data), len(zinfo.comment),
1501 0, zinfo.internal_attr, zinfo.external_attr,
1502 header_offset)
1503 except DeprecationWarning:
1504 print((structCentralDir, stringCentralDir, create_version,
1505 zinfo.create_system, extract_version, zinfo.reserved,
1506 zinfo.flag_bits, zinfo.compress_type, dostime, dosdate,
1507 zinfo.CRC, compress_size, file_size,
1508 len(zinfo.filename), len(extra_data), len(zinfo.comment),
1509 0, zinfo.internal_attr, zinfo.external_attr,
1510 header_offset), file=sys.stderr)
1511 raise
1512 self.fp.write(centdir)
1513 self.fp.write(filename)
1514 self.fp.write(extra_data)
1515 self.fp.write(zinfo.comment)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001516
Antoine Pitrou17babc52012-11-17 23:50:08 +01001517 pos2 = self.fp.tell()
1518 # Write end-of-zip-archive record
1519 centDirCount = count
1520 centDirSize = pos2 - pos1
1521 centDirOffset = pos1
1522 if (centDirCount >= ZIP_FILECOUNT_LIMIT or
1523 centDirOffset > ZIP64_LIMIT or
1524 centDirSize > ZIP64_LIMIT):
1525 # Need to write the ZIP64 end-of-archive records
1526 zip64endrec = struct.pack(
1527 structEndArchive64, stringEndArchive64,
1528 44, 45, 45, 0, 0, centDirCount, centDirCount,
1529 centDirSize, centDirOffset)
1530 self.fp.write(zip64endrec)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001531
Antoine Pitrou17babc52012-11-17 23:50:08 +01001532 zip64locrec = struct.pack(
1533 structEndArchive64Locator,
1534 stringEndArchive64Locator, 0, pos2, 1)
1535 self.fp.write(zip64locrec)
1536 centDirCount = min(centDirCount, 0xFFFF)
1537 centDirSize = min(centDirSize, 0xFFFFFFFF)
1538 centDirOffset = min(centDirOffset, 0xFFFFFFFF)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001539
Antoine Pitrou17babc52012-11-17 23:50:08 +01001540 endrec = struct.pack(structEndArchive, stringEndArchive,
1541 0, 0, centDirCount, centDirCount,
1542 centDirSize, centDirOffset, len(self._comment))
1543 self.fp.write(endrec)
1544 self.fp.write(self._comment)
1545 self.fp.flush()
1546 finally:
1547 fp = self.fp
1548 self.fp = None
1549 if not self._filePassed:
1550 fp.close()
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001551
1552
1553class PyZipFile(ZipFile):
Fred Drake484d7352000-10-02 21:14:52 +00001554 """Class to create ZIP archives with Python library files and packages."""
1555
Georg Brandl8334fd92010-12-04 10:26:46 +00001556 def __init__(self, file, mode="r", compression=ZIP_STORED,
1557 allowZip64=False, optimize=-1):
1558 ZipFile.__init__(self, file, mode=mode, compression=compression,
1559 allowZip64=allowZip64)
1560 self._optimize = optimize
1561
Georg Brandlfe991052009-09-16 15:54:04 +00001562 def writepy(self, pathname, basename=""):
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001563 """Add all files from "pathname" to the ZIP archive.
1564
Fred Drake484d7352000-10-02 21:14:52 +00001565 If pathname is a package directory, search the directory and
1566 all package subdirectories recursively for all *.py and enter
1567 the modules into the archive. If pathname is a plain
1568 directory, listdir *.py and enter all modules. Else, pathname
1569 must be a Python *.py file and the module will be put into the
1570 archive. Added modules are always module.pyo or module.pyc.
1571 This method will compile the module.py into module.pyc if
1572 necessary.
1573 """
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001574 dir, name = os.path.split(pathname)
1575 if os.path.isdir(pathname):
1576 initname = os.path.join(pathname, "__init__.py")
1577 if os.path.isfile(initname):
1578 # This is a package directory, add it
1579 if basename:
1580 basename = "%s/%s" % (basename, name)
1581 else:
1582 basename = name
1583 if self.debug:
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001584 print("Adding package in", pathname, "as", basename)
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001585 fname, arcname = self._get_codename(initname[0:-3], basename)
1586 if self.debug:
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001587 print("Adding", arcname)
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001588 self.write(fname, arcname)
1589 dirlist = os.listdir(pathname)
1590 dirlist.remove("__init__.py")
1591 # Add all *.py files and package subdirectories
1592 for filename in dirlist:
1593 path = os.path.join(pathname, filename)
1594 root, ext = os.path.splitext(filename)
1595 if os.path.isdir(path):
1596 if os.path.isfile(os.path.join(path, "__init__.py")):
1597 # This is a package directory, add it
1598 self.writepy(path, basename) # Recursive call
1599 elif ext == ".py":
1600 fname, arcname = self._get_codename(path[0:-3],
1601 basename)
1602 if self.debug:
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001603 print("Adding", arcname)
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001604 self.write(fname, arcname)
1605 else:
1606 # This is NOT a package directory, add its files at top level
1607 if self.debug:
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001608 print("Adding files from directory", pathname)
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001609 for filename in os.listdir(pathname):
1610 path = os.path.join(pathname, filename)
1611 root, ext = os.path.splitext(filename)
1612 if ext == ".py":
1613 fname, arcname = self._get_codename(path[0:-3],
1614 basename)
1615 if self.debug:
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001616 print("Adding", arcname)
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001617 self.write(fname, arcname)
1618 else:
1619 if pathname[-3:] != ".py":
Collin Winterce36ad82007-08-30 01:19:48 +00001620 raise RuntimeError(
1621 'Files added with writepy() must end with ".py"')
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001622 fname, arcname = self._get_codename(pathname[0:-3], basename)
1623 if self.debug:
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001624 print("Adding file", arcname)
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001625 self.write(fname, arcname)
1626
1627 def _get_codename(self, pathname, basename):
1628 """Return (filename, archivename) for the path.
1629
Fred Drake484d7352000-10-02 21:14:52 +00001630 Given a module name path, return the correct file path and
1631 archive name, compiling if necessary. For example, given
1632 /python/lib/string, return (/python/lib/string.pyc, string).
1633 """
Georg Brandl8334fd92010-12-04 10:26:46 +00001634 def _compile(file, optimize=-1):
1635 import py_compile
1636 if self.debug:
1637 print("Compiling", file)
1638 try:
1639 py_compile.compile(file, doraise=True, optimize=optimize)
Serhiy Storchaka45c43752013-01-29 20:10:28 +02001640 except py_compile.PyCompileError as err:
Georg Brandl8334fd92010-12-04 10:26:46 +00001641 print(err.msg)
1642 return False
1643 return True
1644
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001645 file_py = pathname + ".py"
1646 file_pyc = pathname + ".pyc"
1647 file_pyo = pathname + ".pyo"
Barry Warsaw28a691b2010-04-17 00:19:56 +00001648 pycache_pyc = imp.cache_from_source(file_py, True)
1649 pycache_pyo = imp.cache_from_source(file_py, False)
Georg Brandl8334fd92010-12-04 10:26:46 +00001650 if self._optimize == -1:
1651 # legacy mode: use whatever file is present
1652 if (os.path.isfile(file_pyo) and
1653 os.stat(file_pyo).st_mtime >= os.stat(file_py).st_mtime):
1654 # Use .pyo file.
1655 arcname = fname = file_pyo
1656 elif (os.path.isfile(file_pyc) and
1657 os.stat(file_pyc).st_mtime >= os.stat(file_py).st_mtime):
1658 # Use .pyc file.
1659 arcname = fname = file_pyc
1660 elif (os.path.isfile(pycache_pyc) and
1661 os.stat(pycache_pyc).st_mtime >= os.stat(file_py).st_mtime):
1662 # Use the __pycache__/*.pyc file, but write it to the legacy pyc
1663 # file name in the archive.
1664 fname = pycache_pyc
1665 arcname = file_pyc
1666 elif (os.path.isfile(pycache_pyo) and
1667 os.stat(pycache_pyo).st_mtime >= os.stat(file_py).st_mtime):
1668 # Use the __pycache__/*.pyo file, but write it to the legacy pyo
1669 # file name in the archive.
1670 fname = pycache_pyo
1671 arcname = file_pyo
Barry Warsaw28a691b2010-04-17 00:19:56 +00001672 else:
Georg Brandl8334fd92010-12-04 10:26:46 +00001673 # Compile py into PEP 3147 pyc file.
1674 if _compile(file_py):
1675 fname = (pycache_pyc if __debug__ else pycache_pyo)
1676 arcname = (file_pyc if __debug__ else file_pyo)
1677 else:
1678 fname = arcname = file_py
1679 else:
1680 # new mode: use given optimization level
1681 if self._optimize == 0:
1682 fname = pycache_pyc
1683 arcname = file_pyc
1684 else:
1685 fname = pycache_pyo
1686 arcname = file_pyo
1687 if not (os.path.isfile(fname) and
1688 os.stat(fname).st_mtime >= os.stat(file_py).st_mtime):
1689 if not _compile(file_py, optimize=self._optimize):
1690 fname = arcname = file_py
Barry Warsaw28a691b2010-04-17 00:19:56 +00001691 archivename = os.path.split(arcname)[1]
Guido van Rossum32abe6f2000-03-31 17:30:02 +00001692 if basename:
1693 archivename = "%s/%s" % (basename, archivename)
1694 return (fname, archivename)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001695
1696
1697def main(args = None):
1698 import textwrap
1699 USAGE=textwrap.dedent("""\
1700 Usage:
1701 zipfile.py -l zipfile.zip # Show listing of a zipfile
1702 zipfile.py -t zipfile.zip # Test if a zipfile is valid
1703 zipfile.py -e zipfile.zip target # Extract zipfile into target dir
1704 zipfile.py -c zipfile.zip src ... # Create zipfile from sources
1705 """)
1706 if args is None:
1707 args = sys.argv[1:]
1708
1709 if not args or args[0] not in ('-l', '-c', '-e', '-t'):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001710 print(USAGE)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001711 sys.exit(1)
1712
1713 if args[0] == '-l':
1714 if len(args) != 2:
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001715 print(USAGE)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001716 sys.exit(1)
Antoine Pitrou17babc52012-11-17 23:50:08 +01001717 with ZipFile(args[1], 'r') as zf:
1718 zf.printdir()
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001719
1720 elif args[0] == '-t':
1721 if len(args) != 2:
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001722 print(USAGE)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001723 sys.exit(1)
Antoine Pitrou17babc52012-11-17 23:50:08 +01001724 with ZipFile(args[1], 'r') as zf:
1725 badfile = zf.testzip()
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001726 if badfile:
1727 print("The following enclosed file is corrupted: {!r}".format(badfile))
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001728 print("Done testing")
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001729
1730 elif args[0] == '-e':
1731 if len(args) != 3:
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001732 print(USAGE)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001733 sys.exit(1)
1734
Antoine Pitrou17babc52012-11-17 23:50:08 +01001735 with ZipFile(args[1], 'r') as zf:
1736 out = args[2]
1737 for path in zf.namelist():
1738 if path.startswith('./'):
1739 tgt = os.path.join(out, path[2:])
1740 else:
1741 tgt = os.path.join(out, path)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001742
Antoine Pitrou17babc52012-11-17 23:50:08 +01001743 tgtdir = os.path.dirname(tgt)
1744 if not os.path.exists(tgtdir):
1745 os.makedirs(tgtdir)
1746 with open(tgt, 'wb') as fp:
1747 fp.write(zf.read(path))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001748
1749 elif args[0] == '-c':
1750 if len(args) < 3:
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001751 print(USAGE)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001752 sys.exit(1)
1753
1754 def addToZip(zf, path, zippath):
1755 if os.path.isfile(path):
1756 zf.write(path, zippath, ZIP_DEFLATED)
1757 elif os.path.isdir(path):
1758 for nm in os.listdir(path):
1759 addToZip(zf,
1760 os.path.join(path, nm), os.path.join(zippath, nm))
1761 # else: ignore
1762
Antoine Pitrou17babc52012-11-17 23:50:08 +01001763 with ZipFile(args[1], 'w', allowZip64=True) as zf:
1764 for src in args[2:]:
1765 addToZip(zf, src, os.path.basename(src))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001766
1767if __name__ == "__main__":
1768 main()