annotate Lib/zipfile.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 3e84d60126b7
children 3be37ec43c1b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1 """
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
2 Read and write ZIP files.
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
3 """
44576
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
4 import struct, os, time, sys, shutil
50999
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50836
diff changeset
5 import binascii, cStringIO, stat
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 try:
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
8 import zlib # We may need its compression method
45896
733c3ee19899 Use zlib's crc32 routine instead of binascii when available. zlib's is faster
Gregory P. Smith <greg@mad-scientist.com>
parents: 44809
diff changeset
9 crc32 = zlib.crc32
17377
5f68754eb3b6 Try an except: after an import into "except ImportError".
Guido van Rossum <guido@python.org>
parents: 17312
diff changeset
10 except ImportError:
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 zlib = None
45896
733c3ee19899 Use zlib's crc32 routine instead of binascii when available. zlib's is faster
Gregory P. Smith <greg@mad-scientist.com>
parents: 44809
diff changeset
12 crc32 = binascii.crc32
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
16889
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 16840
diff changeset
14 __all__ = ["BadZipfile", "error", "ZIP_STORED", "ZIP_DEFLATED", "is_zipfile",
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
15 "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile" ]
16889
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 16840
diff changeset
16
14933
715a3e924737 Always use the same name for the exception defined in this module!
Fred Drake <fdrake@acm.org>
parents: 12919
diff changeset
17 class BadZipfile(Exception):
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 pass
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
19
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
20
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
21 class LargeZipFile(Exception):
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
22 """
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
23 Raised when writing a zipfile, the zipfile requires ZIP64 extensions
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
24 and those extensions are disabled.
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
25 """
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
26
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
27 error = BadZipfile # The exception raised by this module
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
50824
dc31be50c405 #3997: zipfiles generated with more than 65536 files could not be opened
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50300
diff changeset
29 ZIP64_LIMIT = (1 << 31) - 1
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
30 ZIP_FILECOUNT_LIMIT = 1 << 16
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
31 ZIP_MAX_COMMENT = (1 << 16) - 1
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
32
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 # constants for Zip file compression methods
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 ZIP_STORED = 0
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 ZIP_DEFLATED = 8
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 # Other ZIP compression methods not supported
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
38 # Below are some formats and associated data for reading/writing headers using
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
39 # the struct module. The names and structures of headers/records are those used
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
40 # in the PKWARE description of the ZIP file format:
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
41 # http://www.pkware.com/documents/casestudies/APPNOTE.TXT
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
42 # (URL valid as of January 2008)
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
43
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
44 # The "end of central directory" structure, magic number, size, and indices
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
45 # (section V.I in the format document)
47932
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
46 structEndArchive = "<4s4H2LH"
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
47 stringEndArchive = "PK\005\006"
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
48 sizeEndCentDir = struct.calcsize(structEndArchive)
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
49
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
50 _ECD_SIGNATURE = 0
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
51 _ECD_DISK_NUMBER = 1
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
52 _ECD_DISK_START = 2
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
53 _ECD_ENTRIES_THIS_DISK = 3
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
54 _ECD_ENTRIES_TOTAL = 4
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
55 _ECD_SIZE = 5
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
56 _ECD_OFFSET = 6
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
57 _ECD_COMMENT_SIZE = 7
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
58 # These last two indices are not part of the structure as defined in the
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
59 # spec, but they are used internally by this module as a convenience
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
60 _ECD_COMMENT = 8
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
61 _ECD_LOCATION = 9
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
62
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
63 # The "central directory" structure, magic number, size, and indices
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
64 # of entries in the structure (section V.F in the format document)
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
65 structCentralDir = "<4s4B4HL2L5H2L"
47932
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
66 stringCentralDir = "PK\001\002"
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
67 sizeCentralDir = struct.calcsize(structCentralDir)
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
68
16840
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
69 # indexes of entries in the central directory structure
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
70 _CD_SIGNATURE = 0
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
71 _CD_CREATE_VERSION = 1
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
72 _CD_CREATE_SYSTEM = 2
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
73 _CD_EXTRACT_VERSION = 3
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
74 _CD_EXTRACT_SYSTEM = 4
16840
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
75 _CD_FLAG_BITS = 5
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
76 _CD_COMPRESS_TYPE = 6
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
77 _CD_TIME = 7
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
78 _CD_DATE = 8
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
79 _CD_CRC = 9
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
80 _CD_COMPRESSED_SIZE = 10
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
81 _CD_UNCOMPRESSED_SIZE = 11
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
82 _CD_FILENAME_LENGTH = 12
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
83 _CD_EXTRA_FIELD_LENGTH = 13
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
84 _CD_COMMENT_LENGTH = 14
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
85 _CD_DISK_NUMBER_START = 15
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
86 _CD_INTERNAL_FILE_ATTRIBUTES = 16
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
87 _CD_EXTERNAL_FILE_ATTRIBUTES = 17
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
88 _CD_LOCAL_HEADER_OFFSET = 18
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
89
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
90 # The "local file header" structure, magic number, size, and indices
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
91 # (section V.A in the format document)
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
92 structFileHeader = "<4s2B4HL2L2H"
47932
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
93 stringFileHeader = "PK\003\004"
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
94 sizeFileHeader = struct.calcsize(structFileHeader)
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
95
16840
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
96 _FH_SIGNATURE = 0
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
97 _FH_EXTRACT_VERSION = 1
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
98 _FH_EXTRACT_SYSTEM = 2
16840
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
99 _FH_GENERAL_PURPOSE_FLAG_BITS = 3
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
100 _FH_COMPRESSION_METHOD = 4
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
101 _FH_LAST_MOD_TIME = 5
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
102 _FH_LAST_MOD_DATE = 6
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
103 _FH_CRC = 7
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
104 _FH_COMPRESSED_SIZE = 8
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
105 _FH_UNCOMPRESSED_SIZE = 9
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
106 _FH_FILENAME_LENGTH = 10
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
107 _FH_EXTRA_FIELD_LENGTH = 11
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
108
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
109 # The "Zip64 end of central directory locator" structure, magic number, and size
47932
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
110 structEndArchive64Locator = "<4sLQL"
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
111 stringEndArchive64Locator = "PK\x06\x07"
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
112 sizeEndCentDir64Locator = struct.calcsize(structEndArchive64Locator)
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
113
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
114 # The "Zip64 end of central directory" record, magic number, size, and indices
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
115 # (section V.G in the format document)
47932
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
116 structEndArchive64 = "<4sQ2H2L4Q"
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
117 stringEndArchive64 = "PK\x06\x06"
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
118 sizeEndCentDir64 = struct.calcsize(structEndArchive64)
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
119
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
120 _CD64_SIGNATURE = 0
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
121 _CD64_DIRECTORY_RECSIZE = 1
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
122 _CD64_CREATE_VERSION = 2
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
123 _CD64_EXTRACT_VERSION = 3
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
124 _CD64_DISK_NUMBER = 4
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
125 _CD64_DISK_NUMBER_START = 5
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
126 _CD64_NUMBER_ENTRIES_THIS_DISK = 6
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
127 _CD64_NUMBER_ENTRIES_TOTAL = 7
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
128 _CD64_DIRECTORY_SIZE = 8
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
129 _CD64_OFFSET_START_CENTDIR = 9
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
130
50300
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
131 def _check_zipfile(fp):
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 try:
50300
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
133 if _EndRecData(fp):
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
134 return True # file has correct magic number
17805
bf70d1f4f61f Fix one bare except: clause.
Fred Drake <fdrake@acm.org>
parents: 17517
diff changeset
135 except IOError:
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 pass
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 21251
diff changeset
137 return False
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
138
50300
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
139 def is_zipfile(filename):
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
140 """Quickly see if a file is a ZIP file by checking the magic number.
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
141
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
142 The filename argument may be a file or file-like object too.
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
143 """
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
144 result = False
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
145 try:
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
146 if hasattr(filename, "read"):
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
147 result = _check_zipfile(fp=filename)
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
148 else:
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
149 with open(filename, "rb") as fp:
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
150 result = _check_zipfile(fp)
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
151 except IOError:
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
152 pass
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
153 return result
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
154
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
155 def _EndRecData64(fpin, offset, endrec):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
156 """
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
157 Read the ZIP64 end-of-archive records and use that to update endrec
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
158 """
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
159 fpin.seek(offset - sizeEndCentDir64Locator, 2)
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
160 data = fpin.read(sizeEndCentDir64Locator)
47932
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
161 sig, diskno, reloff, disks = struct.unpack(structEndArchive64Locator, data)
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
162 if sig != stringEndArchive64Locator:
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
163 return endrec
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
164
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
165 if diskno != 0 or disks != 1:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
166 raise BadZipfile("zipfiles that span multiple disks are not supported")
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
167
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
168 # Assume no 'zip64 extensible data'
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
169 fpin.seek(offset - sizeEndCentDir64Locator - sizeEndCentDir64, 2)
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
170 data = fpin.read(sizeEndCentDir64)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
171 sig, sz, create_version, read_version, disk_num, disk_dir, \
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
172 dircount, dircount2, dirsize, diroffset = \
47932
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
173 struct.unpack(structEndArchive64, data)
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
174 if sig != stringEndArchive64:
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
175 return endrec
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
176
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
177 # Update the original endrec using data from the ZIP64 record
49036
f32517f876b6 Issue #3535: zipfile couldn't read some zip files larger than 2GB.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48265
diff changeset
178 endrec[_ECD_SIGNATURE] = sig
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
179 endrec[_ECD_DISK_NUMBER] = disk_num
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
180 endrec[_ECD_DISK_START] = disk_dir
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
181 endrec[_ECD_ENTRIES_THIS_DISK] = dircount
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
182 endrec[_ECD_ENTRIES_TOTAL] = dircount2
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
183 endrec[_ECD_SIZE] = dirsize
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
184 endrec[_ECD_OFFSET] = diroffset
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
185 return endrec
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
186
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
187
25756
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
188 def _EndRecData(fpin):
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
189 """Return data from the "End of Central Directory" record, or None.
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
190
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
191 The data is a list of the nine items in the ZIP "End of central dir"
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
192 record followed by a tenth item, the file seek offset of this record."""
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
193
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
194 # Determine file size
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
195 fpin.seek(0, 2)
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
196 filesize = fpin.tell()
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
197
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
198 # Check to see if this is ZIP file with no archive comment (the
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
199 # "end of central directory" structure should be the last item in the
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
200 # file if this is the case).
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
201 fpin.seek(-sizeEndCentDir, 2)
25756
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
202 data = fpin.read()
47932
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
203 if data[0:4] == stringEndArchive and data[-2:] == "\000\000":
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
204 # the signature is correct and there's no comment, unpack structure
47932
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
205 endrec = struct.unpack(structEndArchive, data)
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
206 endrec=list(endrec)
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
207
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
208 # Append a blank comment and record start offset
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
209 endrec.append("")
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
210 endrec.append(filesize - sizeEndCentDir)
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
211
50836
bb9c6524fdca follow-up of #3997: since 0xFFFF numbers are not enough to indicate a zip64 format,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50824
diff changeset
212 # Try to read the "Zip64 end of central directory" structure
bb9c6524fdca follow-up of #3997: since 0xFFFF numbers are not enough to indicate a zip64 format,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50824
diff changeset
213 return _EndRecData64(fpin, -sizeEndCentDir, endrec)
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
214
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
215 # Either this is not a ZIP file, or it is a ZIP file with an archive
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
216 # comment. Search the end of the file for the "end of central directory"
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
217 # record signature. The comment is the last item in the ZIP file and may be
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
218 # up to 64K long. It is assumed that the "end of central directory" magic
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
219 # number does not appear in the comment.
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
220 maxCommentStart = max(filesize - (1 << 16) - sizeEndCentDir, 0)
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
221 fpin.seek(maxCommentStart, 0)
25756
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
222 data = fpin.read()
47932
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
223 start = data.rfind(stringEndArchive)
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
224 if start >= 0:
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
225 # found the magic number; attempt to unpack and interpret
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
226 recData = data[start:start+sizeEndCentDir]
47932
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
227 endrec = list(struct.unpack(structEndArchive, recData))
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
228 comment = data[start+sizeEndCentDir:]
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
229 # check that comment length is correct
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
230 if endrec[_ECD_COMMENT_SIZE] == len(comment):
25756
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
231 # Append the archive comment and start offset
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
232 endrec.append(comment)
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
233 endrec.append(maxCommentStart + start)
50836
bb9c6524fdca follow-up of #3997: since 0xFFFF numbers are not enough to indicate a zip64 format,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50824
diff changeset
234
bb9c6524fdca follow-up of #3997: since 0xFFFF numbers are not enough to indicate a zip64 format,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50824
diff changeset
235 # Try to read the "Zip64 end of central directory" structure
bb9c6524fdca follow-up of #3997: since 0xFFFF numbers are not enough to indicate a zip64 format,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50824
diff changeset
236 return _EndRecData64(fpin, maxCommentStart + start - filesize,
bb9c6524fdca follow-up of #3997: since 0xFFFF numbers are not enough to indicate a zip64 format,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50824
diff changeset
237 endrec)
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
238
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
239 # Unable to find a valid end of central directory structure
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
240 return
25756
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
241
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
242
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
243 class ZipInfo (object):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
244 """Class with attributes describing each file in the ZIP archive."""
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
245
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
246 __slots__ = (
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
247 'orig_filename',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
248 'filename',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
249 'date_time',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
250 'compress_type',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
251 'comment',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
252 'extra',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
253 'create_system',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
254 'create_version',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
255 'extract_version',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
256 'reserved',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
257 'flag_bits',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
258 'volume',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
259 'internal_attr',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
260 'external_attr',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
261 'header_offset',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
262 'CRC',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
263 'compress_size',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
264 'file_size',
44807
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44802
diff changeset
265 '_raw_time',
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
266 )
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
267
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 def __init__(self, filename="NoName", date_time=(1980,1,1,0,0,0)):
29523
87f30fcfad48 SF patch #755987 (Jim Ahlstrom):
Greg Ward <gward@python.net>
parents: 27250
diff changeset
269 self.orig_filename = filename # Original file name in archive
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
270
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
271 # Terminate the file name at the first null byte. Null bytes in file
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
272 # names are used as tricks by viruses in archives.
29523
87f30fcfad48 SF patch #755987 (Jim Ahlstrom):
Greg Ward <gward@python.net>
parents: 27250
diff changeset
273 null_byte = filename.find(chr(0))
87f30fcfad48 SF patch #755987 (Jim Ahlstrom):
Greg Ward <gward@python.net>
parents: 27250
diff changeset
274 if null_byte >= 0:
87f30fcfad48 SF patch #755987 (Jim Ahlstrom):
Greg Ward <gward@python.net>
parents: 27250
diff changeset
275 filename = filename[0:null_byte]
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
276 # This is used to ensure paths in generated ZIP files always use
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
277 # forward slashes as the directory separator, as required by the
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
278 # ZIP format specification.
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
279 if os.sep != "/" and os.sep in filename:
29523
87f30fcfad48 SF patch #755987 (Jim Ahlstrom):
Greg Ward <gward@python.net>
parents: 27250
diff changeset
280 filename = filename.replace(os.sep, "/")
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
281
29523
87f30fcfad48 SF patch #755987 (Jim Ahlstrom):
Greg Ward <gward@python.net>
parents: 27250
diff changeset
282 self.filename = filename # Normalized file name
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
283 self.date_time = date_time # year, month, day, hour, min, sec
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 # Standard values:
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
285 self.compress_type = ZIP_STORED # Type of compression for the file
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
286 self.comment = "" # Comment for each file
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
287 self.extra = "" # ZIP extra data
36449
4ed30eaa0256 Patch #1412872: zipfile: use correct system type on unixy systems.
Martin v. Löwis <martin@v.loewis.de>
parents: 34594
diff changeset
288 if sys.platform == 'win32':
4ed30eaa0256 Patch #1412872: zipfile: use correct system type on unixy systems.
Martin v. Löwis <martin@v.loewis.de>
parents: 34594
diff changeset
289 self.create_system = 0 # System which created ZIP archive
4ed30eaa0256 Patch #1412872: zipfile: use correct system type on unixy systems.
Martin v. Löwis <martin@v.loewis.de>
parents: 34594
diff changeset
290 else:
4ed30eaa0256 Patch #1412872: zipfile: use correct system type on unixy systems.
Martin v. Löwis <martin@v.loewis.de>
parents: 34594
diff changeset
291 # Assume everything else is unix-y
4ed30eaa0256 Patch #1412872: zipfile: use correct system type on unixy systems.
Martin v. Löwis <martin@v.loewis.de>
parents: 34594
diff changeset
292 self.create_system = 3 # System which created ZIP archive
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
293 self.create_version = 20 # Version which created ZIP archive
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
294 self.extract_version = 20 # Version needed to extract archive
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
295 self.reserved = 0 # Must be zero
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
296 self.flag_bits = 0 # ZIP flag bits
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
297 self.volume = 0 # Volume number of file header
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
298 self.internal_attr = 0 # Internal attributes
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
299 self.external_attr = 0 # External file attributes
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 # Other attributes are set by class ZipFile:
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
301 # header_offset Byte offset to the file header
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
302 # CRC CRC-32 of the uncompressed file
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
303 # compress_size Size of the compressed file
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
304 # file_size Size of the uncompressed file
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
305
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 def FileHeader(self):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
307 """Return the per-file header as a string."""
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 dt = self.date_time
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2]
21251
45e1177424e1 SF bug #488514: -Qnew needs work
Tim Peters <tim.peters@gmail.com>
parents: 21098
diff changeset
310 dostime = dt[3] << 11 | dt[4] << 5 | (dt[5] // 2)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 if self.flag_bits & 0x08:
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
312 # Set these to zero because we write them after the file data
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
313 CRC = compress_size = file_size = 0
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 else:
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
315 CRC = self.CRC
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
316 compress_size = self.compress_size
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
317 file_size = self.file_size
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
318
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
319 extra = self.extra
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
320
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
321 if file_size > ZIP64_LIMIT or compress_size > ZIP64_LIMIT:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
322 # File is larger than what fits into a 4 byte integer,
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
323 # fall back to the ZIP64 extension
45902
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
324 fmt = '<HHQQ'
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
325 extra = extra + struct.pack(fmt,
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
326 1, struct.calcsize(fmt)-4, file_size, compress_size)
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
327 file_size = 0xffffffff
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
328 compress_size = 0xffffffff
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
329 self.extract_version = max(45, self.extract_version)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
330 self.create_version = max(45, self.extract_version)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
331
46671
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
332 filename, flag_bits = self._encodeFilenameFlags()
47932
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
333 header = struct.pack(structFileHeader, stringFileHeader,
46671
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
334 self.extract_version, self.reserved, flag_bits,
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
335 self.compress_type, dostime, dosdate, CRC,
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 compress_size, file_size,
46671
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
337 len(filename), len(extra))
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
338 return header + filename + extra
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
339
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
340 def _encodeFilenameFlags(self):
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
341 if isinstance(self.filename, unicode):
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
342 try:
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
343 return self.filename.encode('ascii'), self.flag_bits
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
344 except UnicodeEncodeError:
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
345 return self.filename.encode('utf-8'), self.flag_bits | 0x800
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
346 else:
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
347 return self.filename, self.flag_bits
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
348
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
349 def _decodeFilename(self):
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
350 if self.flag_bits & 0x800:
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
351 return self.filename.decode('utf-8')
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
352 else:
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
353 return self.filename
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
354
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
355 def _decodeExtra(self):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
356 # Try to decode the extra field.
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
357 extra = self.extra
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
358 unpack = struct.unpack
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
359 while extra:
45902
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
360 tp, ln = unpack('<HH', extra[:4])
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
361 if tp == 1:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
362 if ln >= 24:
45902
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
363 counts = unpack('<QQQ', extra[4:28])
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
364 elif ln == 16:
45902
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
365 counts = unpack('<QQ', extra[4:20])
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
366 elif ln == 8:
45902
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
367 counts = unpack('<Q', extra[4:12])
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
368 elif ln == 0:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
369 counts = ()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
370 else:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
371 raise RuntimeError, "Corrupt extra field %s"%(ln,)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
372
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
373 idx = 0
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
374
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
375 # ZIP64 extension (large files and/or large archives)
45902
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
376 if self.file_size in (0xffffffffffffffffL, 0xffffffffL):
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
377 self.file_size = counts[idx]
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
378 idx += 1
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
379
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
380 if self.compress_size == 0xFFFFFFFFL:
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
381 self.compress_size = counts[idx]
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
382 idx += 1
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
383
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
384 if self.header_offset == 0xffffffffL:
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
385 old = self.header_offset
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
386 self.header_offset = counts[idx]
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
387 idx+=1
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
388
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
389 extra = extra[ln+4:]
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
390
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
391
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
392 class _ZipDecrypter:
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
393 """Class to handle decryption of files stored within a ZIP archive.
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
394
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
395 ZIP supports a password-based form of encryption. Even though known
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
396 plaintext attacks have been found against it, it is still useful
44809
3e0f4a4b9f55 Document that zipfile decryption is insanely slow and fix a typo and
Gregory P. Smith <greg@mad-scientist.com>
parents: 44807
diff changeset
397 to be able to get data out of such a file.
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
398
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
399 Usage:
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
400 zd = _ZipDecrypter(mypwd)
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
401 plain_char = zd(cypher_char)
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
402 plain_text = map(zd, cypher_text)
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
403 """
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
404
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
405 def _GenerateCRCTable():
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
406 """Generate a CRC-32 table.
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
407
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
408 ZIP encryption uses the CRC32 one-byte primitive for scrambling some
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
409 internal keys. We noticed that a direct implementation is faster than
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
410 relying on binascii.crc32().
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
411 """
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
412 poly = 0xedb88320
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
413 table = [0] * 256
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
414 for i in range(256):
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
415 crc = i
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
416 for j in range(8):
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
417 if crc & 1:
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
418 crc = ((crc >> 1) & 0x7FFFFFFF) ^ poly
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
419 else:
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
420 crc = ((crc >> 1) & 0x7FFFFFFF)
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
421 table[i] = crc
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
422 return table
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
423 crctable = _GenerateCRCTable()
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
424
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
425 def _crc32(self, ch, crc):
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
426 """Compute the CRC32 primitive on one byte."""
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
427 return ((crc >> 8) & 0xffffff) ^ self.crctable[(crc ^ ord(ch)) & 0xff]
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
428
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
429 def __init__(self, pwd):
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
430 self.key0 = 305419896
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
431 self.key1 = 591751049
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
432 self.key2 = 878082192
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
433 for p in pwd:
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
434 self._UpdateKeys(p)
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
435
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
436 def _UpdateKeys(self, c):
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
437 self.key0 = self._crc32(c, self.key0)
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
438 self.key1 = (self.key1 + (self.key0 & 255)) & 4294967295
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
439 self.key1 = (self.key1 * 134775813 + 1) & 4294967295
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
440 self.key2 = self._crc32(chr((self.key1 >> 24) & 255), self.key2)
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
441
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
442 def __call__(self, c):
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
443 """Decrypt a single character."""
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
444 c = ord(c)
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
445 k = self.key2 | 2
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
446 c = c ^ (((k * (k^1)) >> 8) & 255)
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
447 c = chr(c)
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
448 self._UpdateKeys(c)
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
449 return c
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
450
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
451 class ZipExtFile:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
452 """File-like object for reading an archive member.
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
453 Is returned by ZipFile.open().
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
454 """
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
455
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
456 def __init__(self, fileobj, zipinfo, decrypt=None):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
457 self.fileobj = fileobj
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
458 self.decrypter = decrypt
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
459 self.bytes_read = 0L
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
460 self.rawbuffer = ''
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
461 self.readbuffer = ''
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
462 self.linebuffer = ''
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
463 self.eof = False
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
464 self.univ_newlines = False
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
465 self.nlSeps = ("\n", )
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
466 self.lastdiscard = ''
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
467
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
468 self.compress_type = zipinfo.compress_type
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
469 self.compress_size = zipinfo.compress_size
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
470
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
471 self.closed = False
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
472 self.mode = "r"
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
473 self.name = zipinfo.filename
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
474
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
475 # read from compressed files in 64k blocks
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
476 self.compreadsize = 64*1024
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
477 if self.compress_type == ZIP_DEFLATED:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
478 self.dc = zlib.decompressobj(-15)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
479
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
480 def set_univ_newlines(self, univ_newlines):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
481 self.univ_newlines = univ_newlines
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
482
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
483 # pick line separator char(s) based on universal newlines flag
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
484 self.nlSeps = ("\n", )
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
485 if self.univ_newlines:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
486 self.nlSeps = ("\r\n", "\r", "\n")
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
487
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
488 def __iter__(self):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
489 return self
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
490
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
491 def next(self):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
492 nextline = self.readline()
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
493 if not nextline:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
494 raise StopIteration()
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
495
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
496 return nextline
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
497
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
498 def close(self):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
499 self.closed = True
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
500
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
501 def _checkfornewline(self):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
502 nl, nllen = -1, -1
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
503 if self.linebuffer:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
504 # ugly check for cases where half of an \r\n pair was
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
505 # read on the last pass, and the \r was discarded. In this
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
506 # case we just throw away the \n at the start of the buffer.
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
507 if (self.lastdiscard, self.linebuffer[0]) == ('\r','\n'):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
508 self.linebuffer = self.linebuffer[1:]
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
509
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
510 for sep in self.nlSeps:
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
511 nl = self.linebuffer.find(sep)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
512 if nl >= 0:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
513 nllen = len(sep)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
514 return nl, nllen
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
515
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
516 return nl, nllen
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
517
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
518 def readline(self, size = -1):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
519 """Read a line with approx. size. If size is negative,
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
520 read a whole line.
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
521 """
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
522 if size < 0:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
523 size = sys.maxint
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
524 elif size == 0:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
525 return ''
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
526
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
527 # check for a newline already in buffer
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
528 nl, nllen = self._checkfornewline()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
529
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
530 if nl >= 0:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
531 # the next line was already in the buffer
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
532 nl = min(nl, size)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
533 else:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
534 # no line break in buffer - try to read more
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
535 size -= len(self.linebuffer)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
536 while nl < 0 and size > 0:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
537 buf = self.read(min(size, 100))
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
538 if not buf:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
539 break
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
540 self.linebuffer += buf
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
541 size -= len(buf)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
542
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
543 # check for a newline in buffer
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
544 nl, nllen = self._checkfornewline()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
545
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
546 # we either ran out of bytes in the file, or
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
547 # met the specified size limit without finding a newline,
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
548 # so return current buffer
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
549 if nl < 0:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
550 s = self.linebuffer
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
551 self.linebuffer = ''
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
552 return s
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
553
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
554 buf = self.linebuffer[:nl]
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
555 self.lastdiscard = self.linebuffer[nl:nl + nllen]
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
556 self.linebuffer = self.linebuffer[nl + nllen:]
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
557
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
558 # line is always returned with \n as newline char (except possibly
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
559 # for a final incomplete line in the file, which is handled above).
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
560 return buf + "\n"
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
561
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
562 def readlines(self, sizehint = -1):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
563 """Return a list with all (following) lines. The sizehint parameter
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
564 is ignored in this implementation.
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
565 """
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
566 result = []
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
567 while True:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
568 line = self.readline()
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
569 if not line: break
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
570 result.append(line)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
571 return result
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
572
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
573 def read(self, size = None):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
574 # act like file() obj and return empty string if size is 0
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
575 if size == 0:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
576 return ''
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
577
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
578 # determine read size
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
579 bytesToRead = self.compress_size - self.bytes_read
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
580
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
581 # adjust read size for encrypted files since the first 12 bytes
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
582 # are for the encryption/password information
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
583 if self.decrypter is not None:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
584 bytesToRead -= 12
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
585
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
586 if size is not None and size >= 0:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
587 if self.compress_type == ZIP_STORED:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
588 lr = len(self.readbuffer)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
589 bytesToRead = min(bytesToRead, size - lr)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
590 elif self.compress_type == ZIP_DEFLATED:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
591 if len(self.readbuffer) > size:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
592 # the user has requested fewer bytes than we've already
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
593 # pulled through the decompressor; don't read any more
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
594 bytesToRead = 0
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
595 else:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
596 # user will use up the buffer, so read some more
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
597 lr = len(self.rawbuffer)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
598 bytesToRead = min(bytesToRead, self.compreadsize - lr)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
599
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
600 # avoid reading past end of file contents
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
601 if bytesToRead + self.bytes_read > self.compress_size:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
602 bytesToRead = self.compress_size - self.bytes_read
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
603
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
604 # try to read from file (if necessary)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
605 if bytesToRead > 0:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
606 bytes = self.fileobj.read(bytesToRead)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
607 self.bytes_read += len(bytes)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
608 self.rawbuffer += bytes
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
609
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
610 # handle contents of raw buffer
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
611 if self.rawbuffer:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
612 newdata = self.rawbuffer
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
613 self.rawbuffer = ''
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
614
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
615 # decrypt new data if we were given an object to handle that
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
616 if newdata and self.decrypter is not None:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
617 newdata = ''.join(map(self.decrypter, newdata))
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
618
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
619 # decompress newly read data if necessary
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
620 if newdata and self.compress_type == ZIP_DEFLATED:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
621 newdata = self.dc.decompress(newdata)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
622 self.rawbuffer = self.dc.unconsumed_tail
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
623 if self.eof and len(self.rawbuffer) == 0:
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
624 # we're out of raw bytes (both from the file and
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
625 # the local buffer); flush just to make sure the
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
626 # decompressor is done
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
627 newdata += self.dc.flush()
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
628 # prevent decompressor from being used again
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
629 self.dc = None
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
630
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
631 self.readbuffer += newdata
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
632
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
633
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
634 # return what the user asked for
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
635 if size is None or len(self.readbuffer) <= size:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
636 bytes = self.readbuffer
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
637 self.readbuffer = ''
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
638 else:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
639 bytes = self.readbuffer[:size]
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
640 self.readbuffer = self.readbuffer[size:]
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
641
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
642 return bytes
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
643
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
644
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
645 class ZipFile:
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17224
diff changeset
646 """ Class with methods to open, read, write, close, list zip files.
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17224
diff changeset
647
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
648 z = ZipFile(file, mode="r", compression=ZIP_STORED, allowZip64=False)
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17224
diff changeset
649
17224
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
650 file: Either the path to the file, or a file-like object.
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
651 If it is a path, the file will be opened and closed by ZipFile.
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
652 mode: The mode can be either read "r", write "w" or append "a".
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
653 compression: ZIP_STORED (no compression) or ZIP_DEFLATED (requires zlib).
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
654 allowZip64: if True ZipFile will create files with ZIP64 extensions when
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
655 needed, otherwise it will raise an exception when this would
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
656 be necessary.
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
657
17224
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
658 """
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
659
16826
a95693491eca Fix SF tracker bug #403871: AttributeError in ZipFile.__del__() when
Fred Drake <fdrake@acm.org>
parents: 15874
diff changeset
660 fp = None # Set here since __del__ checks it
a95693491eca Fix SF tracker bug #403871: AttributeError in ZipFile.__del__() when
Fred Drake <fdrake@acm.org>
parents: 15874
diff changeset
661
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
662 def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=False):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
663 """Open the ZIP file with mode read "r", write "w" or append "a"."""
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
664 if mode not in ("r", "w", "a"):
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
665 raise RuntimeError('ZipFile() requires mode "r", "w", or "a"')
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
666
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
667 if compression == ZIP_STORED:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
668 pass
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
669 elif compression == ZIP_DEFLATED:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
670 if not zlib:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
671 raise RuntimeError,\
14933
715a3e924737 Always use the same name for the exception defined in this module!
Fred Drake <fdrake@acm.org>
parents: 12919
diff changeset
672 "Compression requires the (missing) zlib module"
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
673 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
674 raise RuntimeError, "That compression method is not supported"
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
675
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
676 self._allowZip64 = allowZip64
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
677 self._didModify = False
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
678 self.debug = 0 # Level of printing: 0 through 3
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
679 self.NameToInfo = {} # Find file info given name
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
680 self.filelist = [] # List of ZipInfo instances for archive
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
681 self.compression = compression # Method of compression
34594
32b89606f0cd Remove dependency on order of mode flags
Raymond Hettinger <python@rcn.com>
parents: 33914
diff changeset
682 self.mode = key = mode.replace('b', '')[0]
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
683 self.pwd = None
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
684 self.comment = ''
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17224
diff changeset
685
17224
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
686 # Check if we were passed a file-like object
23645
e1e90b0f1f45 Remove uses of the string and types modules:
Walter Dörwald <walter@livinglogic.de>
parents: 23625
diff changeset
687 if isinstance(file, basestring):
17224
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
688 self._filePassed = 0
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
689 self.filename = file
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
690 modeDict = {'r' : 'rb', 'w': 'wb', 'a' : 'r+b'}
40834
d949933cd6b4 Patch #1517891: Make 'a' create the file if it doesn't exist.
Martin v. Löwis <martin@v.loewis.de>
parents: 40833
diff changeset
691 try:
d949933cd6b4 Patch #1517891: Make 'a' create the file if it doesn't exist.
Martin v. Löwis <martin@v.loewis.de>
parents: 40833
diff changeset
692 self.fp = open(file, modeDict[mode])
d949933cd6b4 Patch #1517891: Make 'a' create the file if it doesn't exist.
Martin v. Löwis <martin@v.loewis.de>
parents: 40833
diff changeset
693 except IOError:
d949933cd6b4 Patch #1517891: Make 'a' create the file if it doesn't exist.
Martin v. Löwis <martin@v.loewis.de>
parents: 40833
diff changeset
694 if mode == 'a':
d949933cd6b4 Patch #1517891: Make 'a' create the file if it doesn't exist.
Martin v. Löwis <martin@v.loewis.de>
parents: 40833
diff changeset
695 mode = key = 'w'
d949933cd6b4 Patch #1517891: Make 'a' create the file if it doesn't exist.
Martin v. Löwis <martin@v.loewis.de>
parents: 40833
diff changeset
696 self.fp = open(file, modeDict[mode])
d949933cd6b4 Patch #1517891: Make 'a' create the file if it doesn't exist.
Martin v. Löwis <martin@v.loewis.de>
parents: 40833
diff changeset
697 else:
d949933cd6b4 Patch #1517891: Make 'a' create the file if it doesn't exist.
Martin v. Löwis <martin@v.loewis.de>
parents: 40833
diff changeset
698 raise
17224
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
699 else:
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
700 self._filePassed = 1
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
701 self.fp = file
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
702 self.filename = getattr(file, 'name', None)
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17224
diff changeset
703
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
704 if key == 'r':
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
705 self._GetContents()
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
706 elif key == 'w':
17224
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
707 pass
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
708 elif key == 'a':
25756
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
709 try: # See if file is a zip file
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
710 self._RealGetContents()
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
711 # seek to start of directory and overwrite
25756
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
712 self.fp.seek(self.start_dir, 0)
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
713 except BadZipfile: # file is not a zip file, just append
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
714 self.fp.seek(0, 2)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
715 else:
17312
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
716 if not self._filePassed:
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
717 self.fp.close()
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
718 self.fp = None
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
719 raise RuntimeError, 'Mode must be "r", "w" or "a"'
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
720
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
721 def _GetContents(self):
17312
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
722 """Read the directory, making sure we close the file if the format
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
723 is bad."""
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
724 try:
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
725 self._RealGetContents()
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
726 except BadZipfile:
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
727 if not self._filePassed:
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
728 self.fp.close()
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
729 self.fp = None
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
730 raise
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
731
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
732 def _RealGetContents(self):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
733 """Read in the table of contents for the ZIP file."""
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
734 fp = self.fp
25756
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
735 endrec = _EndRecData(fp)
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
736 if not endrec:
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
737 raise BadZipfile, "File is not a zip file"
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
738 if self.debug > 1:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
739 print endrec
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
740 size_cd = endrec[_ECD_SIZE] # bytes in central directory
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
741 offset_cd = endrec[_ECD_OFFSET] # offset of central directory
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
742 self.comment = endrec[_ECD_COMMENT] # archive comment
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
743
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
744 # "concat" is zero, unless zip was concatenated to another file
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
745 concat = endrec[_ECD_LOCATION] - size_cd - offset_cd
49036
f32517f876b6 Issue #3535: zipfile couldn't read some zip files larger than 2GB.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48265
diff changeset
746 if endrec[_ECD_SIGNATURE] == stringEndArchive64:
f32517f876b6 Issue #3535: zipfile couldn't read some zip files larger than 2GB.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48265
diff changeset
747 # If Zip64 extension structures are present, account for them
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
748 concat -= (sizeEndCentDir64 + sizeEndCentDir64Locator)
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
749
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
750 if self.debug > 2:
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
751 inferred = concat + offset_cd
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
752 print "given, inferred, offset", offset_cd, inferred, concat
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
753 # self.start_dir: Position of start of central directory
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
754 self.start_dir = offset_cd + concat
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
755 fp.seek(self.start_dir, 0)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
756 data = fp.read(size_cd)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
757 fp = cStringIO.StringIO(data)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
758 total = 0
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
759 while total < size_cd:
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
760 centdir = fp.read(sizeCentralDir)
47932
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
761 if centdir[0:4] != stringCentralDir:
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
762 raise BadZipfile, "Bad magic number for central directory"
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
763 centdir = struct.unpack(structCentralDir, centdir)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
764 if self.debug > 2:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
765 print centdir
16840
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
766 filename = fp.read(centdir[_CD_FILENAME_LENGTH])
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
767 # Create ZipInfo instance to store file information
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
768 x = ZipInfo(filename)
16840
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
769 x.extra = fp.read(centdir[_CD_EXTRA_FIELD_LENGTH])
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
770 x.comment = fp.read(centdir[_CD_COMMENT_LENGTH])
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
771 x.header_offset = centdir[_CD_LOCAL_HEADER_OFFSET]
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
772 (x.create_version, x.create_system, x.extract_version, x.reserved,
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
773 x.flag_bits, x.compress_type, t, d,
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
774 x.CRC, x.compress_size, x.file_size) = centdir[1:12]
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
775 x.volume, x.internal_attr, x.external_attr = centdir[15:18]
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
776 # Convert date/time code to (year, month, day, hour, min, sec)
44807
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44802
diff changeset
777 x._raw_time = t
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
778 x.date_time = ( (d>>9)+1980, (d>>5)&0xF, d&0x1F,
12919
31c725d70e65 James C. Ahlstron <jim@interet.com>:
Fred Drake <fdrake@acm.org>
parents: 12197
diff changeset
779 t>>11, (t>>5)&0x3F, (t&0x1F) * 2 )
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
780
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
781 x._decodeExtra()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
782 x.header_offset = x.header_offset + concat
46671
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
783 x.filename = x._decodeFilename()
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
784 self.filelist.append(x)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
785 self.NameToInfo[x.filename] = x
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
786
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
787 # update total bytes read from central directory
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
788 total = (total + sizeCentralDir + centdir[_CD_FILENAME_LENGTH]
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
789 + centdir[_CD_EXTRA_FIELD_LENGTH]
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
790 + centdir[_CD_COMMENT_LENGTH])
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
791
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
792 if self.debug > 2:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
793 print "total", total
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
794
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
795
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
796 def namelist(self):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
797 """Return a list of file names in the archive."""
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
798 l = []
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
799 for data in self.filelist:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
800 l.append(data.filename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
801 return l
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
802
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
803 def infolist(self):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
804 """Return a list of class ZipInfo instances for files in the
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
805 archive."""
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
806 return self.filelist
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
807
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
808 def printdir(self):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
809 """Print a table of contents for the zip file."""
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
810 print "%-46s %19s %12s" % ("File Name", "Modified ", "Size")
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
811 for zinfo in self.filelist:
44676
005021bf5ace Fix 1698398: Zipfile.printdir() crashed because the format string expected a tuple object of length six instead of a time.struct_time object.
Raymond Hettinger <python@rcn.com>
parents: 44576
diff changeset
812 date = "%d-%02d-%02d %02d:%02d:%02d" % zinfo.date_time[:6]
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
813 print "%-46s %s %12d" % (zinfo.filename, date, zinfo.file_size)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
814
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
815 def testzip(self):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
816 """Read all the files and check the CRC."""
48265
f482875e2a03 fix ZipFile.testzip() to work with very large embedded files
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
817 chunk_size = 2 ** 20
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
818 for zinfo in self.filelist:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
819 try:
48265
f482875e2a03 fix ZipFile.testzip() to work with very large embedded files
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
820 # Read by chunks, to avoid an OverflowError or a
f482875e2a03 fix ZipFile.testzip() to work with very large embedded files
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
821 # MemoryError with very large embedded files.
f482875e2a03 fix ZipFile.testzip() to work with very large embedded files
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
822 f = self.open(zinfo.filename, "r")
f482875e2a03 fix ZipFile.testzip() to work with very large embedded files
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
823 while f.read(chunk_size): # Check CRC-32
f482875e2a03 fix ZipFile.testzip() to work with very large embedded files
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
824 pass
29625
7d41785fb60c SF patch #756996: Bare except in ZipFile.testzip()
Raymond Hettinger <python@rcn.com>
parents: 29524
diff changeset
825 except BadZipfile:
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
826 return zinfo.filename
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
827
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
828 def getinfo(self, name):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
829 """Return the instance of ZipInfo given 'name'."""
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
830 info = self.NameToInfo.get(name)
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
831 if info is None:
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
832 raise KeyError(
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
833 'There is no item named %r in the archive' % name)
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
834
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
835 return info
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
836
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
837 def setpassword(self, pwd):
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
838 """Set default password for encrypted files."""
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
839 self.pwd = pwd
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
840
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
841 def read(self, name, pwd=None):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
842 """Return file bytes (as a string) for name."""
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
843 return self.open(name, "r", pwd).read()
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
844
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
845 def open(self, name, mode="r", pwd=None):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
846 """Return file-like object for 'name'."""
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
847 if mode not in ("r", "U", "rU"):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
848 raise RuntimeError, 'open() requires mode "r", "U", or "rU"'
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
849 if not self.fp:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
850 raise RuntimeError, \
14933
715a3e924737 Always use the same name for the exception defined in this module!
Fred Drake <fdrake@acm.org>
parents: 12919
diff changeset
851 "Attempt to read ZIP archive that was already closed"
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
852
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
853 # Only open a new file for instances where we were not
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
854 # given a file object in the constructor
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
855 if self._filePassed:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
856 zef_file = self.fp
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
857 else:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
858 zef_file = open(self.filename, 'rb')
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
859
47195
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46671
diff changeset
860 # Make sure we have an info object
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46671
diff changeset
861 if isinstance(name, ZipInfo):
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46671
diff changeset
862 # 'name' is already an info object
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46671
diff changeset
863 zinfo = name
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46671
diff changeset
864 else:
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46671
diff changeset
865 # Get info object for name
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46671
diff changeset
866 zinfo = self.getinfo(name)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
867
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
868 zef_file.seek(zinfo.header_offset, 0)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
869
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
870 # Skip the file header:
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
871 fheader = zef_file.read(sizeFileHeader)
47932
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
872 if fheader[0:4] != stringFileHeader:
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
873 raise BadZipfile, "Bad magic number for file header"
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
874
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
875 fheader = struct.unpack(structFileHeader, fheader)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
876 fname = zef_file.read(fheader[_FH_FILENAME_LENGTH])
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
877 if fheader[_FH_EXTRA_FIELD_LENGTH]:
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
878 zef_file.read(fheader[_FH_EXTRA_FIELD_LENGTH])
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
879
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
880 if fname != zinfo.orig_filename:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
881 raise BadZipfile, \
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
882 'File name in directory "%s" and header "%s" differ.' % (
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
883 zinfo.orig_filename, fname)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
884
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
885 # check for encrypted flag & handle password
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
886 is_encrypted = zinfo.flag_bits & 0x1
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
887 zd = None
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
888 if is_encrypted:
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
889 if not pwd:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
890 pwd = self.pwd
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
891 if not pwd:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
892 raise RuntimeError, "File %s is encrypted, " \
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
893 "password required for extraction" % name
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
894
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
895 zd = _ZipDecrypter(pwd)
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
896 # The first 12 bytes in the cypher stream is an encryption header
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
897 # used to strengthen the algorithm. The first 11 bytes are
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
898 # completely random, while the 12th contains the MSB of the CRC,
44807
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44802
diff changeset
899 # or the MSB of the file time depending on the header type
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
900 # and is used to check the correctness of the password.
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
901 bytes = zef_file.read(12)
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
902 h = map(zd, bytes[0:12])
44807
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44802
diff changeset
903 if zinfo.flag_bits & 0x8:
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44802
diff changeset
904 # compare against the file type from extended local headers
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44802
diff changeset
905 check_byte = (zinfo._raw_time >> 8) & 0xff
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44802
diff changeset
906 else:
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44802
diff changeset
907 # compare against the CRC otherwise
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44802
diff changeset
908 check_byte = (zinfo.CRC >> 24) & 0xff
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44802
diff changeset
909 if ord(h[11]) != check_byte:
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44802
diff changeset
910 raise RuntimeError("Bad password for file", name)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
911
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
912 # build and return a ZipExtFile
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
913 if zd is None:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
914 zef = ZipExtFile(zef_file, zinfo)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
915 else:
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
916 zef = ZipExtFile(zef_file, zinfo, zd)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
917
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
918 # set universal newlines on ZipExtFile if necessary
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
919 if "U" in mode:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
920 zef.set_univ_newlines(True)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
921 return zef
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
922
44576
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
923 def extract(self, member, path=None, pwd=None):
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
924 """Extract a member from the archive to the current working directory,
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
925 using its full name. Its file information is extracted as accurately
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
926 as possible. `member' may be a filename or a ZipInfo object. You can
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
927 specify a different directory using `path'.
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
928 """
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
929 if not isinstance(member, ZipInfo):
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
930 member = self.getinfo(member)
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
931
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
932 if path is None:
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
933 path = os.getcwd()
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
934
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
935 return self._extract_member(member, path, pwd)
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
936
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
937 def extractall(self, path=None, members=None, pwd=None):
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
938 """Extract all members from the archive to the current working
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
939 directory. `path' specifies a different directory to extract to.
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
940 `members' is optional and must be a subset of the list returned
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
941 by namelist().
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
942 """
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
943 if members is None:
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
944 members = self.namelist()
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
945
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
946 for zipinfo in members:
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
947 self.extract(zipinfo, path, pwd)
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
948
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
949 def _extract_member(self, member, targetpath, pwd):
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
950 """Extract the ZipInfo object 'member' to a physical
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
951 file on the path targetpath.
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
952 """
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
953 # build the destination pathname, replacing
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
954 # forward slashes to platform specific separators.
53752
5399be7a39ba Issue #5692: In :class:`zipfile.Zipfile`, fix wrong path calculation when extracting a file to the root directory.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53673
diff changeset
955 # Strip trailing path separator, unless it represents the root.
5399be7a39ba Issue #5692: In :class:`zipfile.Zipfile`, fix wrong path calculation when extracting a file to the root directory.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53673
diff changeset
956 if (targetpath[-1:] in (os.path.sep, os.path.altsep)
5399be7a39ba Issue #5692: In :class:`zipfile.Zipfile`, fix wrong path calculation when extracting a file to the root directory.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53673
diff changeset
957 and len(os.path.splitdrive(targetpath)[1]) > 1):
44576
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
958 targetpath = targetpath[:-1]
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
959
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
960 # don't include leading "/" from file name if present
50999
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50836
diff changeset
961 if member.filename[0] == '/':
44576
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
962 targetpath = os.path.join(targetpath, member.filename[1:])
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
963 else:
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
964 targetpath = os.path.join(targetpath, member.filename)
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
965
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
966 targetpath = os.path.normpath(targetpath)
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
967
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
968 # Create all upper directories if necessary.
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
969 upperdirs = os.path.dirname(targetpath)
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
970 if upperdirs and not os.path.exists(upperdirs):
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
971 os.makedirs(upperdirs)
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
972
50999
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50836
diff changeset
973 if member.filename[-1] == '/':
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50836
diff changeset
974 os.mkdir(targetpath)
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50836
diff changeset
975 return targetpath
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50836
diff changeset
976
47195
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46671
diff changeset
977 source = self.open(member, pwd=pwd)
44576
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
978 target = file(targetpath, "wb")
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
979 shutil.copyfileobj(source, target)
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
980 source.close()
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
981 target.close()
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
982
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
983 return targetpath
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
984
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
985 def _writecheck(self, zinfo):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
986 """Check for errors before writing a file to the archive."""
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 22867
diff changeset
987 if zinfo.filename in self.NameToInfo:
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
988 if self.debug: # Warning for duplicate names
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
989 print "Duplicate name:", zinfo.filename
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
990 if self.mode not in ("w", "a"):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
991 raise RuntimeError, 'write() requires mode "w" or "a"'
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
992 if not self.fp:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
993 raise RuntimeError, \
14933
715a3e924737 Always use the same name for the exception defined in this module!
Fred Drake <fdrake@acm.org>
parents: 12919
diff changeset
994 "Attempt to write ZIP archive that was already closed"
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
995 if zinfo.compress_type == ZIP_DEFLATED and not zlib:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
996 raise RuntimeError, \
14933
715a3e924737 Always use the same name for the exception defined in this module!
Fred Drake <fdrake@acm.org>
parents: 12919
diff changeset
997 "Compression requires the (missing) zlib module"
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
998 if zinfo.compress_type not in (ZIP_STORED, ZIP_DEFLATED):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
999 raise RuntimeError, \
14933
715a3e924737 Always use the same name for the exception defined in this module!
Fred Drake <fdrake@acm.org>
parents: 12919
diff changeset
1000 "That compression method is not supported"
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1001 if zinfo.file_size > ZIP64_LIMIT:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1002 if not self._allowZip64:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1003 raise LargeZipFile("Filesize would require ZIP64 extensions")
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1004 if zinfo.header_offset > ZIP64_LIMIT:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1005 if not self._allowZip64:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1006 raise LargeZipFile("Zipfile size would require ZIP64 extensions")
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1007
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1008 def write(self, filename, arcname=None, compress_type=None):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
1009 """Put the bytes from filename into the archive under the name
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
1010 arcname."""
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
1011 if not self.fp:
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
1012 raise RuntimeError(
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
1013 "Attempt to write to ZIP archive that was already closed")
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
1014
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1015 st = os.stat(filename)
50999
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50836
diff changeset
1016 isdir = stat.S_ISDIR(st.st_mode)
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 23620
diff changeset
1017 mtime = time.localtime(st.st_mtime)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1018 date_time = mtime[0:6]
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1019 # Create ZipInfo instance to store file information
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1020 if arcname is None:
36609
46ec948cbde8 Bug #1413790: zipfile now sanitizes absolute archive names that are
Georg Brandl <georg@python.org>
parents: 36449
diff changeset
1021 arcname = filename
46ec948cbde8 Bug #1413790: zipfile now sanitizes absolute archive names that are
Georg Brandl <georg@python.org>
parents: 36449
diff changeset
1022 arcname = os.path.normpath(os.path.splitdrive(arcname)[1])
46ec948cbde8 Bug #1413790: zipfile now sanitizes absolute archive names that are
Georg Brandl <georg@python.org>
parents: 36449
diff changeset
1023 while arcname[0] in (os.sep, os.altsep):
46ec948cbde8 Bug #1413790: zipfile now sanitizes absolute archive names that are
Georg Brandl <georg@python.org>
parents: 36449
diff changeset
1024 arcname = arcname[1:]
50999
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50836
diff changeset
1025 if isdir:
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50836
diff changeset
1026 arcname += '/'
36609
46ec948cbde8 Bug #1413790: zipfile now sanitizes absolute archive names that are
Georg Brandl <georg@python.org>
parents: 36449
diff changeset
1027 zinfo = ZipInfo(arcname, date_time)
32580
230397bc7e32 [Bug #835415] AIX can return modes that are >65536, which causes an OverflowError. Fix from Albert Chin
Andrew M. Kuchling <amk@amk.ca>
parents: 29625
diff changeset
1028 zinfo.external_attr = (st[0] & 0xFFFF) << 16L # Unix attributes
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1029 if compress_type is None:
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
1030 zinfo.compress_type = self.compression
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1031 else:
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
1032 zinfo.compress_type = compress_type
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1033
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1034 zinfo.file_size = st.st_size
19737
56c549a253a6 [ #458701 ] Patch to zipfile.py for Java
Finn Bock <bckfnn@worldonline.dk>
parents: 18746
diff changeset
1035 zinfo.flag_bits = 0x00
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
1036 zinfo.header_offset = self.fp.tell() # Start of header bytes
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1037
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1038 self._writecheck(zinfo)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1039 self._didModify = True
50999
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50836
diff changeset
1040
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50836
diff changeset
1041 if isdir:
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50836
diff changeset
1042 zinfo.file_size = 0
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50836
diff changeset
1043 zinfo.compress_size = 0
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50836
diff changeset
1044 zinfo.CRC = 0
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50836
diff changeset
1045 self.filelist.append(zinfo)
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50836
diff changeset
1046 self.NameToInfo[zinfo.filename] = zinfo
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50836
diff changeset
1047 self.fp.write(zinfo.FileHeader())
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50836
diff changeset
1048 return
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50836
diff changeset
1049
53964
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1050 with open(filename, "rb") as fp:
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1051 # Must overwrite CRC and sizes with correct data later
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1052 zinfo.CRC = CRC = 0
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1053 zinfo.compress_size = compress_size = 0
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1054 zinfo.file_size = file_size = 0
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1055 self.fp.write(zinfo.FileHeader())
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1056 if zinfo.compress_type == ZIP_DEFLATED:
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1057 cmpr = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION,
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1058 zlib.DEFLATED, -15)
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1059 else:
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1060 cmpr = None
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1061 while 1:
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1062 buf = fp.read(1024 * 8)
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1063 if not buf:
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1064 break
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1065 file_size = file_size + len(buf)
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1066 CRC = crc32(buf, CRC) & 0xffffffff
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1067 if cmpr:
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1068 buf = cmpr.compress(buf)
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1069 compress_size = compress_size + len(buf)
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1070 self.fp.write(buf)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1071 if cmpr:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1072 buf = cmpr.flush()
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1073 compress_size = compress_size + len(buf)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1074 self.fp.write(buf)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1075 zinfo.compress_size = compress_size
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1076 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1077 zinfo.compress_size = file_size
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1078 zinfo.CRC = CRC
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1079 zinfo.file_size = file_size
19737
56c549a253a6 [ #458701 ] Patch to zipfile.py for Java
Finn Bock <bckfnn@worldonline.dk>
parents: 18746
diff changeset
1080 # Seek backwards and write CRC and file sizes
19953
0f9df563b65d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 19737
diff changeset
1081 position = self.fp.tell() # Preserve current position in file
19737
56c549a253a6 [ #458701 ] Patch to zipfile.py for Java
Finn Bock <bckfnn@worldonline.dk>
parents: 18746
diff changeset
1082 self.fp.seek(zinfo.header_offset + 14, 0)
45902
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1083 self.fp.write(struct.pack("<LLL", zinfo.CRC, zinfo.compress_size,
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1084 zinfo.file_size))
19737
56c549a253a6 [ #458701 ] Patch to zipfile.py for Java
Finn Bock <bckfnn@worldonline.dk>
parents: 18746
diff changeset
1085 self.fp.seek(position, 0)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1086 self.filelist.append(zinfo)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1087 self.NameToInfo[zinfo.filename] = zinfo
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1088
26611
8771993c9aac Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents: 25756
diff changeset
1089 def writestr(self, zinfo_or_arcname, bytes):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
1090 """Write a file into the archive. The contents is the string
26611
8771993c9aac Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents: 25756
diff changeset
1091 'bytes'. 'zinfo_or_arcname' is either a ZipInfo instance or
8771993c9aac Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents: 25756
diff changeset
1092 the name of the file in the archive."""
8771993c9aac Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents: 25756
diff changeset
1093 if not isinstance(zinfo_or_arcname, ZipInfo):
8771993c9aac Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents: 25756
diff changeset
1094 zinfo = ZipInfo(filename=zinfo_or_arcname,
44676
005021bf5ace Fix 1698398: Zipfile.printdir() crashed because the format string expected a tuple object of length six instead of a time.struct_time object.
Raymond Hettinger <python@rcn.com>
parents: 44576
diff changeset
1095 date_time=time.localtime(time.time())[:6])
26611
8771993c9aac Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents: 25756
diff changeset
1096 zinfo.compress_type = self.compression
48073
972f489ae356 #3394: zipfile.writestr doesn't set external attributes, so files are extracted mode 000 on Unix
Antoine Pitrou <solipsis@pitrou.net>
parents: 47932
diff changeset
1097 zinfo.external_attr = 0600 << 16
26611
8771993c9aac Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents: 25756
diff changeset
1098 else:
8771993c9aac Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents: 25756
diff changeset
1099 zinfo = zinfo_or_arcname
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
1100
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
1101 if not self.fp:
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
1102 raise RuntimeError(
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
1103 "Attempt to write to ZIP archive that was already closed")
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
1104
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1105 zinfo.file_size = len(bytes) # Uncompressed size
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1106 zinfo.header_offset = self.fp.tell() # Start of header bytes
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1107 self._writecheck(zinfo)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1108 self._didModify = True
45902
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1109 zinfo.CRC = crc32(bytes) & 0xffffffff # CRC-32 checksum
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1110 if zinfo.compress_type == ZIP_DEFLATED:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1111 co = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION,
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1112 zlib.DEFLATED, -15)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1113 bytes = co.compress(bytes) + co.flush()
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
1114 zinfo.compress_size = len(bytes) # Compressed size
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1115 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1116 zinfo.compress_size = zinfo.file_size
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
1117 zinfo.header_offset = self.fp.tell() # Start of header bytes
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1118 self.fp.write(zinfo.FileHeader())
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1119 self.fp.write(bytes)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1120 self.fp.flush()
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1121 if zinfo.flag_bits & 0x08:
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
1122 # Write CRC and file sizes after the file data
32590
f3f4765efe66 Make struct formats for specifying file size to be unsigned instead of signed
Brett Cannon <bcannon@gmail.com>
parents: 32580
diff changeset
1123 self.fp.write(struct.pack("<lLL", zinfo.CRC, zinfo.compress_size,
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
1124 zinfo.file_size))
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1125 self.filelist.append(zinfo)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1126 self.NameToInfo[zinfo.filename] = zinfo
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1127
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1128 def __del__(self):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
1129 """Call the "close()" method in case the user forgot."""
21098
82d8413636e7 SF bug 486480: zipfile __del__ is broken
Tim Peters <tim.peters@gmail.com>
parents: 20020
diff changeset
1130 self.close()
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1131
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1132 def close(self):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
1133 """Close the file, and for mode "w" and "a" write the ending
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
1134 records."""
21098
82d8413636e7 SF bug 486480: zipfile __del__ is broken
Tim Peters <tim.peters@gmail.com>
parents: 20020
diff changeset
1135 if self.fp is None:
82d8413636e7 SF bug 486480: zipfile __del__ is broken
Tim Peters <tim.peters@gmail.com>
parents: 20020
diff changeset
1136 return
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
1137
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1138 if self.mode in ("w", "a") and self._didModify: # write ending records
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1139 count = 0
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1140 pos1 = self.fp.tell()
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
1141 for zinfo in self.filelist: # write central directory
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1142 count = count + 1
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1143 dt = zinfo.date_time
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1144 dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2]
21251
45e1177424e1 SF bug #488514: -Qnew needs work
Tim Peters <tim.peters@gmail.com>
parents: 21098
diff changeset
1145 dostime = dt[3] << 11 | dt[4] << 5 | (dt[5] // 2)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1146 extra = []
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1147 if zinfo.file_size > ZIP64_LIMIT \
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1148 or zinfo.compress_size > ZIP64_LIMIT:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1149 extra.append(zinfo.file_size)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1150 extra.append(zinfo.compress_size)
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
1151 file_size = 0xffffffff
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
1152 compress_size = 0xffffffff
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1153 else:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1154 file_size = zinfo.file_size
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1155 compress_size = zinfo.compress_size
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1156
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1157 if zinfo.header_offset > ZIP64_LIMIT:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1158 extra.append(zinfo.header_offset)
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
1159 header_offset = 0xffffffffL
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1160 else:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1161 header_offset = zinfo.header_offset
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1162
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1163 extra_data = zinfo.extra
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1164 if extra:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1165 # Append a ZIP64 field to the extra's
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1166 extra_data = struct.pack(
45896
733c3ee19899 Use zlib's crc32 routine instead of binascii when available. zlib's is faster
Gregory P. Smith <greg@mad-scientist.com>
parents: 44809
diff changeset
1167 '<HH' + 'Q'*len(extra),
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1168 1, 8*len(extra), *extra) + extra_data
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
1169
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1170 extract_version = max(45, zinfo.extract_version)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1171 create_version = max(45, zinfo.create_version)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1172 else:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1173 extract_version = zinfo.extract_version
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1174 create_version = zinfo.create_version
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1175
45902
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1176 try:
46671
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
1177 filename, flag_bits = zinfo._encodeFilenameFlags()
45902
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1178 centdir = struct.pack(structCentralDir,
47932
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
1179 stringCentralDir, create_version,
45902
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1180 zinfo.create_system, extract_version, zinfo.reserved,
46671
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
1181 flag_bits, zinfo.compress_type, dostime, dosdate,
45902
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1182 zinfo.CRC, compress_size, file_size,
46671
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
1183 len(filename), len(extra_data), len(zinfo.comment),
45902
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1184 0, zinfo.internal_attr, zinfo.external_attr,
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1185 header_offset)
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1186 except DeprecationWarning:
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1187 print >>sys.stderr, (structCentralDir,
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1188 stringCentralDir, create_version,
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1189 zinfo.create_system, extract_version, zinfo.reserved,
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1190 zinfo.flag_bits, zinfo.compress_type, dostime, dosdate,
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1191 zinfo.CRC, compress_size, file_size,
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1192 len(zinfo.filename), len(extra_data), len(zinfo.comment),
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1193 0, zinfo.internal_attr, zinfo.external_attr,
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1194 header_offset)
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1195 raise
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1196 self.fp.write(centdir)
46671
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
1197 self.fp.write(filename)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1198 self.fp.write(extra_data)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1199 self.fp.write(zinfo.comment)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1200
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1201 pos2 = self.fp.tell()
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1202 # Write end-of-zip-archive record
50824
dc31be50c405 #3997: zipfiles generated with more than 65536 files could not be opened
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50300
diff changeset
1203 centDirCount = count
dc31be50c405 #3997: zipfiles generated with more than 65536 files could not be opened
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50300
diff changeset
1204 centDirSize = pos2 - pos1
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
1205 centDirOffset = pos1
50824
dc31be50c405 #3997: zipfiles generated with more than 65536 files could not be opened
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50300
diff changeset
1206 if (centDirCount >= ZIP_FILECOUNT_LIMIT or
dc31be50c405 #3997: zipfiles generated with more than 65536 files could not be opened
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50300
diff changeset
1207 centDirOffset > ZIP64_LIMIT or
dc31be50c405 #3997: zipfiles generated with more than 65536 files could not be opened
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50300
diff changeset
1208 centDirSize > ZIP64_LIMIT):
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1209 # Need to write the ZIP64 end-of-archive records
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1210 zip64endrec = struct.pack(
47932
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
1211 structEndArchive64, stringEndArchive64,
50824
dc31be50c405 #3997: zipfiles generated with more than 65536 files could not be opened
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50300
diff changeset
1212 44, 45, 45, 0, 0, centDirCount, centDirCount,
dc31be50c405 #3997: zipfiles generated with more than 65536 files could not be opened
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50300
diff changeset
1213 centDirSize, centDirOffset)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1214 self.fp.write(zip64endrec)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1215
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1216 zip64locrec = struct.pack(
47932
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
1217 structEndArchive64Locator,
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
1218 stringEndArchive64Locator, 0, pos2, 1)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1219 self.fp.write(zip64locrec)
50824
dc31be50c405 #3997: zipfiles generated with more than 65536 files could not be opened
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50300
diff changeset
1220 centDirCount = min(centDirCount, 0xFFFF)
dc31be50c405 #3997: zipfiles generated with more than 65536 files could not be opened
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50300
diff changeset
1221 centDirSize = min(centDirSize, 0xFFFFFFFF)
dc31be50c405 #3997: zipfiles generated with more than 65536 files could not be opened
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50300
diff changeset
1222 centDirOffset = min(centDirOffset, 0xFFFFFFFF)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1223
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
1224 # check for valid comment length
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
1225 if len(self.comment) >= ZIP_MAX_COMMENT:
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
1226 if self.debug > 0:
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
1227 msg = 'Archive comment is too long; truncating to %d bytes' \
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
1228 % ZIP_MAX_COMMENT
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
1229 self.comment = self.comment[:ZIP_MAX_COMMENT]
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1230
47932
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
1231 endrec = struct.pack(structEndArchive, stringEndArchive,
50824
dc31be50c405 #3997: zipfiles generated with more than 65536 files could not be opened
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50300
diff changeset
1232 0, 0, centDirCount, centDirCount,
dc31be50c405 #3997: zipfiles generated with more than 65536 files could not be opened
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50300
diff changeset
1233 centDirSize, centDirOffset, len(self.comment))
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
1234 self.fp.write(endrec)
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
1235 self.fp.write(self.comment)
17517
1e23d91c0a82 Mark Favas points out that there's an 'self.fp.flush()' call in the
Guido van Rossum <guido@python.org>
parents: 17377
diff changeset
1236 self.fp.flush()
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
1237
17224
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
1238 if not self._filePassed:
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
1239 self.fp.close()
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1240 self.fp = None
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1241
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1242
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1243 class PyZipFile(ZipFile):
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
1244 """Class to create ZIP archives with Python library files and packages."""
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
1245
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1246 def writepy(self, pathname, basename = ""):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1247 """Add all files from "pathname" to the ZIP archive.
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1248
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
1249 If pathname is a package directory, search the directory and
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
1250 all package subdirectories recursively for all *.py and enter
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
1251 the modules into the archive. If pathname is a plain
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
1252 directory, listdir *.py and enter all modules. Else, pathname
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
1253 must be a Python *.py file and the module will be put into the
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
1254 archive. Added modules are always module.pyo or module.pyc.
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
1255 This method will compile the module.py into module.pyc if
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
1256 necessary.
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
1257 """
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1258 dir, name = os.path.split(pathname)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1259 if os.path.isdir(pathname):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1260 initname = os.path.join(pathname, "__init__.py")
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1261 if os.path.isfile(initname):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1262 # This is a package directory, add it
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1263 if basename:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1264 basename = "%s/%s" % (basename, name)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1265 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1266 basename = name
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1267 if self.debug:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1268 print "Adding package in", pathname, "as", basename
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1269 fname, arcname = self._get_codename(initname[0:-3], basename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1270 if self.debug:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1271 print "Adding", arcname
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1272 self.write(fname, arcname)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1273 dirlist = os.listdir(pathname)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1274 dirlist.remove("__init__.py")
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1275 # Add all *.py files and package subdirectories
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1276 for filename in dirlist:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1277 path = os.path.join(pathname, filename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1278 root, ext = os.path.splitext(filename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1279 if os.path.isdir(path):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1280 if os.path.isfile(os.path.join(path, "__init__.py")):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1281 # This is a package directory, add it
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1282 self.writepy(path, basename) # Recursive call
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1283 elif ext == ".py":
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1284 fname, arcname = self._get_codename(path[0:-3],
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1285 basename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1286 if self.debug:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1287 print "Adding", arcname
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1288 self.write(fname, arcname)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1289 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1290 # This is NOT a package directory, add its files at top level
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1291 if self.debug:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1292 print "Adding files from directory", pathname
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1293 for filename in os.listdir(pathname):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1294 path = os.path.join(pathname, filename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1295 root, ext = os.path.splitext(filename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1296 if ext == ".py":
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1297 fname, arcname = self._get_codename(path[0:-3],
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1298 basename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1299 if self.debug:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1300 print "Adding", arcname
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1301 self.write(fname, arcname)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1302 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1303 if pathname[-3:] != ".py":
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1304 raise RuntimeError, \
14933
715a3e924737 Always use the same name for the exception defined in this module!
Fred Drake <fdrake@acm.org>
parents: 12919
diff changeset
1305 'Files added with writepy() must end with ".py"'
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1306 fname, arcname = self._get_codename(pathname[0:-3], basename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1307 if self.debug:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1308 print "Adding file", arcname
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1309 self.write(fname, arcname)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1310
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1311 def _get_codename(self, pathname, basename):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1312 """Return (filename, archivename) for the path.
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1313
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
1314 Given a module name path, return the correct file path and
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
1315 archive name, compiling if necessary. For example, given
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
1316 /python/lib/string, return (/python/lib/string.pyc, string).
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
1317 """
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1318 file_py = pathname + ".py"
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1319 file_pyc = pathname + ".pyc"
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1320 file_pyo = pathname + ".pyo"
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1321 if os.path.isfile(file_pyo) and \
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 23620
diff changeset
1322 os.stat(file_pyo).st_mtime >= os.stat(file_py).st_mtime:
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
1323 fname = file_pyo # Use .pyo file
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1324 elif not os.path.isfile(file_pyc) or \
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 23620
diff changeset
1325 os.stat(file_pyc).st_mtime < os.stat(file_py).st_mtime:
14975
27eab8032766 Delay import of py_compile until needed, since is is only used by the
Fred Drake <fdrake@acm.org>
parents: 14933
diff changeset
1326 import py_compile
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1327 if self.debug:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1328 print "Compiling", file_py
27250
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 26611
diff changeset
1329 try:
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 26611
diff changeset
1330 py_compile.compile(file_py, file_pyc, None, True)
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 26611
diff changeset
1331 except py_compile.PyCompileError,err:
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 26611
diff changeset
1332 print err.msg
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1333 fname = file_pyc
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1334 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1335 fname = file_pyc
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1336 archivename = os.path.split(fname)[1]
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1337 if basename:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1338 archivename = "%s/%s" % (basename, archivename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1339 return (fname, archivename)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1340
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1341
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1342 def main(args = None):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1343 import textwrap
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1344 USAGE=textwrap.dedent("""\
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1345 Usage:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1346 zipfile.py -l zipfile.zip # Show listing of a zipfile
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1347 zipfile.py -t zipfile.zip # Test if a zipfile is valid
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1348 zipfile.py -e zipfile.zip target # Extract zipfile into target dir
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1349 zipfile.py -c zipfile.zip src ... # Create zipfile from sources
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1350 """)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1351 if args is None:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1352 args = sys.argv[1:]
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1353
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1354 if not args or args[0] not in ('-l', '-c', '-e', '-t'):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1355 print USAGE
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1356 sys.exit(1)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1357
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1358 if args[0] == '-l':
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1359 if len(args) != 2:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1360 print USAGE
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1361 sys.exit(1)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1362 zf = ZipFile(args[1], 'r')
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1363 zf.printdir()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1364 zf.close()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1365
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1366 elif args[0] == '-t':
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1367 if len(args) != 2:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1368 print USAGE
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1369 sys.exit(1)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1370 zf = ZipFile(args[1], 'r')
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1371 zf.testzip()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1372 print "Done testing"
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1373
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1374 elif args[0] == '-e':
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1375 if len(args) != 3:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1376 print USAGE
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1377 sys.exit(1)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1378
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1379 zf = ZipFile(args[1], 'r')
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1380 out = args[2]
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1381 for path in zf.namelist():
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
1382 if path.startswith('./'):
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1383 tgt = os.path.join(out, path[2:])
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1384 else:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1385 tgt = os.path.join(out, path)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1386
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1387 tgtdir = os.path.dirname(tgt)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1388 if not os.path.exists(tgtdir):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1389 os.makedirs(tgtdir)
53964
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1390 with open(tgt, 'wb') as fp:
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1391 fp.write(zf.read(path))
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1392 zf.close()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1393
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1394 elif args[0] == '-c':
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1395 if len(args) < 3:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1396 print USAGE
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1397 sys.exit(1)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1398
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1399 def addToZip(zf, path, zippath):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1400 if os.path.isfile(path):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1401 zf.write(path, zippath, ZIP_DEFLATED)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1402 elif os.path.isdir(path):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1403 for nm in os.listdir(path):
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
1404 addToZip(zf,
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1405 os.path.join(path, nm), os.path.join(zippath, nm))
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
1406 # else: ignore
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1407
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1408 zf = ZipFile(args[1], 'w', allowZip64=True)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1409 for src in args[2:]:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1410 addToZip(zf, src, os.path.basename(src))
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1411
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1412 zf.close()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1413
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1414 if __name__ == "__main__":
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1415 main()