annotate Lib/zipfile.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 8feaa8d04c56
children ce869b05762c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1 """
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
2 Read and write ZIP files.
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
3 """
44576
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
4 import struct, os, time, sys, shutil
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
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
6 import io
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
7 import re
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 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
10 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
11 crc32 = zlib.crc32
17377
5f68754eb3b6 Try an except: after an import into "except ImportError".
Guido van Rossum <guido@python.org>
parents: 17312
diff changeset
12 except ImportError:
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 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
14 crc32 = binascii.crc32
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
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 __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
17 "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
18
14933
715a3e924737 Always use the same name for the exception defined in this module!
Fred Drake <fdrake@acm.org>
parents: 12919
diff changeset
19 class BadZipfile(Exception):
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 pass
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
21
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
22
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
23 class LargeZipFile(Exception):
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
24 """
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
25 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
26 and those extensions are disabled.
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
27 """
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
28
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
29 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
30
50826
486e2579c978 Merged revisions 68661 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49036
diff changeset
31 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
32 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
33 ZIP_MAX_COMMENT = (1 << 16) - 1
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
34
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 # constants for Zip file compression methods
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 ZIP_STORED = 0
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 ZIP_DEFLATED = 8
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 # Other ZIP compression methods not supported
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
40 # 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
41 # 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
42 # 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
43 # 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
44 # (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
45
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
46 # 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
47 # (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
48 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
49 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
50 sizeEndCentDir = struct.calcsize(structEndArchive)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
51
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
52 _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
53 _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
54 _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
55 _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
56 _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
57 _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
58 _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
59 _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
60 # 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
61 # 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
62 _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
63 _ECD_LOCATION = 9
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
64
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
65 # 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
66 # 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
67 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
68 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
69 sizeCentralDir = struct.calcsize(structCentralDir)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70
16840
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
71 # 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
72 _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
73 _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
74 _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
75 _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
76 _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
77 _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
78 _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
79 _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
80 _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
81 _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
82 _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
83 _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
84 _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
85 _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
86 _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
87 _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
88 _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
89 _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
90 _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
91
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
92 # 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
93 # (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
94 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
95 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
96 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
97
16840
950fc2261da4 Define lots of constants for indexes into the structures for the file
Fred Drake <fdrake@acm.org>
parents: 16826
diff changeset
98 _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
99 _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
100 _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
101 _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
102 _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
103 _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
104 _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
105 _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
106 _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
107 _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
108 _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
109 _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
110
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
111 # 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
112 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
113 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
114 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
115
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
116 # 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
117 # (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
118 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
119 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
120 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
121
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
122 _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
123 _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
124 _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
125 _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
126 _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
127 _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
128 _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
129 _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
130 _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
131 _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
132
50300
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
133 def _check_zipfile(fp):
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 try:
50300
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
135 if _EndRecData(fp):
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
136 return True # file has correct magic number
17805
bf70d1f4f61f Fix one bare except: clause.
Fred Drake <fdrake@acm.org>
parents: 17517
diff changeset
137 except IOError:
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 pass
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 21251
diff changeset
139 return False
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
140
50300
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
141 def is_zipfile(filename):
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
142 """Quickly see if a file is a ZIP file by checking the magic number.
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
143
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
144 The filename argument may be a file or file-like object too.
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
145 """
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
146 result = False
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
147 try:
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
148 if hasattr(filename, "read"):
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
149 result = _check_zipfile(fp=filename)
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
150 else:
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
151 with open(filename, "rb") as fp:
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
152 result = _check_zipfile(fp)
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
153 except IOError:
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
154 pass
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
155 return result
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 49036
diff changeset
156
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
157 def _EndRecData64(fpin, offset, endrec):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
158 """
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
159 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
160 """
66450
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
161 try:
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
162 fpin.seek(offset - sizeEndCentDir64Locator, 2)
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
163 except IOError:
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
164 # If the seek fails, the file is not large enough to contain a ZIP64
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
165 # end-of-archive record, so just return the end record we were given.
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
166 return endrec
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
167
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
168 data = fpin.read(sizeEndCentDir64Locator)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
169 sig, diskno, reloff, disks = struct.unpack(structEndArchive64Locator, data)
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
170 if sig != stringEndArchive64Locator:
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
171 return endrec
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
172
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
173 if diskno != 0 or disks != 1:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
174 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
175
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
176 # 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
177 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
178 data = fpin.read(sizeEndCentDir64)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
179 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
180 dircount, dircount2, dirsize, diroffset = \
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
181 struct.unpack(structEndArchive64, data)
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
182 if sig != stringEndArchive64:
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
183 return endrec
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
184
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
185 # 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
186 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
187 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
188 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
189 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
190 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
191 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
192 endrec[_ECD_OFFSET] = diroffset
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
193 return endrec
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
194
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
195
25756
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
196 def _EndRecData(fpin):
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
197 """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
198
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
199 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
200 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
201
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
202 # 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
203 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
204 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
205
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
206 # 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
207 # "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
208 # file if this is the case).
58175
99e7c1b8b1e1 Merged revisions 74245 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 54862
diff changeset
209 try:
99e7c1b8b1e1 Merged revisions 74245 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 54862
diff changeset
210 fpin.seek(-sizeEndCentDir, 2)
99e7c1b8b1e1 Merged revisions 74245 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 54862
diff changeset
211 except IOError:
99e7c1b8b1e1 Merged revisions 74245 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 54862
diff changeset
212 return None
25756
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
213 data = fpin.read()
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
214 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
215 # the signature is correct and there's no comment, unpack structure
25756
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
216 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
217 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
218
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
219 # 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
220 endrec.append("")
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
221 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
222
50877
6a76abd616de Merged revisions 68678 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50826
diff changeset
223 # 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
224 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
225
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
226 # 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
227 # 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
228 # 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
229 # 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
230 # 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
231 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
232 fpin.seek(maxCommentStart, 0)
25756
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
233 data = fpin.read()
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
234 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
235 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
236 # 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
237 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
238 endrec = list(struct.unpack(structEndArchive, recData))
70748
cc3255a707c7 #10694: zipfile now ignores garbage at the end of a zipfile.
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
239 commentSize = endrec[_ECD_COMMENT_SIZE] #as claimed by the zip file
cc3255a707c7 #10694: zipfile now ignores garbage at the end of a zipfile.
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
240 comment = data[start+sizeEndCentDir:start+sizeEndCentDir+commentSize]
cc3255a707c7 #10694: zipfile now ignores garbage at the end of a zipfile.
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
241 endrec.append(comment)
cc3255a707c7 #10694: zipfile now ignores garbage at the end of a zipfile.
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
242 endrec.append(maxCommentStart + start)
50877
6a76abd616de Merged revisions 68678 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50826
diff changeset
243
70748
cc3255a707c7 #10694: zipfile now ignores garbage at the end of a zipfile.
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
244 # Try to read the "Zip64 end of central directory" structure
cc3255a707c7 #10694: zipfile now ignores garbage at the end of a zipfile.
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
245 return _EndRecData64(fpin, maxCommentStart + start - filesize,
cc3255a707c7 #10694: zipfile now ignores garbage at the end of a zipfile.
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
246 endrec)
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
247
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
248 # 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
249 return
25756
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
250
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
251
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
252 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
253 """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
254
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
255 __slots__ = (
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
256 'orig_filename',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
257 'filename',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
258 'date_time',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
259 'compress_type',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
260 'comment',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
261 'extra',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
262 'create_system',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
263 'create_version',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
264 'extract_version',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
265 'reserved',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
266 'flag_bits',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
267 'volume',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
268 'internal_attr',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
269 'external_attr',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
270 'header_offset',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
271 'CRC',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
272 'compress_size',
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
273 '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
274 '_raw_time',
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
275 )
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
276
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 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
278 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
279
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
280 # 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
281 # 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
282 null_byte = filename.find(chr(0))
87f30fcfad48 SF patch #755987 (Jim Ahlstrom):
Greg Ward <gward@python.net>
parents: 27250
diff changeset
283 if null_byte >= 0:
87f30fcfad48 SF patch #755987 (Jim Ahlstrom):
Greg Ward <gward@python.net>
parents: 27250
diff changeset
284 filename = filename[0:null_byte]
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
285 # 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
286 # 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
287 # ZIP format specification.
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
288 if os.sep != "/" and os.sep in filename:
29523
87f30fcfad48 SF patch #755987 (Jim Ahlstrom):
Greg Ward <gward@python.net>
parents: 27250
diff changeset
289 filename = filename.replace(os.sep, "/")
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
290
29523
87f30fcfad48 SF patch #755987 (Jim Ahlstrom):
Greg Ward <gward@python.net>
parents: 27250
diff changeset
291 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
292 self.date_time = date_time # year, month, day, hour, min, sec
72991
649ac338203f Fix closes Issue6090 - Raise a ValueError, instead of failing with unrelated
Senthil Kumaran <senthil@uthcode.com>
parents: 70748
diff changeset
293
649ac338203f Fix closes Issue6090 - Raise a ValueError, instead of failing with unrelated
Senthil Kumaran <senthil@uthcode.com>
parents: 70748
diff changeset
294 if date_time[0] < 1980:
649ac338203f Fix closes Issue6090 - Raise a ValueError, instead of failing with unrelated
Senthil Kumaran <senthil@uthcode.com>
parents: 70748
diff changeset
295 raise ValueError('ZIP does not support timestamps before 1980')
649ac338203f Fix closes Issue6090 - Raise a ValueError, instead of failing with unrelated
Senthil Kumaran <senthil@uthcode.com>
parents: 70748
diff changeset
296
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 # 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
298 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
299 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
300 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
301 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
302 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
303 else:
4ed30eaa0256 Patch #1412872: zipfile: use correct system type on unixy systems.
Martin v. Löwis <martin@v.loewis.de>
parents: 34594
diff changeset
304 # 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
305 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
306 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
307 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
308 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
309 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
310 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
311 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
312 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
313 # 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
314 # 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
315 # 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
316 # 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
317 # 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
318
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 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
320 """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
321 dt = self.date_time
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 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
323 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
324 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
325 # 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
326 CRC = compress_size = file_size = 0
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 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
328 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
329 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
330 file_size = self.file_size
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
331
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
332 extra = self.extra
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
333
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
334 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
335 # 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
336 # 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
337 fmt = '<HHQQ'
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
338 extra = extra + struct.pack(fmt,
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
339 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
340 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
341 compress_size = 0xffffffff
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
342 self.extract_version = max(45, self.extract_version)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
343 self.create_version = max(45, self.extract_version)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
344
46671
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
345 filename, flag_bits = self._encodeFilenameFlags()
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 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
347 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
348 self.compress_type, dostime, dosdate, CRC,
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 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
350 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
351 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
352
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
353 def _encodeFilenameFlags(self):
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
354 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
355 try:
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
356 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
357 except UnicodeEncodeError:
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
358 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
359 else:
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
360 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
361
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
362 def _decodeFilename(self):
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
363 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
364 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
365 else:
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
366 return self.filename
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
367
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
368 def _decodeExtra(self):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
369 # Try to decode the extra field.
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
370 extra = self.extra
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
371 unpack = struct.unpack
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
372 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
373 tp, ln = unpack('<HH', extra[:4])
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
374 if tp == 1:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
375 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
376 counts = unpack('<QQQ', extra[4:28])
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
377 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
378 counts = unpack('<QQ', extra[4:20])
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
379 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
380 counts = unpack('<Q', extra[4:12])
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
381 elif ln == 0:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
382 counts = ()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
383 else:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
384 raise RuntimeError, "Corrupt extra field %s"%(ln,)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
385
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
386 idx = 0
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
387
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
388 # 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
389 if self.file_size in (0xffffffffffffffffL, 0xffffffffL):
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
390 self.file_size = counts[idx]
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
391 idx += 1
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
392
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
393 if self.compress_size == 0xFFFFFFFFL:
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
394 self.compress_size = counts[idx]
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
395 idx += 1
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
396
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
397 if self.header_offset == 0xffffffffL:
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
398 old = self.header_offset
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
399 self.header_offset = counts[idx]
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
400 idx+=1
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
401
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
402 extra = extra[ln+4:]
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
403
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
404
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
405 class _ZipDecrypter:
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
406 """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
407
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
408 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
409 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
410 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
411
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
412 Usage:
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
413 zd = _ZipDecrypter(mypwd)
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
414 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
415 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
416 """
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
417
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
418 def _GenerateCRCTable():
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
419 """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
420
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
421 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
422 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
423 relying on binascii.crc32().
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
424 """
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
425 poly = 0xedb88320
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
426 table = [0] * 256
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
427 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
428 crc = i
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
429 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
430 if crc & 1:
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
431 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
432 else:
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
433 crc = ((crc >> 1) & 0x7FFFFFFF)
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
434 table[i] = crc
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
435 return table
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
436 crctable = _GenerateCRCTable()
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
437
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
438 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
439 """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
440 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
441
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
442 def __init__(self, pwd):
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
443 self.key0 = 305419896
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
444 self.key1 = 591751049
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
445 self.key2 = 878082192
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
446 for p in pwd:
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
447 self._UpdateKeys(p)
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
448
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
449 def _UpdateKeys(self, c):
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
450 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
451 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
452 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
453 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
454
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
455 def __call__(self, c):
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
456 """Decrypt a single character."""
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
457 c = ord(c)
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
458 k = self.key2 | 2
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
459 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
460 c = chr(c)
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
461 self._UpdateKeys(c)
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
462 return c
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
463
80499
8feaa8d04c56 #14313: zipfile now raises NotImplementedError when the compression type is unknown.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80485
diff changeset
464
8feaa8d04c56 #14313: zipfile now raises NotImplementedError when the compression type is unknown.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80485
diff changeset
465 compressor_names = {
8feaa8d04c56 #14313: zipfile now raises NotImplementedError when the compression type is unknown.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80485
diff changeset
466 0: 'store',
8feaa8d04c56 #14313: zipfile now raises NotImplementedError when the compression type is unknown.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80485
diff changeset
467 1: 'shrink',
8feaa8d04c56 #14313: zipfile now raises NotImplementedError when the compression type is unknown.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80485
diff changeset
468 2: 'reduce',
8feaa8d04c56 #14313: zipfile now raises NotImplementedError when the compression type is unknown.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80485
diff changeset
469 3: 'reduce',
8feaa8d04c56 #14313: zipfile now raises NotImplementedError when the compression type is unknown.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80485
diff changeset
470 4: 'reduce',
8feaa8d04c56 #14313: zipfile now raises NotImplementedError when the compression type is unknown.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80485
diff changeset
471 5: 'reduce',
8feaa8d04c56 #14313: zipfile now raises NotImplementedError when the compression type is unknown.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80485
diff changeset
472 6: 'implode',
8feaa8d04c56 #14313: zipfile now raises NotImplementedError when the compression type is unknown.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80485
diff changeset
473 7: 'tokenize',
8feaa8d04c56 #14313: zipfile now raises NotImplementedError when the compression type is unknown.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80485
diff changeset
474 8: 'deflate',
8feaa8d04c56 #14313: zipfile now raises NotImplementedError when the compression type is unknown.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80485
diff changeset
475 9: 'deflate64',
8feaa8d04c56 #14313: zipfile now raises NotImplementedError when the compression type is unknown.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80485
diff changeset
476 10: 'implode',
8feaa8d04c56 #14313: zipfile now raises NotImplementedError when the compression type is unknown.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80485
diff changeset
477 12: 'bzip2',
8feaa8d04c56 #14313: zipfile now raises NotImplementedError when the compression type is unknown.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80485
diff changeset
478 14: 'lzma',
8feaa8d04c56 #14313: zipfile now raises NotImplementedError when the compression type is unknown.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80485
diff changeset
479 18: 'terse',
8feaa8d04c56 #14313: zipfile now raises NotImplementedError when the compression type is unknown.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80485
diff changeset
480 19: 'lz77',
8feaa8d04c56 #14313: zipfile now raises NotImplementedError when the compression type is unknown.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80485
diff changeset
481 97: 'wavpack',
8feaa8d04c56 #14313: zipfile now raises NotImplementedError when the compression type is unknown.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80485
diff changeset
482 98: 'ppmd',
8feaa8d04c56 #14313: zipfile now raises NotImplementedError when the compression type is unknown.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80485
diff changeset
483 }
8feaa8d04c56 #14313: zipfile now raises NotImplementedError when the compression type is unknown.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80485
diff changeset
484
8feaa8d04c56 #14313: zipfile now raises NotImplementedError when the compression type is unknown.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80485
diff changeset
485
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
486 class ZipExtFile(io.BufferedIOBase):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
487 """File-like object for reading an archive member.
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
488 Is returned by ZipFile.open().
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
489 """
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
490
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
491 # Max size supported by decompressor.
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
492 MAX_N = 1 << 31 - 1
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
493
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
494 # Read from compressed files in 4k blocks.
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
495 MIN_READ_SIZE = 4096
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
496
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
497 # Search for universal newlines or line chunks.
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
498 PATTERN = re.compile(r'^(?P<chunk>[^\r\n]+)|(?P<newline>\n|\r\n?)')
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
499
80231
6a14f692df1c Closes #16183: ZipExtFile object close without file handle closed (backporting of Issue #9846)
Jesus Cea <jcea@jcea.es>
parents: 76269
diff changeset
500 def __init__(self, fileobj, mode, zipinfo, decrypter=None,
6a14f692df1c Closes #16183: ZipExtFile object close without file handle closed (backporting of Issue #9846)
Jesus Cea <jcea@jcea.es>
parents: 76269
diff changeset
501 close_fileobj=False):
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
502 self._fileobj = fileobj
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
503 self._decrypter = decrypter
80231
6a14f692df1c Closes #16183: ZipExtFile object close without file handle closed (backporting of Issue #9846)
Jesus Cea <jcea@jcea.es>
parents: 76269
diff changeset
504 self._close_fileobj = close_fileobj
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
505
58551
f606474a210d avoid to use zlib when the compress type is not ZIP_DEFLATED
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58540
diff changeset
506 self._compress_type = zipinfo.compress_type
f606474a210d avoid to use zlib when the compress type is not ZIP_DEFLATED
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58540
diff changeset
507 self._compress_size = zipinfo.compress_size
f606474a210d avoid to use zlib when the compress type is not ZIP_DEFLATED
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58540
diff changeset
508 self._compress_left = zipinfo.compress_size
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
509
58551
f606474a210d avoid to use zlib when the compress type is not ZIP_DEFLATED
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58540
diff changeset
510 if self._compress_type == ZIP_DEFLATED:
f606474a210d avoid to use zlib when the compress type is not ZIP_DEFLATED
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58540
diff changeset
511 self._decompressor = zlib.decompressobj(-15)
80499
8feaa8d04c56 #14313: zipfile now raises NotImplementedError when the compression type is unknown.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80485
diff changeset
512 elif self._compress_type != ZIP_STORED:
8feaa8d04c56 #14313: zipfile now raises NotImplementedError when the compression type is unknown.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80485
diff changeset
513 descr = compressor_names.get(self._compress_type)
8feaa8d04c56 #14313: zipfile now raises NotImplementedError when the compression type is unknown.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80485
diff changeset
514 if descr:
8feaa8d04c56 #14313: zipfile now raises NotImplementedError when the compression type is unknown.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80485
diff changeset
515 raise NotImplementedError("compression type %d (%s)" % (self._compress_type, descr))
8feaa8d04c56 #14313: zipfile now raises NotImplementedError when the compression type is unknown.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80485
diff changeset
516 else:
8feaa8d04c56 #14313: zipfile now raises NotImplementedError when the compression type is unknown.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80485
diff changeset
517 raise NotImplementedError("compression type %d" % (self._compress_type,))
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
518 self._unconsumed = ''
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
519
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
520 self._readbuffer = ''
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
521 self._offset = 0
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
522
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
523 self._universal = 'U' in mode
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
524 self.newlines = None
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
525
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
526 # Adjust read size for encrypted files since the first 12 bytes
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
527 # are for the encryption/password information.
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
528 if self._decrypter is not None:
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
529 self._compress_left -= 12
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
530
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
531 self.mode = mode
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
532 self.name = zipinfo.filename
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
533
63935
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
534 if hasattr(zipinfo, 'CRC'):
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
535 self._expected_crc = zipinfo.CRC
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
536 self._running_crc = crc32(b'') & 0xffffffff
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
537 else:
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
538 self._expected_crc = None
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
539
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
540 def readline(self, limit=-1):
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
541 """Read and return a line from the stream.
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
542
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
543 If limit is specified, at most limit bytes will be read.
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
544 """
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
545
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
546 if not self._universal and limit < 0:
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
547 # Shortcut common case - newline found in buffer.
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
548 i = self._readbuffer.find('\n', self._offset) + 1
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
549 if i > 0:
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
550 line = self._readbuffer[self._offset: i]
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
551 self._offset = i
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
552 return line
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
553
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
554 if not self._universal:
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
555 return io.BufferedIOBase.readline(self, limit)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
556
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
557 line = ''
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
558 while limit < 0 or len(line) < limit:
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
559 readahead = self.peek(2)
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
560 if readahead == '':
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
561 return line
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
562
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
563 #
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
564 # Search for universal newlines or line chunks.
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
565 #
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
566 # The pattern returns either a line chunk or a newline, but not
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
567 # both. Combined with peek(2), we are assured that the sequence
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
568 # '\r\n' is always retrieved completely and never split into
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
569 # separate newlines - '\r', '\n' due to coincidental readaheads.
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
570 #
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
571 match = self.PATTERN.search(readahead)
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
572 newline = match.group('newline')
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
573 if newline is not None:
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
574 if self.newlines is None:
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
575 self.newlines = []
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
576 if newline not in self.newlines:
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
577 self.newlines.append(newline)
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
578 self._offset += len(newline)
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
579 return line + '\n'
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
580
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
581 chunk = match.group('chunk')
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
582 if limit >= 0:
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
583 chunk = chunk[: limit - len(line)]
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
584
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
585 self._offset += len(chunk)
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
586 line += chunk
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
587
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
588 return line
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
589
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
590 def peek(self, n=1):
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
591 """Returns buffered bytes without advancing the position."""
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
592 if n > len(self._readbuffer) - self._offset:
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
593 chunk = self.read(n)
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
594 self._offset -= len(chunk)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
595
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
596 # Return up to 512 bytes to reduce allocation overhead for tight loops.
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
597 return self._readbuffer[self._offset: self._offset + 512]
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
598
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
599 def readable(self):
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
600 return True
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
601
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
602 def read(self, n=-1):
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
603 """Read and return up to n bytes.
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
604 If the argument is omitted, None, or negative, data is read and returned until EOF is reached..
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
605 """
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
606 buf = ''
64674
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
607 if n is None:
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
608 n = -1
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
609 while True:
64674
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
610 if n < 0:
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
611 data = self.read1(n)
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
612 elif n > len(buf):
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
613 data = self.read1(n - len(buf))
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
614 else:
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
615 return buf
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
616 if len(data) == 0:
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
617 return buf
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
618 buf += data
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
619
63935
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
620 def _update_crc(self, newdata, eof):
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
621 # Update the CRC using the given data.
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
622 if self._expected_crc is None:
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
623 # No need to compute the CRC if we don't have a reference value
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
624 return
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
625 self._running_crc = crc32(newdata, self._running_crc) & 0xffffffff
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
626 # Check the CRC if we're at the end of the file
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
627 if eof and self._running_crc != self._expected_crc:
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
628 raise BadZipfile("Bad CRC-32 for file %r" % self.name)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
629
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
630 def read1(self, n):
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
631 """Read up to n bytes with at most one read() system call."""
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
632
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
633 # Simplify algorithm (branching) by transforming negative n to large n.
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
634 if n < 0 or n is None:
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
635 n = self.MAX_N
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
636
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
637 # Bytes available in read buffer.
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
638 len_readbuffer = len(self._readbuffer) - self._offset
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
639
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
640 # Read from file.
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
641 if self._compress_left > 0 and n > len_readbuffer + len(self._unconsumed):
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
642 nbytes = n - len_readbuffer - len(self._unconsumed)
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
643 nbytes = max(nbytes, self.MIN_READ_SIZE)
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
644 nbytes = min(nbytes, self._compress_left)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
645
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
646 data = self._fileobj.read(nbytes)
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
647 self._compress_left -= len(data)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
648
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
649 if data and self._decrypter is not None:
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
650 data = ''.join(map(self._decrypter, data))
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
651
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
652 if self._compress_type == ZIP_STORED:
63935
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
653 self._update_crc(data, eof=(self._compress_left==0))
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
654 self._readbuffer = self._readbuffer[self._offset:] + data
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
655 self._offset = 0
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
656 else:
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
657 # Prepare deflated bytes for decompression.
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
658 self._unconsumed += data
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
659
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
660 # Handle unconsumed data.
58551
f606474a210d avoid to use zlib when the compress type is not ZIP_DEFLATED
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58540
diff changeset
661 if (len(self._unconsumed) > 0 and n > len_readbuffer and
f606474a210d avoid to use zlib when the compress type is not ZIP_DEFLATED
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58540
diff changeset
662 self._compress_type == ZIP_DEFLATED):
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
663 data = self._decompressor.decompress(
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
664 self._unconsumed,
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
665 max(n - len_readbuffer, self.MIN_READ_SIZE)
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
666 )
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
667
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
668 self._unconsumed = self._decompressor.unconsumed_tail
63935
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
669 eof = len(self._unconsumed) == 0 and self._compress_left == 0
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
670 if eof:
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
671 data += self._decompressor.flush()
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
672
63935
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
673 self._update_crc(data, eof=eof)
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
674 self._readbuffer = self._readbuffer[self._offset:] + data
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
675 self._offset = 0
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
676
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
677 # Read from buffer.
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
678 data = self._readbuffer[self._offset: self._offset + n]
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
679 self._offset += len(data)
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
680 return data
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
681
80231
6a14f692df1c Closes #16183: ZipExtFile object close without file handle closed (backporting of Issue #9846)
Jesus Cea <jcea@jcea.es>
parents: 76269
diff changeset
682 def close(self):
6a14f692df1c Closes #16183: ZipExtFile object close without file handle closed (backporting of Issue #9846)
Jesus Cea <jcea@jcea.es>
parents: 76269
diff changeset
683 try :
6a14f692df1c Closes #16183: ZipExtFile object close without file handle closed (backporting of Issue #9846)
Jesus Cea <jcea@jcea.es>
parents: 76269
diff changeset
684 if self._close_fileobj:
6a14f692df1c Closes #16183: ZipExtFile object close without file handle closed (backporting of Issue #9846)
Jesus Cea <jcea@jcea.es>
parents: 76269
diff changeset
685 self._fileobj.close()
6a14f692df1c Closes #16183: ZipExtFile object close without file handle closed (backporting of Issue #9846)
Jesus Cea <jcea@jcea.es>
parents: 76269
diff changeset
686 finally:
6a14f692df1c Closes #16183: ZipExtFile object close without file handle closed (backporting of Issue #9846)
Jesus Cea <jcea@jcea.es>
parents: 76269
diff changeset
687 super(ZipExtFile, self).close()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
688
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
689
76269
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
690 class ZipFile(object):
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17224
diff changeset
691 """ Class with methods to open, read, write, close, list zip files.
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17224
diff changeset
692
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
693 z = ZipFile(file, mode="r", compression=ZIP_STORED, allowZip64=False)
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17224
diff changeset
694
17224
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
695 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
696 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
697 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
698 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
699 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
700 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
701 be necessary.
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
702
17224
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
703 """
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
704
16826
a95693491eca Fix SF tracker bug #403871: AttributeError in ZipFile.__del__() when
Fred Drake <fdrake@acm.org>
parents: 15874
diff changeset
705 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
706
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
707 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
708 """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
709 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
710 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
711
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
712 if compression == ZIP_STORED:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
713 pass
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
714 elif compression == ZIP_DEFLATED:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
715 if not zlib:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
716 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
717 "Compression requires the (missing) zlib module"
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
718 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
719 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
720
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
721 self._allowZip64 = allowZip64
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
722 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
723 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
724 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
725 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
726 self.compression = compression # Method of compression
34594
32b89606f0cd Remove dependency on order of mode flags
Raymond Hettinger <python@rcn.com>
parents: 33914
diff changeset
727 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
728 self.pwd = None
76269
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
729 self._comment = ''
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17224
diff changeset
730
17224
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
731 # 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
732 if isinstance(file, basestring):
17224
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
733 self._filePassed = 0
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
734 self.filename = file
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
735 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
736 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
737 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
738 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
739 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
740 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
741 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
742 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
743 raise
17224
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
744 else:
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
745 self._filePassed = 1
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
746 self.fp = file
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
747 self.filename = getattr(file, 'name', None)
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17224
diff changeset
748
80485
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
749 try:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
750 if key == 'r':
25756
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
751 self._RealGetContents()
80485
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
752 elif key == 'w':
66450
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
753 # set the modified flag so central directory gets written
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
754 # even if no files are added to the archive
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
755 self._didModify = True
80485
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
756 elif key == 'a':
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
757 try:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
758 # See if file is a zip file
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
759 self._RealGetContents()
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
760 # seek to start of directory and overwrite
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
761 self.fp.seek(self.start_dir, 0)
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
762 except BadZipfile:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
763 # file is not a zip file, just append
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
764 self.fp.seek(0, 2)
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
765
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
766 # set the modified flag so central directory gets written
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
767 # even if no files are added to the archive
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
768 self._didModify = True
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
769 else:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
770 raise RuntimeError('Mode must be "r", "w" or "a"')
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
771 except:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
772 fp = self.fp
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
773 self.fp = None
17312
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
774 if not self._filePassed:
80485
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
775 fp.close()
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
776 raise
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
777
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55485
diff changeset
778 def __enter__(self):
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55485
diff changeset
779 return self
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55485
diff changeset
780
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55485
diff changeset
781 def __exit__(self, type, value, traceback):
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55485
diff changeset
782 self.close()
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55485
diff changeset
783
17312
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
784 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
785 """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
786 fp = self.fp
66450
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
787 try:
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
788 endrec = _EndRecData(fp)
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
789 except IOError:
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
790 raise BadZipfile("File is not a zip file")
25756
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
791 if not endrec:
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
792 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
793 if self.debug > 1:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
794 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
795 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
796 offset_cd = endrec[_ECD_OFFSET] # offset of central directory
76269
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
797 self._comment = endrec[_ECD_COMMENT] # archive comment
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
798
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
799 # "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
800 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
801 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
802 # 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
803 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
804
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
805 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
806 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
807 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
808 # 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
809 self.start_dir = offset_cd + concat
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
810 fp.seek(self.start_dir, 0)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
811 data = fp.read(size_cd)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
812 fp = cStringIO.StringIO(data)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
813 total = 0
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
814 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
815 centdir = fp.read(sizeCentralDir)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
816 if centdir[0:4] != stringCentralDir:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
817 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
818 centdir = struct.unpack(structCentralDir, centdir)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
819 if self.debug > 2:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
820 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
821 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
822 # Create ZipInfo instance to store file information
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
823 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
824 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
825 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
826 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
827 (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
828 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
829 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
830 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
831 # 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
832 x._raw_time = t
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
833 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
834 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
835
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
836 x._decodeExtra()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
837 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
838 x.filename = x._decodeFilename()
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
839 self.filelist.append(x)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
840 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
841
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
842 # 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
843 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
844 + 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
845 + 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
846
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
847 if self.debug > 2:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
848 print "total", total
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
849
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
850
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
851 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
852 """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
853 l = []
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
854 for data in self.filelist:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
855 l.append(data.filename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
856 return l
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
857
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
858 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
859 """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
860 archive."""
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
861 return self.filelist
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
862
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
863 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
864 """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
865 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
866 for zinfo in self.filelist:
44677
827f0451734f 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: 39325
diff changeset
867 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
868 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
869
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
870 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
871 """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
872 chunk_size = 2 ** 20
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
873 for zinfo in self.filelist:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
874 try:
48265
f482875e2a03 fix ZipFile.testzip() to work with very large embedded files
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
875 # 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
876 # MemoryError with very large embedded files.
80485
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
877 with self.open(zinfo.filename, "r") as f:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
878 while f.read(chunk_size): # Check CRC-32
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
879 pass
29625
7d41785fb60c SF patch #756996: Bare except in ZipFile.testzip()
Raymond Hettinger <python@rcn.com>
parents: 29524
diff changeset
880 except BadZipfile:
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
881 return zinfo.filename
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
882
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
883 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
884 """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
885 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
886 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
887 raise KeyError(
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
888 '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
889
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
890 return info
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
891
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
892 def setpassword(self, pwd):
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
893 """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
894 self.pwd = pwd
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
895
76269
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
896 @property
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
897 def comment(self):
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
898 """The comment text associated with the ZIP file."""
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
899 return self._comment
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
900
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
901 @comment.setter
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
902 def comment(self, comment):
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
903 # check for valid comment length
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
904 if len(comment) >= ZIP_MAX_COMMENT:
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
905 if self.debug:
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
906 print('Archive comment is too long; truncating to %d bytes'
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
907 % ZIP_MAX_COMMENT)
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
908 comment = comment[:ZIP_MAX_COMMENT]
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
909 self._comment = comment
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
910 self._didModify = True
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
911
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
912 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
913 """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
914 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
915
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
916 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
917 """Return file-like object for 'name'."""
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
918 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
919 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
920 if not self.fp:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
921 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
922 "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
923
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
924 # 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
925 # 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
926 if self._filePassed:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
927 zef_file = self.fp
80231
6a14f692df1c Closes #16183: ZipExtFile object close without file handle closed (backporting of Issue #9846)
Jesus Cea <jcea@jcea.es>
parents: 76269
diff changeset
928 should_close = False
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
929 else:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
930 zef_file = open(self.filename, 'rb')
80231
6a14f692df1c Closes #16183: ZipExtFile object close without file handle closed (backporting of Issue #9846)
Jesus Cea <jcea@jcea.es>
parents: 76269
diff changeset
931 should_close = True
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
932
80485
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
933 try:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
934 # Make sure we have an info object
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
935 if isinstance(name, ZipInfo):
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
936 # 'name' is already an info object
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
937 zinfo = name
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
938 else:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
939 # Get info object for name
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
940 zinfo = self.getinfo(name)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
941
80485
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
942 zef_file.seek(zinfo.header_offset, 0)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
943
80485
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
944 # Skip the file header:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
945 fheader = zef_file.read(sizeFileHeader)
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
946 if fheader[0:4] != stringFileHeader:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
947 raise BadZipfile, "Bad magic number for file header"
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
948
80485
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
949 fheader = struct.unpack(structFileHeader, fheader)
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
950 fname = zef_file.read(fheader[_FH_FILENAME_LENGTH])
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
951 if fheader[_FH_EXTRA_FIELD_LENGTH]:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
952 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
953
80485
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
954 if fname != zinfo.orig_filename:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
955 raise BadZipfile, \
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
956 'File name in directory "%s" and header "%s" differ.' % (
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
957 zinfo.orig_filename, fname)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
958
80485
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
959 # check for encrypted flag & handle password
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
960 is_encrypted = zinfo.flag_bits & 0x1
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
961 zd = None
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
962 if is_encrypted:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
963 if not pwd:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
964 pwd = self.pwd
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
965 if not pwd:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
966 raise RuntimeError, "File %s is encrypted, " \
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
967 "password required for extraction" % name
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
968
80485
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
969 zd = _ZipDecrypter(pwd)
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
970 # The first 12 bytes in the cypher stream is an encryption header
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
971 # used to strengthen the algorithm. The first 11 bytes are
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
972 # completely random, while the 12th contains the MSB of the CRC,
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
973 # or the MSB of the file time depending on the header type
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
974 # and is used to check the correctness of the password.
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
975 bytes = zef_file.read(12)
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
976 h = map(zd, bytes[0:12])
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
977 if zinfo.flag_bits & 0x8:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
978 # compare against the file type from extended local headers
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
979 check_byte = (zinfo._raw_time >> 8) & 0xff
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
980 else:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
981 # compare against the CRC otherwise
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
982 check_byte = (zinfo.CRC >> 24) & 0xff
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
983 if ord(h[11]) != check_byte:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
984 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
985
80485
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
986 return ZipExtFile(zef_file, mode, zinfo, zd,
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
987 close_fileobj=should_close)
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
988 except:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
989 if should_close:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
990 zef_file.close()
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
991 raise
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
992
44576
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
993 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
994 """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
995 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
996 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
997 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
998 """
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
999 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
1000 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
1001
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
1002 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
1003 path = os.getcwd()
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
1004
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
1005 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
1006
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
1007 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
1008 """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
1009 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
1010 `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
1011 by namelist().
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
1012 """
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
1013 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
1014 members = self.namelist()
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
1015
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
1016 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
1017 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
1018
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
1019 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
1020 """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
1021 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
1022 """
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
1023 # 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
1024 # forward slashes to platform specific separators.
53754
d3747b0a2f36 Merged revisions 72295 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51000
diff changeset
1025 # 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
1026 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
1027 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
1028 targetpath = targetpath[:-1]
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
1029
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
1030 # 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
1031 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
1032 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
1033 else:
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
1034 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
1035
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
1036 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
1037
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
1038 # 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
1039 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
1040 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
1041 os.makedirs(upperdirs)
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
1042
51000
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1043 if member.filename[-1] == '/':
54312
e67b1d071ed6 Merged revisions 72893 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 53754
diff changeset
1044 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
1045 os.mkdir(targetpath)
51000
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1046 return targetpath
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1047
80485
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1048 with self.open(member, pwd=pwd) as source, \
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1049 file(targetpath, "wb") as target:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1050 shutil.copyfileobj(source, target)
44576
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
1051
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
1052 return targetpath
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1053
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1054 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
1055 """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
1056 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
1057 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
1058 print "Duplicate name:", zinfo.filename
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1059 if self.mode not in ("w", "a"):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1060 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
1061 if not self.fp:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1062 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
1063 "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
1064 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
1065 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
1066 "Compression requires the (missing) zlib module"
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1067 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
1068 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
1069 "That compression method is not supported"
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1070 if zinfo.file_size > ZIP64_LIMIT:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1071 if not self._allowZip64:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1072 raise LargeZipFile("Filesize would require ZIP64 extensions")
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1073 if zinfo.header_offset > ZIP64_LIMIT:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1074 if not self._allowZip64:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1075 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
1076
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1077 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
1078 """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
1079 arcname."""
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
1080 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
1081 raise RuntimeError(
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
1082 "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
1083
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1084 st = os.stat(filename)
51000
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1085 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
1086 mtime = time.localtime(st.st_mtime)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1087 date_time = mtime[0:6]
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1088 # Create ZipInfo instance to store file information
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1089 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
1090 arcname = filename
46ec948cbde8 Bug #1413790: zipfile now sanitizes absolute archive names that are
Georg Brandl <georg@python.org>
parents: 36449
diff changeset
1091 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
1092 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
1093 arcname = arcname[1:]
51000
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1094 if isdir:
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1095 arcname += '/'
36609
46ec948cbde8 Bug #1413790: zipfile now sanitizes absolute archive names that are
Georg Brandl <georg@python.org>
parents: 36449
diff changeset
1096 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
1097 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
1098 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
1099 zinfo.compress_type = self.compression
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1100 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
1101 zinfo.compress_type = compress_type
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1102
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1103 zinfo.file_size = st.st_size
19737
56c549a253a6 [ #458701 ] Patch to zipfile.py for Java
Finn Bock <bckfnn@worldonline.dk>
parents: 18746
diff changeset
1104 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
1105 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
1106
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1107 self._writecheck(zinfo)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1108 self._didModify = True
51000
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1109
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1110 if isdir:
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1111 zinfo.file_size = 0
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1112 zinfo.compress_size = 0
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1113 zinfo.CRC = 0
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1114 self.filelist.append(zinfo)
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1115 self.NameToInfo[zinfo.filename] = zinfo
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1116 self.fp.write(zinfo.FileHeader())
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1117 return
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1118
53964
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1119 with open(filename, "rb") as fp:
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1120 # Must overwrite CRC and sizes with correct data later
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1121 zinfo.CRC = CRC = 0
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1122 zinfo.compress_size = compress_size = 0
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1123 zinfo.file_size = file_size = 0
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1124 self.fp.write(zinfo.FileHeader())
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1125 if zinfo.compress_type == ZIP_DEFLATED:
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1126 cmpr = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION,
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1127 zlib.DEFLATED, -15)
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1128 else:
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1129 cmpr = None
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1130 while 1:
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1131 buf = fp.read(1024 * 8)
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1132 if not buf:
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1133 break
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1134 file_size = file_size + len(buf)
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1135 CRC = crc32(buf, CRC) & 0xffffffff
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1136 if cmpr:
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1137 buf = cmpr.compress(buf)
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1138 compress_size = compress_size + len(buf)
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1139 self.fp.write(buf)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1140 if cmpr:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1141 buf = cmpr.flush()
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1142 compress_size = compress_size + len(buf)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1143 self.fp.write(buf)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1144 zinfo.compress_size = compress_size
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1145 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1146 zinfo.compress_size = file_size
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1147 zinfo.CRC = CRC
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1148 zinfo.file_size = file_size
19737
56c549a253a6 [ #458701 ] Patch to zipfile.py for Java
Finn Bock <bckfnn@worldonline.dk>
parents: 18746
diff changeset
1149 # Seek backwards and write CRC and file sizes
19953
0f9df563b65d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 19737
diff changeset
1150 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
1151 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
1152 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
1153 zinfo.file_size))
19737
56c549a253a6 [ #458701 ] Patch to zipfile.py for Java
Finn Bock <bckfnn@worldonline.dk>
parents: 18746
diff changeset
1154 self.fp.seek(position, 0)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1155 self.filelist.append(zinfo)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1156 self.NameToInfo[zinfo.filename] = zinfo
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1157
58821
a280844b68f4 Issue 6003: ZipFile.writestr "compression_type" argument
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58551
diff changeset
1158 def writestr(self, zinfo_or_arcname, bytes, 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
1159 """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
1160 '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
1161 the name of the file in the archive."""
8771993c9aac Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents: 25756
diff changeset
1162 if not isinstance(zinfo_or_arcname, ZipInfo):
8771993c9aac Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents: 25756
diff changeset
1163 zinfo = ZipInfo(filename=zinfo_or_arcname,
44677
827f0451734f 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: 39325
diff changeset
1164 date_time=time.localtime(time.time())[:6])
58821
a280844b68f4 Issue 6003: ZipFile.writestr "compression_type" argument
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58551
diff changeset
1165
26611
8771993c9aac Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents: 25756
diff changeset
1166 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
1167 zinfo.external_attr = 0600 << 16
26611
8771993c9aac Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents: 25756
diff changeset
1168 else:
8771993c9aac Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents: 25756
diff changeset
1169 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
1170
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
1171 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
1172 raise RuntimeError(
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
1173 "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
1174
58821
a280844b68f4 Issue 6003: ZipFile.writestr "compression_type" argument
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58551
diff changeset
1175 if compress_type is not None:
a280844b68f4 Issue 6003: ZipFile.writestr "compression_type" argument
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58551
diff changeset
1176 zinfo.compress_type = compress_type
a280844b68f4 Issue 6003: ZipFile.writestr "compression_type" argument
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58551
diff changeset
1177
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1178 zinfo.file_size = len(bytes) # Uncompressed size
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1179 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
1180 self._writecheck(zinfo)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1181 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
1182 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
1183 if zinfo.compress_type == ZIP_DEFLATED:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1184 co = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION,
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1185 zlib.DEFLATED, -15)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1186 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
1187 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
1188 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1189 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
1190 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
1191 self.fp.write(zinfo.FileHeader())
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1192 self.fp.write(bytes)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1193 self.fp.flush()
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1194 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
1195 # 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
1196 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
1197 zinfo.file_size))
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1198 self.filelist.append(zinfo)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1199 self.NameToInfo[zinfo.filename] = zinfo
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1200
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1201 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
1202 """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
1203 self.close()
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1204
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1205 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
1206 """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
1207 records."""
21098
82d8413636e7 SF bug 486480: zipfile __del__ is broken
Tim Peters <tim.peters@gmail.com>
parents: 20020
diff changeset
1208 if self.fp is None:
82d8413636e7 SF bug 486480: zipfile __del__ is broken
Tim Peters <tim.peters@gmail.com>
parents: 20020
diff changeset
1209 return
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
1210
80485
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1211 try:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1212 if self.mode in ("w", "a") and self._didModify: # write ending records
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1213 count = 0
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1214 pos1 = self.fp.tell()
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1215 for zinfo in self.filelist: # write central directory
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1216 count = count + 1
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1217 dt = zinfo.date_time
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1218 dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2]
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1219 dostime = dt[3] << 11 | dt[4] << 5 | (dt[5] // 2)
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1220 extra = []
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1221 if zinfo.file_size > ZIP64_LIMIT \
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1222 or zinfo.compress_size > ZIP64_LIMIT:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1223 extra.append(zinfo.file_size)
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1224 extra.append(zinfo.compress_size)
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1225 file_size = 0xffffffff
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1226 compress_size = 0xffffffff
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1227 else:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1228 file_size = zinfo.file_size
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1229 compress_size = zinfo.compress_size
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1230
80485
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1231 if zinfo.header_offset > ZIP64_LIMIT:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1232 extra.append(zinfo.header_offset)
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1233 header_offset = 0xffffffffL
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1234 else:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1235 header_offset = zinfo.header_offset
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1236
80485
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1237 extra_data = zinfo.extra
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1238 if extra:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1239 # Append a ZIP64 field to the extra's
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1240 extra_data = struct.pack(
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1241 '<HH' + 'Q'*len(extra),
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1242 1, 8*len(extra), *extra) + extra_data
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
1243
80485
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1244 extract_version = max(45, zinfo.extract_version)
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1245 create_version = max(45, zinfo.create_version)
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1246 else:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1247 extract_version = zinfo.extract_version
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1248 create_version = zinfo.create_version
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1249
80485
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1250 try:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1251 filename, flag_bits = zinfo._encodeFilenameFlags()
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1252 centdir = struct.pack(structCentralDir,
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1253 stringCentralDir, create_version,
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1254 zinfo.create_system, extract_version, zinfo.reserved,
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1255 flag_bits, zinfo.compress_type, dostime, dosdate,
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1256 zinfo.CRC, compress_size, file_size,
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1257 len(filename), len(extra_data), len(zinfo.comment),
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1258 0, zinfo.internal_attr, zinfo.external_attr,
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1259 header_offset)
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1260 except DeprecationWarning:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1261 print >>sys.stderr, (structCentralDir,
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1262 stringCentralDir, create_version,
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1263 zinfo.create_system, extract_version, zinfo.reserved,
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1264 zinfo.flag_bits, zinfo.compress_type, dostime, dosdate,
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1265 zinfo.CRC, compress_size, file_size,
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1266 len(zinfo.filename), len(extra_data), len(zinfo.comment),
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1267 0, zinfo.internal_attr, zinfo.external_attr,
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1268 header_offset)
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1269 raise
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1270 self.fp.write(centdir)
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1271 self.fp.write(filename)
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1272 self.fp.write(extra_data)
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1273 self.fp.write(zinfo.comment)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1274
80485
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1275 pos2 = self.fp.tell()
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1276 # Write end-of-zip-archive record
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1277 centDirCount = count
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1278 centDirSize = pos2 - pos1
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1279 centDirOffset = pos1
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1280 if (centDirCount >= ZIP_FILECOUNT_LIMIT or
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1281 centDirOffset > ZIP64_LIMIT or
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1282 centDirSize > ZIP64_LIMIT):
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1283 # Need to write the ZIP64 end-of-archive records
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1284 zip64endrec = struct.pack(
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1285 structEndArchive64, stringEndArchive64,
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1286 44, 45, 45, 0, 0, centDirCount, centDirCount,
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1287 centDirSize, centDirOffset)
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1288 self.fp.write(zip64endrec)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1289
80485
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1290 zip64locrec = struct.pack(
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1291 structEndArchive64Locator,
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1292 stringEndArchive64Locator, 0, pos2, 1)
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1293 self.fp.write(zip64locrec)
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1294 centDirCount = min(centDirCount, 0xFFFF)
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1295 centDirSize = min(centDirSize, 0xFFFFFFFF)
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1296 centDirOffset = min(centDirOffset, 0xFFFFFFFF)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1297
80485
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1298 endrec = struct.pack(structEndArchive, stringEndArchive,
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1299 0, 0, centDirCount, centDirCount,
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1300 centDirSize, centDirOffset, len(self._comment))
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1301 self.fp.write(endrec)
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1302 self.fp.write(self._comment)
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1303 self.fp.flush()
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1304 finally:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1305 fp = self.fp
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1306 self.fp = None
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1307 if not self._filePassed:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1308 fp.close()
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1309
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1310
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1311 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
1312 """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
1313
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1314 def writepy(self, pathname, basename = ""):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1315 """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
1316
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
1317 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
1318 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
1319 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
1320 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
1321 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
1322 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
1323 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
1324 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
1325 """
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1326 dir, name = os.path.split(pathname)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1327 if os.path.isdir(pathname):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1328 initname = os.path.join(pathname, "__init__.py")
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1329 if os.path.isfile(initname):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1330 # This is a package directory, add it
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1331 if basename:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1332 basename = "%s/%s" % (basename, name)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1333 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1334 basename = name
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1335 if self.debug:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1336 print "Adding package in", pathname, "as", basename
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1337 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
1338 if self.debug:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1339 print "Adding", arcname
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1340 self.write(fname, arcname)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1341 dirlist = os.listdir(pathname)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1342 dirlist.remove("__init__.py")
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1343 # Add all *.py files and package subdirectories
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1344 for filename in dirlist:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1345 path = os.path.join(pathname, filename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1346 root, ext = os.path.splitext(filename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1347 if os.path.isdir(path):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1348 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
1349 # This is a package directory, add it
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1350 self.writepy(path, basename) # Recursive call
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1351 elif ext == ".py":
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1352 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
1353 basename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1354 if self.debug:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1355 print "Adding", arcname
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1356 self.write(fname, arcname)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1357 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1358 # 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
1359 if self.debug:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1360 print "Adding files from directory", pathname
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1361 for filename in os.listdir(pathname):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1362 path = os.path.join(pathname, filename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1363 root, ext = os.path.splitext(filename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1364 if ext == ".py":
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1365 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
1366 basename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1367 if self.debug:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1368 print "Adding", arcname
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1369 self.write(fname, arcname)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1370 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1371 if pathname[-3:] != ".py":
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1372 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
1373 '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
1374 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
1375 if self.debug:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1376 print "Adding file", arcname
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1377 self.write(fname, arcname)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1378
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1379 def _get_codename(self, pathname, basename):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1380 """Return (filename, archivename) for the path.
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1381
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
1382 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
1383 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
1384 /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
1385 """
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1386 file_py = pathname + ".py"
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1387 file_pyc = pathname + ".pyc"
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1388 file_pyo = pathname + ".pyo"
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1389 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
1390 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
1391 fname = file_pyo # Use .pyo file
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1392 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
1393 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
1394 import py_compile
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1395 if self.debug:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1396 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
1397 try:
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 26611
diff changeset
1398 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
1399 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
1400 print err.msg
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1401 fname = file_pyc
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1402 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1403 fname = file_pyc
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1404 archivename = os.path.split(fname)[1]
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1405 if basename:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1406 archivename = "%s/%s" % (basename, archivename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1407 return (fname, archivename)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1408
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1409
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1410 def main(args = None):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1411 import textwrap
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1412 USAGE=textwrap.dedent("""\
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1413 Usage:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1414 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
1415 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
1416 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
1417 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
1418 """)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1419 if args is None:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1420 args = sys.argv[1:]
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1421
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1422 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
1423 print USAGE
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1424 sys.exit(1)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1425
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1426 if args[0] == '-l':
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1427 if len(args) != 2:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1428 print USAGE
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1429 sys.exit(1)
80485
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1430 with ZipFile(args[1], 'r') as zf:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1431 zf.printdir()
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1432
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1433 elif args[0] == '-t':
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1434 if len(args) != 2:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1435 print USAGE
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1436 sys.exit(1)
80485
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1437 with ZipFile(args[1], 'r') as zf:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1438 badfile = zf.testzip()
63935
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
1439 if badfile:
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
1440 print("The following enclosed file is corrupted: {!r}".format(badfile))
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1441 print "Done testing"
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1442
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1443 elif args[0] == '-e':
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1444 if len(args) != 3:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1445 print USAGE
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1446 sys.exit(1)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1447
80485
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1448 with ZipFile(args[1], 'r') as zf:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1449 out = args[2]
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1450 for path in zf.namelist():
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1451 if path.startswith('./'):
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1452 tgt = os.path.join(out, path[2:])
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1453 else:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1454 tgt = os.path.join(out, path)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1455
80485
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1456 tgtdir = os.path.dirname(tgt)
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1457 if not os.path.exists(tgtdir):
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1458 os.makedirs(tgtdir)
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1459 with open(tgt, 'wb') as fp:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1460 fp.write(zf.read(path))
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1461
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1462 elif args[0] == '-c':
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1463 if len(args) < 3:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1464 print USAGE
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1465 sys.exit(1)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1466
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1467 def addToZip(zf, path, zippath):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1468 if os.path.isfile(path):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1469 zf.write(path, zippath, ZIP_DEFLATED)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1470 elif os.path.isdir(path):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1471 for nm in os.listdir(path):
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
1472 addToZip(zf,
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1473 os.path.join(path, nm), os.path.join(zippath, nm))
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
1474 # else: ignore
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1475
80485
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1476 with ZipFile(args[1], 'w', allowZip64=True) as zf:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1477 for src in args[2:]:
eba28506eed3 Issue #16408: Fix file descriptors not being closed in error conditions in the zipfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80231
diff changeset
1478 addToZip(zf, src, os.path.basename(src))
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1479
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1480 if __name__ == "__main__":
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1481 main()