Mercurial > cpython-lru_cache-weakref
annotate Lib/zipfile.py @ 58925:eecf1bef052d 2.6
Merged revisions 78207 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line
#7930: fix stripid
........
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Tue, 16 Feb 2010 23:29:44 +0000 |
| parents | 99e7c1b8b1e1 |
| children | 787b969d37f0 |
| 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 |
|
51000
6f761e5714e0
Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
50877
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 |
|
50826
486e2579c978
Merged revisions 68661 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
49036
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 |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 def is_zipfile(filename): |
|
25756
63225811498d
Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents:
24756
diff
changeset
|
132 """Quickly see if file is a ZIP file by checking the magic number.""" |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 try: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 fpin = open(filename, "rb") |
|
25756
63225811498d
Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents:
24756
diff
changeset
|
135 endrec = _EndRecData(fpin) |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 fpin.close() |
|
25756
63225811498d
Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents:
24756
diff
changeset
|
137 if endrec: |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
21251
diff
changeset
|
138 return True # file has correct magic number |
| 17805 | 139 except IOError: |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 pass |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
21251
diff
changeset
|
141 return False |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
143 def _EndRecData64(fpin, offset, endrec): |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
144 """ |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
145 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
|
146 """ |
|
47882
acfad8640e21
Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents:
47195
diff
changeset
|
147 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
|
148 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
|
149 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
|
150 if sig != stringEndArchive64Locator: |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
151 return endrec |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
152 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
153 if diskno != 0 or disks != 1: |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
154 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
|
155 |
|
38845
cacd47a61e79
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
38838
diff
changeset
|
156 # 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
|
157 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
|
158 data = fpin.read(sizeEndCentDir64) |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
159 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
|
160 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
|
161 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
|
162 if sig != stringEndArchive64: |
|
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 # 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
|
166 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
|
167 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
|
168 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
|
169 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
|
170 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
|
171 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
|
172 endrec[_ECD_OFFSET] = diroffset |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
173 return endrec |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
174 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
175 |
|
25756
63225811498d
Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents:
24756
diff
changeset
|
176 def _EndRecData(fpin): |
|
63225811498d
Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents:
24756
diff
changeset
|
177 """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
|
178 |
|
63225811498d
Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents:
24756
diff
changeset
|
179 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
|
180 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
|
181 |
|
acfad8640e21
Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents:
47195
diff
changeset
|
182 # 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
|
183 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
|
184 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
|
185 |
|
acfad8640e21
Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents:
47195
diff
changeset
|
186 # 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
|
187 # "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
|
188 # file if this is the case). |
|
58175
99e7c1b8b1e1
Merged revisions 74245 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
54862
diff
changeset
|
189 try: |
|
99e7c1b8b1e1
Merged revisions 74245 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
54862
diff
changeset
|
190 fpin.seek(-sizeEndCentDir, 2) |
|
99e7c1b8b1e1
Merged revisions 74245 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
54862
diff
changeset
|
191 except IOError: |
|
99e7c1b8b1e1
Merged revisions 74245 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
54862
diff
changeset
|
192 return None |
|
25756
63225811498d
Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents:
24756
diff
changeset
|
193 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
|
194 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
|
195 # 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
|
196 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
|
197 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
|
198 |
|
acfad8640e21
Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents:
47195
diff
changeset
|
199 # 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
|
200 endrec.append("") |
|
acfad8640e21
Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents:
47195
diff
changeset
|
201 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
|
202 |
|
50877
6a76abd616de
Merged revisions 68678 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
50826
diff
changeset
|
203 # Try to read the "Zip64 end of central directory" structure |
|
6a76abd616de
Merged revisions 68678 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
50826
diff
changeset
|
204 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
|
205 |
|
acfad8640e21
Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents:
47195
diff
changeset
|
206 # 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
|
207 # 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
|
208 # 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
|
209 # 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
|
210 # 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
|
211 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
|
212 fpin.seek(maxCommentStart, 0) |
|
25756
63225811498d
Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents:
24756
diff
changeset
|
213 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
|
214 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
|
215 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
|
216 # 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
|
217 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
|
218 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
|
219 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
|
220 # 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
|
221 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
|
222 # 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
|
223 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
|
224 endrec.append(maxCommentStart + start) |
|
50877
6a76abd616de
Merged revisions 68678 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
50826
diff
changeset
|
225 |
|
6a76abd616de
Merged revisions 68678 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
50826
diff
changeset
|
226 # Try to read the "Zip64 end of central directory" structure |
|
6a76abd616de
Merged revisions 68678 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
50826
diff
changeset
|
227 return _EndRecData64(fpin, maxCommentStart + start - filesize, |
|
6a76abd616de
Merged revisions 68678 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
50826
diff
changeset
|
228 endrec) |
|
47882
acfad8640e21
Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents:
47195
diff
changeset
|
229 |
|
acfad8640e21
Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents:
47195
diff
changeset
|
230 # 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
|
231 return |
|
25756
63225811498d
Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents:
24756
diff
changeset
|
232 |
|
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
|
233 |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
234 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
|
235 """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
|
236 |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
237 __slots__ = ( |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
238 'orig_filename', |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
239 'filename', |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
240 'date_time', |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
241 'compress_type', |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
242 'comment', |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
243 'extra', |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
244 'create_system', |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
245 'create_version', |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
246 'extract_version', |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
247 'reserved', |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
248 'flag_bits', |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
249 'volume', |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
250 'internal_attr', |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
251 'external_attr', |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
252 'header_offset', |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
253 'CRC', |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
254 'compress_size', |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
255 '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
|
256 '_raw_time', |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
257 ) |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
258 |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
259 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
|
260 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
|
261 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
262 # 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
|
263 # 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
|
264 null_byte = filename.find(chr(0)) |
|
87f30fcfad48
SF patch #755987 (Jim Ahlstrom):
Greg Ward <gward@python.net>
parents:
27250
diff
changeset
|
265 if null_byte >= 0: |
|
87f30fcfad48
SF patch #755987 (Jim Ahlstrom):
Greg Ward <gward@python.net>
parents:
27250
diff
changeset
|
266 filename = filename[0:null_byte] |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
267 # 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
|
268 # 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
|
269 # ZIP format specification. |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
270 if os.sep != "/" and os.sep in filename: |
|
29523
87f30fcfad48
SF patch #755987 (Jim Ahlstrom):
Greg Ward <gward@python.net>
parents:
27250
diff
changeset
|
271 filename = filename.replace(os.sep, "/") |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
272 |
|
29523
87f30fcfad48
SF patch #755987 (Jim Ahlstrom):
Greg Ward <gward@python.net>
parents:
27250
diff
changeset
|
273 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
|
274 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
|
275 # 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
|
276 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
|
277 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
|
278 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
|
279 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
|
280 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
|
281 else: |
|
4ed30eaa0256
Patch #1412872: zipfile: use correct system type on unixy systems.
Martin v. Löwis <martin@v.loewis.de>
parents:
34594
diff
changeset
|
282 # 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
|
283 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
|
284 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
|
285 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
|
286 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
|
287 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
|
288 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
|
289 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
|
290 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
|
291 # 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
|
292 # 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
|
293 # 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
|
294 # 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
|
295 # 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
|
296 |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
297 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
|
298 """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
|
299 dt = self.date_time |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
300 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
|
301 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
|
302 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
|
303 # 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
|
304 CRC = compress_size = file_size = 0 |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
305 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
|
306 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
|
307 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
|
308 file_size = self.file_size |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
309 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
310 extra = self.extra |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
311 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
312 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
|
313 # 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
|
314 # 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
|
315 fmt = '<HHQQ' |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
316 extra = extra + struct.pack(fmt, |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
317 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
|
318 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
|
319 compress_size = 0xffffffff |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
320 self.extract_version = max(45, self.extract_version) |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
321 self.create_version = max(45, self.extract_version) |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
322 |
|
46671
8e33f316ce14
Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents:
45902
diff
changeset
|
323 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
|
324 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
|
325 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
|
326 self.compress_type, dostime, dosdate, CRC, |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
327 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
|
328 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
|
329 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
|
330 |
|
8e33f316ce14
Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents:
45902
diff
changeset
|
331 def _encodeFilenameFlags(self): |
|
8e33f316ce14
Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents:
45902
diff
changeset
|
332 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
|
333 try: |
|
8e33f316ce14
Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents:
45902
diff
changeset
|
334 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
|
335 except UnicodeEncodeError: |
|
8e33f316ce14
Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents:
45902
diff
changeset
|
336 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
|
337 else: |
|
8e33f316ce14
Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents:
45902
diff
changeset
|
338 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
|
339 |
|
8e33f316ce14
Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents:
45902
diff
changeset
|
340 def _decodeFilename(self): |
|
8e33f316ce14
Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents:
45902
diff
changeset
|
341 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
|
342 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
|
343 else: |
|
8e33f316ce14
Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents:
45902
diff
changeset
|
344 return self.filename |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
345 |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
346 def _decodeExtra(self): |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
347 # Try to decode the extra field. |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
348 extra = self.extra |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
349 unpack = struct.unpack |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
350 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
|
351 tp, ln = unpack('<HH', extra[:4]) |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
352 if tp == 1: |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
353 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
|
354 counts = unpack('<QQQ', extra[4:28]) |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
355 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
|
356 counts = unpack('<QQ', extra[4:20]) |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
357 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
|
358 counts = unpack('<Q', extra[4:12]) |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
359 elif ln == 0: |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
360 counts = () |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
361 else: |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
362 raise RuntimeError, "Corrupt extra field %s"%(ln,) |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
363 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
364 idx = 0 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
365 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
366 # 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
|
367 if self.file_size in (0xffffffffffffffffL, 0xffffffffL): |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
368 self.file_size = counts[idx] |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
369 idx += 1 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
370 |
|
47882
acfad8640e21
Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents:
47195
diff
changeset
|
371 if self.compress_size == 0xFFFFFFFFL: |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
372 self.compress_size = counts[idx] |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
373 idx += 1 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
374 |
|
47882
acfad8640e21
Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents:
47195
diff
changeset
|
375 if self.header_offset == 0xffffffffL: |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
376 old = self.header_offset |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
377 self.header_offset = 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 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
380 extra = extra[ln+4:] |
|
38845
cacd47a61e79
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
38838
diff
changeset
|
381 |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
382 |
|
40833
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
383 class _ZipDecrypter: |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
384 """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
|
385 |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
386 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
|
387 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
|
388 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
|
389 |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
390 Usage: |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
391 zd = _ZipDecrypter(mypwd) |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
392 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
|
393 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
|
394 """ |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
395 |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
396 def _GenerateCRCTable(): |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
397 """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
|
398 |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
399 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
|
400 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
|
401 relying on binascii.crc32(). |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
402 """ |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
403 poly = 0xedb88320 |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
404 table = [0] * 256 |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
405 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
|
406 crc = i |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
407 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
|
408 if crc & 1: |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
409 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
|
410 else: |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
411 crc = ((crc >> 1) & 0x7FFFFFFF) |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
412 table[i] = crc |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
413 return table |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
414 crctable = _GenerateCRCTable() |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
415 |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
416 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
|
417 """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
|
418 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
|
419 |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
420 def __init__(self, pwd): |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
421 self.key0 = 305419896 |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
422 self.key1 = 591751049 |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
423 self.key2 = 878082192 |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
424 for p in pwd: |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
425 self._UpdateKeys(p) |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
426 |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
427 def _UpdateKeys(self, c): |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
428 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
|
429 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
|
430 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
|
431 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
|
432 |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
433 def __call__(self, c): |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
434 """Decrypt a single character.""" |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
435 c = ord(c) |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
436 k = self.key2 | 2 |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
437 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
|
438 c = chr(c) |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
439 self._UpdateKeys(c) |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
440 return c |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
441 |
|
41012
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
442 class ZipExtFile: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
443 """File-like object for reading an archive member. |
|
41133
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
41012
diff
changeset
|
444 Is returned by ZipFile.open(). |
|
41012
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
445 """ |
|
41133
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
41012
diff
changeset
|
446 |
|
41012
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
447 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
|
448 self.fileobj = fileobj |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
449 self.decrypter = decrypt |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
450 self.bytes_read = 0L |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
451 self.rawbuffer = '' |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
452 self.readbuffer = '' |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
453 self.linebuffer = '' |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
454 self.eof = False |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
455 self.univ_newlines = False |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
456 self.nlSeps = ("\n", ) |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
457 self.lastdiscard = '' |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
458 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
459 self.compress_type = zipinfo.compress_type |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
460 self.compress_size = zipinfo.compress_size |
|
41133
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
41012
diff
changeset
|
461 |
|
41012
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
462 self.closed = False |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
463 self.mode = "r" |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
464 self.name = zipinfo.filename |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
465 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
466 # 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
|
467 self.compreadsize = 64*1024 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
468 if self.compress_type == ZIP_DEFLATED: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
469 self.dc = zlib.decompressobj(-15) |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
470 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
471 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
|
472 self.univ_newlines = univ_newlines |
|
41133
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
41012
diff
changeset
|
473 |
|
41012
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
474 # 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
|
475 self.nlSeps = ("\n", ) |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
476 if self.univ_newlines: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
477 self.nlSeps = ("\r\n", "\r", "\n") |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
478 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
479 def __iter__(self): |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
480 return self |
|
41133
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
41012
diff
changeset
|
481 |
|
41012
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
482 def next(self): |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
483 nextline = self.readline() |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
484 if not nextline: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
485 raise StopIteration() |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
486 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
487 return nextline |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
488 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
489 def close(self): |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
490 self.closed = True |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
491 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
492 def _checkfornewline(self): |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
493 nl, nllen = -1, -1 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
494 if self.linebuffer: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
495 # 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
|
496 # 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
|
497 # 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
|
498 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
|
499 self.linebuffer = self.linebuffer[1:] |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
500 |
|
41133
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
41012
diff
changeset
|
501 for sep in self.nlSeps: |
|
41012
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
502 nl = self.linebuffer.find(sep) |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
503 if nl >= 0: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
504 nllen = len(sep) |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
505 return nl, nllen |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
506 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
507 return nl, nllen |
|
41133
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
41012
diff
changeset
|
508 |
|
41012
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
509 def readline(self, size = -1): |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
510 """Read a line with approx. size. If size is negative, |
|
41133
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
41012
diff
changeset
|
511 read a whole line. |
|
41012
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
512 """ |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
513 if size < 0: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
514 size = sys.maxint |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
515 elif size == 0: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
516 return '' |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
517 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
518 # 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
|
519 nl, nllen = self._checkfornewline() |
|
41133
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
41012
diff
changeset
|
520 |
|
41012
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
521 if nl >= 0: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
522 # 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
|
523 nl = min(nl, size) |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
524 else: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
525 # 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
|
526 size -= len(self.linebuffer) |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
527 while nl < 0 and size > 0: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
528 buf = self.read(min(size, 100)) |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
529 if not buf: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
530 break |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
531 self.linebuffer += buf |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
532 size -= len(buf) |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
533 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
534 # check for a newline in buffer |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
535 nl, nllen = self._checkfornewline() |
|
41133
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
41012
diff
changeset
|
536 |
|
41012
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
537 # 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
|
538 # 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
|
539 # so return current buffer |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
540 if nl < 0: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
541 s = self.linebuffer |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
542 self.linebuffer = '' |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
543 return s |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
544 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
545 buf = self.linebuffer[:nl] |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
546 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
|
547 self.linebuffer = self.linebuffer[nl + nllen:] |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
548 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
549 # 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
|
550 # 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
|
551 return buf + "\n" |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
552 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
553 def readlines(self, sizehint = -1): |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
554 """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
|
555 is ignored in this implementation. |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
556 """ |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
557 result = [] |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
558 while True: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
559 line = self.readline() |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
560 if not line: break |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
561 result.append(line) |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
562 return result |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
563 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
564 def read(self, size = None): |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
565 # 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
|
566 if size == 0: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
567 return '' |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
568 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
569 # determine read size |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
570 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
|
571 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
572 # 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
|
573 # are for the encryption/password information |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
574 if self.decrypter is not None: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
575 bytesToRead -= 12 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
576 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
577 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
|
578 if self.compress_type == ZIP_STORED: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
579 lr = len(self.readbuffer) |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
580 bytesToRead = min(bytesToRead, size - lr) |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
581 elif self.compress_type == ZIP_DEFLATED: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
582 if len(self.readbuffer) > size: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
583 # 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
|
584 # 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
|
585 bytesToRead = 0 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
586 else: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
587 # 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
|
588 lr = len(self.rawbuffer) |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
589 bytesToRead = min(bytesToRead, self.compreadsize - lr) |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
590 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
591 # 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
|
592 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
|
593 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
|
594 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
595 # 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
|
596 if bytesToRead > 0: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
597 bytes = self.fileobj.read(bytesToRead) |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
598 self.bytes_read += len(bytes) |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
599 self.rawbuffer += bytes |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
600 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
601 # handle contents of raw buffer |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
602 if self.rawbuffer: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
603 newdata = self.rawbuffer |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
604 self.rawbuffer = '' |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
605 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
606 # 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
|
607 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
|
608 newdata = ''.join(map(self.decrypter, newdata)) |
|
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 # decompress newly read data if necessary |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
611 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
|
612 newdata = self.dc.decompress(newdata) |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
613 self.rawbuffer = self.dc.unconsumed_tail |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
614 if self.eof and len(self.rawbuffer) == 0: |
|
41133
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
41012
diff
changeset
|
615 # we're out of raw bytes (both from the file and |
|
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
41012
diff
changeset
|
616 # 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
|
617 # decompressor is done |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
618 newdata += self.dc.flush() |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
619 # prevent decompressor from being used again |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
620 self.dc = None |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
621 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
622 self.readbuffer += newdata |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
623 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
624 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
625 # return what the user asked for |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
626 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
|
627 bytes = self.readbuffer |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
628 self.readbuffer = '' |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
629 else: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
630 bytes = self.readbuffer[:size] |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
631 self.readbuffer = self.readbuffer[size:] |
|
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 return bytes |
|
41133
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
41012
diff
changeset
|
634 |
|
41012
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
635 |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
636 class ZipFile: |
|
17245
0377f927cafa
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
17224
diff
changeset
|
637 """ Class with methods to open, read, write, close, list zip files. |
|
0377f927cafa
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
17224
diff
changeset
|
638 |
|
47882
acfad8640e21
Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents:
47195
diff
changeset
|
639 z = ZipFile(file, mode="r", compression=ZIP_STORED, allowZip64=False) |
|
17245
0377f927cafa
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
17224
diff
changeset
|
640 |
|
17224
f9395f7627c4
Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents:
16889
diff
changeset
|
641 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
|
642 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
|
643 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
|
644 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
|
645 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
|
646 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
|
647 be necessary. |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
648 |
|
17224
f9395f7627c4
Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents:
16889
diff
changeset
|
649 """ |
|
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
|
650 |
|
16826
a95693491eca
Fix SF tracker bug #403871: AttributeError in ZipFile.__del__() when
Fred Drake <fdrake@acm.org>
parents:
15874
diff
changeset
|
651 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
|
652 |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
653 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
|
654 """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
|
655 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
|
656 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
|
657 |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
658 if compression == ZIP_STORED: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
659 pass |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
660 elif compression == ZIP_DEFLATED: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
661 if not zlib: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
662 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
|
663 "Compression requires the (missing) zlib module" |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
664 else: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
665 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
|
666 |
|
343b60b604bd
Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents:
41133
diff
changeset
|
667 self._allowZip64 = allowZip64 |
|
343b60b604bd
Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents:
41133
diff
changeset
|
668 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
|
669 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
|
670 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
|
671 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
|
672 self.compression = compression # Method of compression |
|
34594
32b89606f0cd
Remove dependency on order of mode flags
Raymond Hettinger <python@rcn.com>
parents:
33914
diff
changeset
|
673 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
|
674 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
|
675 self.comment = '' |
|
17245
0377f927cafa
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
17224
diff
changeset
|
676 |
|
17224
f9395f7627c4
Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents:
16889
diff
changeset
|
677 # 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
|
678 if isinstance(file, basestring): |
|
17224
f9395f7627c4
Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents:
16889
diff
changeset
|
679 self._filePassed = 0 |
|
f9395f7627c4
Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents:
16889
diff
changeset
|
680 self.filename = file |
|
f9395f7627c4
Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents:
16889
diff
changeset
|
681 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
|
682 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
|
683 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
|
684 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
|
685 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
|
686 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
|
687 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
|
688 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
|
689 raise |
|
17224
f9395f7627c4
Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents:
16889
diff
changeset
|
690 else: |
|
f9395f7627c4
Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents:
16889
diff
changeset
|
691 self._filePassed = 1 |
|
f9395f7627c4
Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents:
16889
diff
changeset
|
692 self.fp = file |
|
f9395f7627c4
Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents:
16889
diff
changeset
|
693 self.filename = getattr(file, 'name', None) |
|
17245
0377f927cafa
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
17224
diff
changeset
|
694 |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
695 if key == 'r': |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
696 self._GetContents() |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
697 elif key == 'w': |
|
17224
f9395f7627c4
Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents:
16889
diff
changeset
|
698 pass |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
699 elif key == 'a': |
|
25756
63225811498d
Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents:
24756
diff
changeset
|
700 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
|
701 self._RealGetContents() |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
702 # 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
|
703 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
|
704 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
|
705 self.fp.seek(0, 2) |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
706 else: |
|
17312
7635f8838c68
Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents:
17245
diff
changeset
|
707 if not self._filePassed: |
|
7635f8838c68
Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents:
17245
diff
changeset
|
708 self.fp.close() |
|
7635f8838c68
Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents:
17245
diff
changeset
|
709 self.fp = None |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
710 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
|
711 |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
712 def _GetContents(self): |
|
17312
7635f8838c68
Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents:
17245
diff
changeset
|
713 """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
|
714 is bad.""" |
|
7635f8838c68
Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents:
17245
diff
changeset
|
715 try: |
|
7635f8838c68
Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents:
17245
diff
changeset
|
716 self._RealGetContents() |
|
7635f8838c68
Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents:
17245
diff
changeset
|
717 except BadZipfile: |
|
7635f8838c68
Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents:
17245
diff
changeset
|
718 if not self._filePassed: |
|
7635f8838c68
Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents:
17245
diff
changeset
|
719 self.fp.close() |
|
7635f8838c68
Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents:
17245
diff
changeset
|
720 self.fp = None |
|
7635f8838c68
Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents:
17245
diff
changeset
|
721 raise |
|
7635f8838c68
Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents:
17245
diff
changeset
|
722 |
|
7635f8838c68
Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents:
17245
diff
changeset
|
723 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
|
724 """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
|
725 fp = self.fp |
|
25756
63225811498d
Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents:
24756
diff
changeset
|
726 endrec = _EndRecData(fp) |
|
63225811498d
Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents:
24756
diff
changeset
|
727 if not endrec: |
|
63225811498d
Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents:
24756
diff
changeset
|
728 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
|
729 if self.debug > 1: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
730 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
|
731 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
|
732 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
|
733 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
|
734 |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
735 # "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
|
736 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
|
737 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
|
738 # 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
|
739 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
|
740 |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
741 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
|
742 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
|
743 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
|
744 # 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
|
745 self.start_dir = offset_cd + concat |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
746 fp.seek(self.start_dir, 0) |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
747 data = fp.read(size_cd) |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
748 fp = cStringIO.StringIO(data) |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
749 total = 0 |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
750 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
|
751 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
|
752 if centdir[0:4] != stringCentralDir: |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
753 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
|
754 centdir = struct.unpack(structCentralDir, centdir) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
755 if self.debug > 2: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
756 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
|
757 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
|
758 # Create ZipInfo instance to store file information |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
759 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
|
760 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
|
761 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
|
762 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
|
763 (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
|
764 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
|
765 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
|
766 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
|
767 # 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
|
768 x._raw_time = t |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
769 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
|
770 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
|
771 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
772 x._decodeExtra() |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
773 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
|
774 x.filename = x._decodeFilename() |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
775 self.filelist.append(x) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
776 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
|
777 |
|
acfad8640e21
Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents:
47195
diff
changeset
|
778 # 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
|
779 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
|
780 + 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
|
781 + 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
|
782 |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
783 if self.debug > 2: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
784 print "total", total |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
785 |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
786 |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
787 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
|
788 """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
|
789 l = [] |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
790 for data in self.filelist: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
791 l.append(data.filename) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
792 return l |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
793 |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
794 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
|
795 """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
|
796 archive.""" |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
797 return self.filelist |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
798 |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
799 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
|
800 """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
|
801 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
|
802 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
|
803 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
|
804 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
|
805 |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
806 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
|
807 """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
|
808 chunk_size = 2 ** 20 |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
809 for zinfo in self.filelist: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
810 try: |
|
48265
f482875e2a03
fix ZipFile.testzip() to work with very large embedded files
Antoine Pitrou <solipsis@pitrou.net>
parents:
48073
diff
changeset
|
811 # 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
|
812 # 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
|
813 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
|
814 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
|
815 pass |
|
29625
7d41785fb60c
SF patch #756996: Bare except in ZipFile.testzip()
Raymond Hettinger <python@rcn.com>
parents:
29524
diff
changeset
|
816 except BadZipfile: |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
817 return zinfo.filename |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
818 |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
819 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
|
820 """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
|
821 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
|
822 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
|
823 raise KeyError( |
|
343b60b604bd
Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents:
41133
diff
changeset
|
824 '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
|
825 |
|
343b60b604bd
Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents:
41133
diff
changeset
|
826 return info |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
827 |
|
40833
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
828 def setpassword(self, pwd): |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
829 """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
|
830 self.pwd = pwd |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
831 |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
832 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
|
833 """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
|
834 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
|
835 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
836 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
|
837 """Return file-like object for 'name'.""" |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
838 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
|
839 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
|
840 if not self.fp: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
841 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
|
842 "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
|
843 |
|
41133
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
41012
diff
changeset
|
844 # 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
|
845 # 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
|
846 if self._filePassed: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
847 zef_file = self.fp |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
848 else: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
849 zef_file = open(self.filename, 'rb') |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
850 |
|
47195
52a66373437d
Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents:
46671
diff
changeset
|
851 # 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
|
852 if isinstance(name, ZipInfo): |
|
52a66373437d
Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents:
46671
diff
changeset
|
853 # '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
|
854 zinfo = name |
|
52a66373437d
Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents:
46671
diff
changeset
|
855 else: |
|
52a66373437d
Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents:
46671
diff
changeset
|
856 # Get info object for name |
|
52a66373437d
Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents:
46671
diff
changeset
|
857 zinfo = self.getinfo(name) |
|
41012
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
858 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
859 zef_file.seek(zinfo.header_offset, 0) |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
860 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
861 # 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
|
862 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
|
863 if fheader[0:4] != stringFileHeader: |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
864 raise BadZipfile, "Bad magic number for file header" |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
865 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
866 fheader = struct.unpack(structFileHeader, fheader) |
|
41012
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
867 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
|
868 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
|
869 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
|
870 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
871 if fname != zinfo.orig_filename: |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
872 raise BadZipfile, \ |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
873 '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
|
874 zinfo.orig_filename, fname) |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
875 |
|
41012
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
876 # check for encrypted flag & handle password |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
877 is_encrypted = zinfo.flag_bits & 0x1 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
878 zd = None |
|
40833
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
879 if is_encrypted: |
|
41012
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
880 if not pwd: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
881 pwd = self.pwd |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
882 if not pwd: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
883 raise RuntimeError, "File %s is encrypted, " \ |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
884 "password required for extraction" % name |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
885 |
|
40833
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
886 zd = _ZipDecrypter(pwd) |
|
daf063527b97
Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents:
39325
diff
changeset
|
887 # 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
|
888 # 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
|
889 # 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
|
890 # 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
|
891 # 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
|
892 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
|
893 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
|
894 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
|
895 # 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
|
896 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
|
897 else: |
|
1120b3d606f2
Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents:
44802
diff
changeset
|
898 # 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
|
899 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
|
900 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
|
901 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
|
902 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
903 # build and return a ZipExtFile |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
904 if zd is None: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
905 zef = ZipExtFile(zef_file, zinfo) |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
906 else: |
|
41012
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
907 zef = ZipExtFile(zef_file, zinfo, zd) |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
908 |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
909 # 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
|
910 if "U" in mode: |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
911 zef.set_univ_newlines(True) |
|
f1e88c3502d9
Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents:
40834
diff
changeset
|
912 return zef |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
913 |
|
44576
addc18596a15
#467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents:
42225
diff
changeset
|
914 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
|
915 """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
|
916 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
|
917 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
|
918 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
|
919 """ |
|
addc18596a15
#467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents:
42225
diff
changeset
|
920 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
|
921 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
|
922 |
|
addc18596a15
#467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents:
42225
diff
changeset
|
923 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
|
924 path = os.getcwd() |
|
addc18596a15
#467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents:
42225
diff
changeset
|
925 |
|
addc18596a15
#467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents:
42225
diff
changeset
|
926 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
|
927 |
|
addc18596a15
#467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents:
42225
diff
changeset
|
928 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
|
929 """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
|
930 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
|
931 `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
|
932 by namelist(). |
|
addc18596a15
#467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents:
42225
diff
changeset
|
933 """ |
|
addc18596a15
#467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents:
42225
diff
changeset
|
934 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
|
935 members = self.namelist() |
|
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 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
|
938 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
|
939 |
|
addc18596a15
#467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents:
42225
diff
changeset
|
940 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
|
941 """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
|
942 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
|
943 """ |
|
addc18596a15
#467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents:
42225
diff
changeset
|
944 # 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
|
945 # forward slashes to platform specific separators. |
|
53754
d3747b0a2f36
Merged revisions 72295 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51000
diff
changeset
|
946 # Strip trailing path separator, unless it represents the root. |
|
d3747b0a2f36
Merged revisions 72295 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51000
diff
changeset
|
947 if (targetpath[-1:] in (os.path.sep, os.path.altsep) |
|
d3747b0a2f36
Merged revisions 72295 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51000
diff
changeset
|
948 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
|
949 targetpath = targetpath[:-1] |
|
addc18596a15
#467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents:
42225
diff
changeset
|
950 |
|
addc18596a15
#467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents:
42225
diff
changeset
|
951 # don't include leading "/" from file name if present |
|
51000
6f761e5714e0
Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
50877
diff
changeset
|
952 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
|
953 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
|
954 else: |
|
addc18596a15
#467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents:
42225
diff
changeset
|
955 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
|
956 |
|
addc18596a15
#467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents:
42225
diff
changeset
|
957 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
|
958 |
|
addc18596a15
#467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents:
42225
diff
changeset
|
959 # 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
|
960 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
|
961 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
|
962 os.makedirs(upperdirs) |
|
addc18596a15
#467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents:
42225
diff
changeset
|
963 |
|
51000
6f761e5714e0
Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
50877
diff
changeset
|
964 if member.filename[-1] == '/': |
|
54312
e67b1d071ed6
Merged revisions 72893 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
53754
diff
changeset
|
965 if not os.path.isdir(targetpath): |
|
e67b1d071ed6
Merged revisions 72893 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
53754
diff
changeset
|
966 os.mkdir(targetpath) |
|
51000
6f761e5714e0
Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
50877
diff
changeset
|
967 return targetpath |
|
6f761e5714e0
Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
50877
diff
changeset
|
968 |
|
47195
52a66373437d
Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents:
46671
diff
changeset
|
969 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
|
970 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
|
971 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
|
972 source.close() |
|
addc18596a15
#467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents:
42225
diff
changeset
|
973 target.close() |
|
addc18596a15
#467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents:
42225
diff
changeset
|
974 |
|
addc18596a15
#467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents:
42225
diff
changeset
|
975 return targetpath |
|
addc18596a15
#467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents:
42225
diff
changeset
|
976 |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
977 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
|
978 """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
|
979 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
|
980 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
|
981 print "Duplicate name:", zinfo.filename |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
982 if self.mode not in ("w", "a"): |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
983 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
|
984 if not self.fp: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
985 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
|
986 "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
|
987 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
|
988 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
|
989 "Compression requires the (missing) zlib module" |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
990 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
|
991 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
|
992 "That compression method is not supported" |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
993 if zinfo.file_size > ZIP64_LIMIT: |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
994 if not self._allowZip64: |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
995 raise LargeZipFile("Filesize would require ZIP64 extensions") |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
996 if zinfo.header_offset > ZIP64_LIMIT: |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
997 if not self._allowZip64: |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
998 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
|
999 |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1000 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
|
1001 """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
|
1002 arcname.""" |
|
42225
343b60b604bd
Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents:
41133
diff
changeset
|
1003 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
|
1004 raise RuntimeError( |
|
343b60b604bd
Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents:
41133
diff
changeset
|
1005 "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
|
1006 |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1007 st = os.stat(filename) |
|
51000
6f761e5714e0
Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
50877
diff
changeset
|
1008 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
|
1009 mtime = time.localtime(st.st_mtime) |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1010 date_time = mtime[0:6] |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1011 # Create ZipInfo instance to store file information |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1012 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
|
1013 arcname = filename |
|
46ec948cbde8
Bug #1413790: zipfile now sanitizes absolute archive names that are
Georg Brandl <georg@python.org>
parents:
36449
diff
changeset
|
1014 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
|
1015 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
|
1016 arcname = arcname[1:] |
|
51000
6f761e5714e0
Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
50877
diff
changeset
|
1017 if isdir: |
|
6f761e5714e0
Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
50877
diff
changeset
|
1018 arcname += '/' |
|
36609
46ec948cbde8
Bug #1413790: zipfile now sanitizes absolute archive names that are
Georg Brandl <georg@python.org>
parents:
36449
diff
changeset
|
1019 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
|
1020 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
|
1021 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
|
1022 zinfo.compress_type = self.compression |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1023 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
|
1024 zinfo.compress_type = compress_type |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1025 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1026 zinfo.file_size = st.st_size |
|
19737
56c549a253a6
[ #458701 ] Patch to zipfile.py for Java
Finn Bock <bckfnn@worldonline.dk>
parents:
18746
diff
changeset
|
1027 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
|
1028 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
|
1029 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1030 self._writecheck(zinfo) |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1031 self._didModify = True |
|
51000
6f761e5714e0
Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
50877
diff
changeset
|
1032 |
|
6f761e5714e0
Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
50877
diff
changeset
|
1033 if isdir: |
|
6f761e5714e0
Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
50877
diff
changeset
|
1034 zinfo.file_size = 0 |
|
6f761e5714e0
Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
50877
diff
changeset
|
1035 zinfo.compress_size = 0 |
|
6f761e5714e0
Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
50877
diff
changeset
|
1036 zinfo.CRC = 0 |
|
6f761e5714e0
Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
50877
diff
changeset
|
1037 self.filelist.append(zinfo) |
|
6f761e5714e0
Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
50877
diff
changeset
|
1038 self.NameToInfo[zinfo.filename] = zinfo |
|
6f761e5714e0
Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
50877
diff
changeset
|
1039 self.fp.write(zinfo.FileHeader()) |
|
6f761e5714e0
Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
50877
diff
changeset
|
1040 return |
|
6f761e5714e0
Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
50877
diff
changeset
|
1041 |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1042 fp = open(filename, "rb") |
|
19737
56c549a253a6
[ #458701 ] Patch to zipfile.py for Java
Finn Bock <bckfnn@worldonline.dk>
parents:
18746
diff
changeset
|
1043 # Must overwrite CRC and sizes with correct data later |
|
56c549a253a6
[ #458701 ] Patch to zipfile.py for Java
Finn Bock <bckfnn@worldonline.dk>
parents:
18746
diff
changeset
|
1044 zinfo.CRC = CRC = 0 |
|
56c549a253a6
[ #458701 ] Patch to zipfile.py for Java
Finn Bock <bckfnn@worldonline.dk>
parents:
18746
diff
changeset
|
1045 zinfo.compress_size = compress_size = 0 |
|
56c549a253a6
[ #458701 ] Patch to zipfile.py for Java
Finn Bock <bckfnn@worldonline.dk>
parents:
18746
diff
changeset
|
1046 zinfo.file_size = file_size = 0 |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1047 self.fp.write(zinfo.FileHeader()) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1048 if zinfo.compress_type == ZIP_DEFLATED: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1049 cmpr = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION, |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1050 zlib.DEFLATED, -15) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1051 else: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1052 cmpr = None |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1053 while 1: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1054 buf = fp.read(1024 * 8) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1055 if not buf: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1056 break |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1057 file_size = file_size + len(buf) |
|
45902
d4e1db8797f6
Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents:
45896
diff
changeset
|
1058 CRC = crc32(buf, CRC) & 0xffffffff |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1059 if cmpr: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1060 buf = cmpr.compress(buf) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1061 compress_size = compress_size + len(buf) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1062 self.fp.write(buf) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1063 fp.close() |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1064 if cmpr: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1065 buf = cmpr.flush() |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1066 compress_size = compress_size + len(buf) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1067 self.fp.write(buf) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1068 zinfo.compress_size = compress_size |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1069 else: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1070 zinfo.compress_size = file_size |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1071 zinfo.CRC = CRC |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1072 zinfo.file_size = file_size |
|
19737
56c549a253a6
[ #458701 ] Patch to zipfile.py for Java
Finn Bock <bckfnn@worldonline.dk>
parents:
18746
diff
changeset
|
1073 # Seek backwards and write CRC and file sizes |
|
19953
0f9df563b65d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19737
diff
changeset
|
1074 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
|
1075 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
|
1076 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
|
1077 zinfo.file_size)) |
|
19737
56c549a253a6
[ #458701 ] Patch to zipfile.py for Java
Finn Bock <bckfnn@worldonline.dk>
parents:
18746
diff
changeset
|
1078 self.fp.seek(position, 0) |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1079 self.filelist.append(zinfo) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1080 self.NameToInfo[zinfo.filename] = zinfo |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1081 |
|
26611
8771993c9aac
Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents:
25756
diff
changeset
|
1082 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
|
1083 """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
|
1084 '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
|
1085 the name of the file in the archive.""" |
|
8771993c9aac
Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents:
25756
diff
changeset
|
1086 if not isinstance(zinfo_or_arcname, ZipInfo): |
|
8771993c9aac
Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents:
25756
diff
changeset
|
1087 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
|
1088 date_time=time.localtime(time.time())[:6]) |
|
26611
8771993c9aac
Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents:
25756
diff
changeset
|
1089 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
|
1090 zinfo.external_attr = 0600 << 16 |
|
26611
8771993c9aac
Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents:
25756
diff
changeset
|
1091 else: |
|
8771993c9aac
Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents:
25756
diff
changeset
|
1092 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
|
1093 |
|
343b60b604bd
Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents:
41133
diff
changeset
|
1094 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
|
1095 raise RuntimeError( |
|
343b60b604bd
Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents:
41133
diff
changeset
|
1096 "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
|
1097 |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1098 zinfo.file_size = len(bytes) # Uncompressed size |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1099 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
|
1100 self._writecheck(zinfo) |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1101 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
|
1102 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
|
1103 if zinfo.compress_type == ZIP_DEFLATED: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1104 co = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION, |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1105 zlib.DEFLATED, -15) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1106 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
|
1107 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
|
1108 else: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1109 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
|
1110 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
|
1111 self.fp.write(zinfo.FileHeader()) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1112 self.fp.write(bytes) |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1113 self.fp.flush() |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1114 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
|
1115 # Write CRC and file sizes after the file data |
|
54862
d04877391e80
Merged revisions 73565 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
54312
diff
changeset
|
1116 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
|
1117 zinfo.file_size)) |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1118 self.filelist.append(zinfo) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1119 self.NameToInfo[zinfo.filename] = zinfo |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1120 |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1121 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
|
1122 """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
|
1123 self.close() |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1124 |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1125 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
|
1126 """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
|
1127 records.""" |
|
21098
82d8413636e7
SF bug 486480: zipfile __del__ is broken
Tim Peters <tim.peters@gmail.com>
parents:
20020
diff
changeset
|
1128 if self.fp is None: |
|
82d8413636e7
SF bug 486480: zipfile __del__ is broken
Tim Peters <tim.peters@gmail.com>
parents:
20020
diff
changeset
|
1129 return |
|
38845
cacd47a61e79
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
38838
diff
changeset
|
1130 |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1131 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
|
1132 count = 0 |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1133 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
|
1134 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
|
1135 count = count + 1 |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1136 dt = zinfo.date_time |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1137 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
|
1138 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
|
1139 extra = [] |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1140 if zinfo.file_size > ZIP64_LIMIT \ |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1141 or zinfo.compress_size > ZIP64_LIMIT: |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1142 extra.append(zinfo.file_size) |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1143 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
|
1144 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
|
1145 compress_size = 0xffffffff |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1146 else: |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1147 file_size = zinfo.file_size |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1148 compress_size = zinfo.compress_size |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1149 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1150 if zinfo.header_offset > ZIP64_LIMIT: |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1151 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
|
1152 header_offset = 0xffffffffL |
|
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 header_offset = zinfo.header_offset |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1155 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1156 extra_data = zinfo.extra |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1157 if extra: |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1158 # Append a ZIP64 field to the extra's |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1159 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
|
1160 '<HH' + 'Q'*len(extra), |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1161 1, 8*len(extra), *extra) + extra_data |
|
38845
cacd47a61e79
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
38838
diff
changeset
|
1162 |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1163 extract_version = max(45, zinfo.extract_version) |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1164 create_version = max(45, zinfo.create_version) |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1165 else: |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1166 extract_version = zinfo.extract_version |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1167 create_version = zinfo.create_version |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1168 |
|
45902
d4e1db8797f6
Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents:
45896
diff
changeset
|
1169 try: |
|
46671
8e33f316ce14
Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents:
45902
diff
changeset
|
1170 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
|
1171 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
|
1172 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
|
1173 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
|
1174 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
|
1175 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
|
1176 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
|
1177 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
|
1178 header_offset) |
|
d4e1db8797f6
Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents:
45896
diff
changeset
|
1179 except DeprecationWarning: |
|
d4e1db8797f6
Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents:
45896
diff
changeset
|
1180 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
|
1181 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
|
1182 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
|
1183 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
|
1184 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
|
1185 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
|
1186 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
|
1187 header_offset) |
|
d4e1db8797f6
Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents:
45896
diff
changeset
|
1188 raise |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1189 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
|
1190 self.fp.write(filename) |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1191 self.fp.write(extra_data) |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1192 self.fp.write(zinfo.comment) |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1193 |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1194 pos2 = self.fp.tell() |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1195 # Write end-of-zip-archive record |
|
50826
486e2579c978
Merged revisions 68661 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
49036
diff
changeset
|
1196 centDirCount = count |
|
486e2579c978
Merged revisions 68661 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
49036
diff
changeset
|
1197 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
|
1198 centDirOffset = pos1 |
|
50826
486e2579c978
Merged revisions 68661 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
49036
diff
changeset
|
1199 if (centDirCount >= ZIP_FILECOUNT_LIMIT or |
|
486e2579c978
Merged revisions 68661 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
49036
diff
changeset
|
1200 centDirOffset > ZIP64_LIMIT or |
|
486e2579c978
Merged revisions 68661 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
49036
diff
changeset
|
1201 centDirSize > ZIP64_LIMIT): |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1202 # 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
|
1203 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
|
1204 structEndArchive64, stringEndArchive64, |
|
50826
486e2579c978
Merged revisions 68661 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
49036
diff
changeset
|
1205 44, 45, 45, 0, 0, centDirCount, centDirCount, |
|
486e2579c978
Merged revisions 68661 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
49036
diff
changeset
|
1206 centDirSize, centDirOffset) |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1207 self.fp.write(zip64endrec) |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1208 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1209 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
|
1210 structEndArchive64Locator, |
|
13e525d974b1
#3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47882
diff
changeset
|
1211 stringEndArchive64Locator, 0, pos2, 1) |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1212 self.fp.write(zip64locrec) |
|
50826
486e2579c978
Merged revisions 68661 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
49036
diff
changeset
|
1213 centDirCount = min(centDirCount, 0xFFFF) |
|
486e2579c978
Merged revisions 68661 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
49036
diff
changeset
|
1214 centDirSize = min(centDirSize, 0xFFFFFFFF) |
|
486e2579c978
Merged revisions 68661 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
49036
diff
changeset
|
1215 centDirOffset = min(centDirOffset, 0xFFFFFFFF) |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1216 |
|
47882
acfad8640e21
Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents:
47195
diff
changeset
|
1217 # 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
|
1218 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
|
1219 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
|
1220 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
|
1221 % 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
|
1222 self.comment = self.comment[:ZIP_MAX_COMMENT] |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1223 |
|
47932
13e525d974b1
#3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47882
diff
changeset
|
1224 endrec = struct.pack(structEndArchive, stringEndArchive, |
|
50826
486e2579c978
Merged revisions 68661 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
49036
diff
changeset
|
1225 0, 0, centDirCount, centDirCount, |
|
486e2579c978
Merged revisions 68661 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
49036
diff
changeset
|
1226 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
|
1227 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
|
1228 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
|
1229 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
|
1230 |
|
17224
f9395f7627c4
Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents:
16889
diff
changeset
|
1231 if not self._filePassed: |
|
f9395f7627c4
Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents:
16889
diff
changeset
|
1232 self.fp.close() |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1233 self.fp = None |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1234 |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1235 |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1236 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
|
1237 """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
|
1238 |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1239 def writepy(self, pathname, basename = ""): |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1240 """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
|
1241 |
|
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
|
1242 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
|
1243 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
|
1244 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
|
1245 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
|
1246 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
|
1247 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
|
1248 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
|
1249 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
|
1250 """ |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1251 dir, name = os.path.split(pathname) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1252 if os.path.isdir(pathname): |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1253 initname = os.path.join(pathname, "__init__.py") |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1254 if os.path.isfile(initname): |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1255 # This is a package directory, add it |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1256 if basename: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1257 basename = "%s/%s" % (basename, name) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1258 else: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1259 basename = name |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1260 if self.debug: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1261 print "Adding package in", pathname, "as", basename |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1262 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
|
1263 if self.debug: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1264 print "Adding", arcname |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1265 self.write(fname, arcname) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1266 dirlist = os.listdir(pathname) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1267 dirlist.remove("__init__.py") |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1268 # Add all *.py files and package subdirectories |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1269 for filename in dirlist: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1270 path = os.path.join(pathname, filename) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1271 root, ext = os.path.splitext(filename) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1272 if os.path.isdir(path): |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1273 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
|
1274 # This is a package directory, add it |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1275 self.writepy(path, basename) # Recursive call |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1276 elif ext == ".py": |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1277 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
|
1278 basename) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1279 if self.debug: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1280 print "Adding", arcname |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1281 self.write(fname, arcname) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1282 else: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1283 # 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
|
1284 if self.debug: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1285 print "Adding files from directory", pathname |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1286 for filename in os.listdir(pathname): |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1287 path = os.path.join(pathname, filename) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1288 root, ext = os.path.splitext(filename) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1289 if ext == ".py": |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1290 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
|
1291 basename) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1292 if self.debug: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1293 print "Adding", arcname |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1294 self.write(fname, arcname) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1295 else: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1296 if pathname[-3:] != ".py": |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1297 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
|
1298 '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
|
1299 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
|
1300 if self.debug: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1301 print "Adding file", arcname |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1302 self.write(fname, arcname) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1303 |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1304 def _get_codename(self, pathname, basename): |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1305 """Return (filename, archivename) for the path. |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1306 |
|
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
|
1307 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
|
1308 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
|
1309 /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
|
1310 """ |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1311 file_py = pathname + ".py" |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1312 file_pyc = pathname + ".pyc" |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1313 file_pyo = pathname + ".pyo" |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1314 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
|
1315 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
|
1316 fname = file_pyo # Use .pyo file |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1317 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
|
1318 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
|
1319 import py_compile |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1320 if self.debug: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1321 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
|
1322 try: |
|
16a876746285
Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents:
26611
diff
changeset
|
1323 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
|
1324 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
|
1325 print err.msg |
|
12197
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1326 fname = file_pyc |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1327 else: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1328 fname = file_pyc |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1329 archivename = os.path.split(fname)[1] |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1330 if basename: |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1331 archivename = "%s/%s" % (basename, archivename) |
|
7f18f979a93a
Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1332 return (fname, archivename) |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1333 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1334 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1335 def main(args = None): |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1336 import textwrap |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1337 USAGE=textwrap.dedent("""\ |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1338 Usage: |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1339 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
|
1340 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
|
1341 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
|
1342 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
|
1343 """) |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1344 if args is None: |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1345 args = sys.argv[1:] |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1346 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1347 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
|
1348 print USAGE |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1349 sys.exit(1) |
|
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[0] == '-l': |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1352 if len(args) != 2: |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1353 print USAGE |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1354 sys.exit(1) |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1355 zf = ZipFile(args[1], 'r') |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1356 zf.printdir() |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1357 zf.close() |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1358 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1359 elif args[0] == '-t': |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1360 if len(args) != 2: |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1361 print USAGE |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1362 sys.exit(1) |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1363 zf = ZipFile(args[1], 'r') |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1364 zf.testzip() |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1365 print "Done testing" |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1366 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1367 elif args[0] == '-e': |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1368 if len(args) != 3: |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1369 print USAGE |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1370 sys.exit(1) |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1371 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1372 zf = ZipFile(args[1], 'r') |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1373 out = args[2] |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1374 for path in zf.namelist(): |
|
38845
cacd47a61e79
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
38838
diff
changeset
|
1375 if path.startswith('./'): |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1376 tgt = os.path.join(out, path[2:]) |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1377 else: |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1378 tgt = os.path.join(out, path) |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1379 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1380 tgtdir = os.path.dirname(tgt) |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1381 if not os.path.exists(tgtdir): |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1382 os.makedirs(tgtdir) |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1383 fp = open(tgt, 'wb') |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1384 fp.write(zf.read(path)) |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1385 fp.close() |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1386 zf.close() |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1387 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1388 elif args[0] == '-c': |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1389 if len(args) < 3: |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1390 print USAGE |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1391 sys.exit(1) |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1392 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1393 def addToZip(zf, path, zippath): |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1394 if os.path.isfile(path): |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1395 zf.write(path, zippath, ZIP_DEFLATED) |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1396 elif os.path.isdir(path): |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1397 for nm in os.listdir(path): |
|
38845
cacd47a61e79
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
38838
diff
changeset
|
1398 addToZip(zf, |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1399 os.path.join(path, nm), os.path.join(zippath, nm)) |
|
38845
cacd47a61e79
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
38838
diff
changeset
|
1400 # else: ignore |
|
38838
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1401 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1402 zf = ZipFile(args[1], 'w', allowZip64=True) |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1403 for src in args[2:]: |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1404 addToZip(zf, src, os.path.basename(src)) |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1405 |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1406 zf.close() |
|
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 if __name__ == "__main__": |
|
fd412a00a07d
Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
36609
diff
changeset
|
1409 main() |
