annotate Lib/zipfile.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents ac0ec1f31b0a
children 6a14f692df1c
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
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
464 class ZipExtFile(io.BufferedIOBase):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
465 """File-like object for reading an archive member.
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
466 Is returned by ZipFile.open().
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
467 """
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
468
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
469 # Max size supported by decompressor.
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
470 MAX_N = 1 << 31 - 1
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
471
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
472 # Read from compressed files in 4k blocks.
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
473 MIN_READ_SIZE = 4096
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
474
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
475 # Search for universal newlines or line chunks.
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
476 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
477
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
478 def __init__(self, fileobj, mode, zipinfo, decrypter=None):
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
479 self._fileobj = fileobj
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
480 self._decrypter = decrypter
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
481
58551
f606474a210d avoid to use zlib when the compress type is not ZIP_DEFLATED
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58540
diff changeset
482 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
483 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
484 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
485
58551
f606474a210d avoid to use zlib when the compress type is not ZIP_DEFLATED
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58540
diff changeset
486 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
487 self._decompressor = zlib.decompressobj(-15)
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
488 self._unconsumed = ''
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
489
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
490 self._readbuffer = ''
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
491 self._offset = 0
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
492
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
493 self._universal = 'U' in mode
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
494 self.newlines = None
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
495
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
496 # 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
497 # are for the encryption/password information.
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
498 if self._decrypter is not None:
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
499 self._compress_left -= 12
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
500
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
501 self.mode = mode
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
502 self.name = zipinfo.filename
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
503
63935
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
504 if hasattr(zipinfo, 'CRC'):
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
505 self._expected_crc = zipinfo.CRC
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
506 self._running_crc = crc32(b'') & 0xffffffff
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
507 else:
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
508 self._expected_crc = None
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
509
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
510 def readline(self, limit=-1):
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
511 """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
512
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
513 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
514 """
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
515
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
516 if not self._universal and limit < 0:
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
517 # Shortcut common case - newline found in buffer.
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
518 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
519 if i > 0:
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
520 line = self._readbuffer[self._offset: i]
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
521 self._offset = i
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
522 return line
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
523
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
524 if not self._universal:
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
525 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
526
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
527 line = ''
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
528 while limit < 0 or len(line) < limit:
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
529 readahead = self.peek(2)
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
530 if readahead == '':
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
531 return line
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
532
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
533 #
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
534 # Search for universal newlines or line chunks.
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
535 #
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
536 # 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
537 # 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
538 # '\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
539 # 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
540 #
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
541 match = self.PATTERN.search(readahead)
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
542 newline = match.group('newline')
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
543 if newline is not None:
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
544 if self.newlines is None:
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
545 self.newlines = []
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
546 if newline not in self.newlines:
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
547 self.newlines.append(newline)
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
548 self._offset += len(newline)
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
549 return line + '\n'
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
550
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
551 chunk = match.group('chunk')
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
552 if limit >= 0:
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
553 chunk = chunk[: limit - len(line)]
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
554
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
555 self._offset += len(chunk)
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
556 line += chunk
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
557
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
558 return line
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
559
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
560 def peek(self, n=1):
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
561 """Returns buffered bytes without advancing the position."""
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
562 if n > len(self._readbuffer) - self._offset:
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
563 chunk = self.read(n)
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
564 self._offset -= len(chunk)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
565
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
566 # 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
567 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
568
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
569 def readable(self):
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
570 return True
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
571
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
572 def read(self, n=-1):
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
573 """Read and return up to n bytes.
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
574 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
575 """
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
576 buf = ''
64674
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
577 if n is None:
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
578 n = -1
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
579 while True:
64674
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
580 if n < 0:
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
581 data = self.read1(n)
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
582 elif n > len(buf):
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
583 data = self.read1(n - len(buf))
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
584 else:
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
585 return buf
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
586 if len(data) == 0:
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
587 return buf
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
588 buf += data
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
589
63935
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
590 def _update_crc(self, newdata, eof):
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
591 # Update the CRC using the given data.
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
592 if self._expected_crc is None:
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
593 # 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
594 return
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
595 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
596 # 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
597 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
598 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
599
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
600 def read1(self, n):
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
601 """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
602
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
603 # 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
604 if n < 0 or n is None:
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
605 n = self.MAX_N
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
606
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
607 # Bytes available in read buffer.
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
608 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
609
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
610 # Read from file.
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
611 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
612 nbytes = n - len_readbuffer - len(self._unconsumed)
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
613 nbytes = max(nbytes, self.MIN_READ_SIZE)
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
614 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
615
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
616 data = self._fileobj.read(nbytes)
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
617 self._compress_left -= len(data)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
618
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
619 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
620 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
621
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
622 if self._compress_type == ZIP_STORED:
63935
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
623 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
624 self._readbuffer = self._readbuffer[self._offset:] + data
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
625 self._offset = 0
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
626 else:
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
627 # Prepare deflated bytes for decompression.
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
628 self._unconsumed += data
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 # 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
631 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
632 self._compress_type == ZIP_DEFLATED):
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
633 data = self._decompressor.decompress(
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
634 self._unconsumed,
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
635 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
636 )
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
637
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
638 self._unconsumed = self._decompressor.unconsumed_tail
63935
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
639 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
640 if eof:
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
641 data += self._decompressor.flush()
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
642
63935
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
643 self._update_crc(data, eof=eof)
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
644 self._readbuffer = self._readbuffer[self._offset:] + data
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
645 self._offset = 0
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
646
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
647 # Read from buffer.
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
648 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
649 self._offset += len(data)
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
650 return data
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
651
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
652
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
653
76269
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
654 class ZipFile(object):
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17224
diff changeset
655 """ Class with methods to open, read, write, close, list zip files.
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17224
diff changeset
656
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
657 z = ZipFile(file, mode="r", compression=ZIP_STORED, allowZip64=False)
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17224
diff changeset
658
17224
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
659 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
660 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
661 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
662 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
663 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
664 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
665 be necessary.
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
666
17224
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
667 """
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
668
16826
a95693491eca Fix SF tracker bug #403871: AttributeError in ZipFile.__del__() when
Fred Drake <fdrake@acm.org>
parents: 15874
diff changeset
669 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
670
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
671 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
672 """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
673 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
674 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
675
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
676 if compression == ZIP_STORED:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
677 pass
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
678 elif compression == ZIP_DEFLATED:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
679 if not zlib:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
680 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
681 "Compression requires the (missing) zlib module"
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
682 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
683 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
684
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
685 self._allowZip64 = allowZip64
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
686 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
687 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
688 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
689 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
690 self.compression = compression # Method of compression
34594
32b89606f0cd Remove dependency on order of mode flags
Raymond Hettinger <python@rcn.com>
parents: 33914
diff changeset
691 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
692 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
693 self._comment = ''
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 # 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
696 if isinstance(file, basestring):
17224
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
697 self._filePassed = 0
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
698 self.filename = file
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
699 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
700 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
701 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
702 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
703 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
704 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
705 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
706 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
707 raise
17224
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
708 else:
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
709 self._filePassed = 1
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
710 self.fp = file
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
711 self.filename = getattr(file, 'name', None)
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17224
diff changeset
712
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
713 if key == 'r':
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
714 self._GetContents()
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
715 elif key == 'w':
66450
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
716 # 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
717 # 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
718 self._didModify = True
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
719 elif key == 'a':
66450
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
720 try:
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
721 # See if file is a zip file
25756
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
722 self._RealGetContents()
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
723 # seek to start of directory and overwrite
25756
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
724 self.fp.seek(self.start_dir, 0)
66450
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
725 except BadZipfile:
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
726 # file is not a zip file, just append
25756
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
727 self.fp.seek(0, 2)
66450
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
728
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
729 # 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
730 # 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
731 self._didModify = True
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
732 else:
17312
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
733 if not self._filePassed:
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
734 self.fp.close()
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
735 self.fp = None
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
736 raise RuntimeError, 'Mode must be "r", "w" or "a"'
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
737
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
738 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
739 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
740
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
741 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
742 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
743
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
744 def _GetContents(self):
17312
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
745 """Read the directory, making sure we close the file if the format
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
746 is bad."""
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
747 try:
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
748 self._RealGetContents()
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
749 except BadZipfile:
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
750 if not self._filePassed:
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
751 self.fp.close()
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
752 self.fp = None
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
753 raise
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
754
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
755 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
756 """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
757 fp = self.fp
66450
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
758 try:
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
759 endrec = _EndRecData(fp)
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
760 except IOError:
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
761 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
762 if not endrec:
63225811498d Patch #611760: read archives with comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 24756
diff changeset
763 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
764 if self.debug > 1:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
765 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
766 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
767 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
768 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
769
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
770 # "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
771 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
772 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
773 # 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
774 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
775
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
776 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
777 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
778 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
779 # 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
780 self.start_dir = offset_cd + concat
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
781 fp.seek(self.start_dir, 0)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
782 data = fp.read(size_cd)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
783 fp = cStringIO.StringIO(data)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
784 total = 0
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
785 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
786 centdir = fp.read(sizeCentralDir)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
787 if centdir[0:4] != stringCentralDir:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
788 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
789 centdir = struct.unpack(structCentralDir, centdir)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
790 if self.debug > 2:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
791 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
792 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
793 # Create ZipInfo instance to store file information
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
794 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
795 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
796 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
797 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
798 (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
799 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
800 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
801 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
802 # 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
803 x._raw_time = t
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
804 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
805 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
806
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
807 x._decodeExtra()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
808 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
809 x.filename = x._decodeFilename()
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
810 self.filelist.append(x)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
811 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
812
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
813 # 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
814 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
815 + 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
816 + 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
817
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
818 if self.debug > 2:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
819 print "total", total
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
820
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
821
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
822 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
823 """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
824 l = []
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
825 for data in self.filelist:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
826 l.append(data.filename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
827 return l
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
828
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
829 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
830 """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
831 archive."""
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
832 return self.filelist
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
833
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
834 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
835 """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
836 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
837 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
838 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
839 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
840
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
841 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
842 """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
843 chunk_size = 2 ** 20
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
844 for zinfo in self.filelist:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
845 try:
48265
f482875e2a03 fix ZipFile.testzip() to work with very large embedded files
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
846 # 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
847 # MemoryError with very large embedded files.
f482875e2a03 fix ZipFile.testzip() to work with very large embedded files
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
848 f = self.open(zinfo.filename, "r")
f482875e2a03 fix ZipFile.testzip() to work with very large embedded files
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
849 while f.read(chunk_size): # Check CRC-32
f482875e2a03 fix ZipFile.testzip() to work with very large embedded files
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
850 pass
29625
7d41785fb60c SF patch #756996: Bare except in ZipFile.testzip()
Raymond Hettinger <python@rcn.com>
parents: 29524
diff changeset
851 except BadZipfile:
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
852 return zinfo.filename
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
853
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
854 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
855 """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
856 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
857 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
858 raise KeyError(
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
859 '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
860
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
861 return info
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
862
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
863 def setpassword(self, pwd):
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
864 """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
865 self.pwd = pwd
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
866
76269
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
867 @property
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
868 def comment(self):
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
869 """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
870 return self._comment
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
871
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
872 @comment.setter
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
873 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
874 # 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
875 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
876 if self.debug:
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
877 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
878 % ZIP_MAX_COMMENT)
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
879 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
880 self._comment = comment
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
881 self._didModify = True
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
882
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
883 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
884 """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
885 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
886
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
887 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
888 """Return file-like object for 'name'."""
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
889 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
890 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
891 if not self.fp:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
892 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
893 "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
894
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
895 # 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
896 # 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
897 if self._filePassed:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
898 zef_file = self.fp
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
899 else:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
900 zef_file = open(self.filename, 'rb')
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
901
47195
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46671
diff changeset
902 # Make sure we have an info object
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46671
diff changeset
903 if isinstance(name, ZipInfo):
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46671
diff changeset
904 # 'name' is already an info object
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46671
diff changeset
905 zinfo = name
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46671
diff changeset
906 else:
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46671
diff changeset
907 # Get info object for name
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46671
diff changeset
908 zinfo = self.getinfo(name)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
909
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
910 zef_file.seek(zinfo.header_offset, 0)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
911
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
912 # Skip the file header:
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
913 fheader = zef_file.read(sizeFileHeader)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
914 if fheader[0:4] != stringFileHeader:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
915 raise BadZipfile, "Bad magic number for file header"
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
916
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
917 fheader = struct.unpack(structFileHeader, fheader)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
918 fname = zef_file.read(fheader[_FH_FILENAME_LENGTH])
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
919 if fheader[_FH_EXTRA_FIELD_LENGTH]:
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
920 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
921
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
922 if fname != zinfo.orig_filename:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
923 raise BadZipfile, \
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
924 'File name in directory "%s" and header "%s" differ.' % (
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
925 zinfo.orig_filename, fname)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
926
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
927 # check for encrypted flag & handle password
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
928 is_encrypted = zinfo.flag_bits & 0x1
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
929 zd = None
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
930 if is_encrypted:
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
931 if not pwd:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
932 pwd = self.pwd
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
933 if not pwd:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
934 raise RuntimeError, "File %s is encrypted, " \
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
935 "password required for extraction" % name
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
936
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
937 zd = _ZipDecrypter(pwd)
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
938 # The first 12 bytes in the cypher stream is an encryption header
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
939 # used to strengthen the algorithm. The first 11 bytes are
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
940 # completely random, while the 12th contains the MSB of the CRC,
44807
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44802
diff changeset
941 # or the MSB of the file time depending on the header type
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
942 # and is used to check the correctness of the password.
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
943 bytes = zef_file.read(12)
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 39325
diff changeset
944 h = map(zd, bytes[0:12])
44807
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44802
diff changeset
945 if zinfo.flag_bits & 0x8:
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44802
diff changeset
946 # compare against the file type from extended local headers
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44802
diff changeset
947 check_byte = (zinfo._raw_time >> 8) & 0xff
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44802
diff changeset
948 else:
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44802
diff changeset
949 # compare against the CRC otherwise
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44802
diff changeset
950 check_byte = (zinfo.CRC >> 24) & 0xff
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44802
diff changeset
951 if ord(h[11]) != check_byte:
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44802
diff changeset
952 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
953
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58000
diff changeset
954 return ZipExtFile(zef_file, mode, zinfo, zd)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
955
44576
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
956 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
957 """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
958 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
959 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
960 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
961 """
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
962 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
963 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
964
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
965 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
966 path = os.getcwd()
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
967
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
968 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
969
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
970 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
971 """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
972 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
973 `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
974 by namelist().
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
975 """
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
976 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
977 members = self.namelist()
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
978
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
979 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
980 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
981
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
982 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
983 """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
984 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
985 """
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
986 # 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
987 # forward slashes to platform specific separators.
53754
d3747b0a2f36 Merged revisions 72295 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51000
diff changeset
988 # 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
989 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
990 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
991 targetpath = targetpath[:-1]
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
992
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
993 # 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
994 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
995 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
996 else:
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
997 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
998
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
999 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
1000
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
1001 # 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
1002 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
1003 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
1004 os.makedirs(upperdirs)
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
1005
51000
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1006 if member.filename[-1] == '/':
54312
e67b1d071ed6 Merged revisions 72893 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 53754
diff changeset
1007 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
1008 os.mkdir(targetpath)
51000
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1009 return targetpath
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1010
47195
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46671
diff changeset
1011 source = self.open(member, pwd=pwd)
44576
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
1012 target = file(targetpath, "wb")
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
1013 shutil.copyfileobj(source, target)
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
1014 source.close()
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
1015 target.close()
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
1016
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
1017 return targetpath
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1018
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1019 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
1020 """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
1021 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
1022 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
1023 print "Duplicate name:", zinfo.filename
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1024 if self.mode not in ("w", "a"):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1025 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
1026 if not self.fp:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1027 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
1028 "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
1029 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
1030 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
1031 "Compression requires the (missing) zlib module"
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1032 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
1033 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
1034 "That compression method is not supported"
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1035 if zinfo.file_size > ZIP64_LIMIT:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1036 if not self._allowZip64:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1037 raise LargeZipFile("Filesize would require ZIP64 extensions")
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1038 if zinfo.header_offset > ZIP64_LIMIT:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1039 if not self._allowZip64:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1040 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
1041
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1042 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
1043 """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
1044 arcname."""
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
1045 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
1046 raise RuntimeError(
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
1047 "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
1048
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1049 st = os.stat(filename)
51000
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1050 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
1051 mtime = time.localtime(st.st_mtime)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1052 date_time = mtime[0:6]
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1053 # Create ZipInfo instance to store file information
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1054 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
1055 arcname = filename
46ec948cbde8 Bug #1413790: zipfile now sanitizes absolute archive names that are
Georg Brandl <georg@python.org>
parents: 36449
diff changeset
1056 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
1057 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
1058 arcname = arcname[1:]
51000
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1059 if isdir:
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1060 arcname += '/'
36609
46ec948cbde8 Bug #1413790: zipfile now sanitizes absolute archive names that are
Georg Brandl <georg@python.org>
parents: 36449
diff changeset
1061 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
1062 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
1063 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
1064 zinfo.compress_type = self.compression
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1065 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
1066 zinfo.compress_type = compress_type
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1067
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1068 zinfo.file_size = st.st_size
19737
56c549a253a6 [ #458701 ] Patch to zipfile.py for Java
Finn Bock <bckfnn@worldonline.dk>
parents: 18746
diff changeset
1069 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
1070 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
1071
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1072 self._writecheck(zinfo)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1073 self._didModify = True
51000
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1074
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1075 if isdir:
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1076 zinfo.file_size = 0
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1077 zinfo.compress_size = 0
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1078 zinfo.CRC = 0
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1079 self.filelist.append(zinfo)
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1080 self.NameToInfo[zinfo.filename] = zinfo
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1081 self.fp.write(zinfo.FileHeader())
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1082 return
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 50877
diff changeset
1083
53964
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1084 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
1085 # 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
1086 zinfo.CRC = CRC = 0
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1087 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
1088 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
1089 self.fp.write(zinfo.FileHeader())
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1090 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
1091 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
1092 zlib.DEFLATED, -15)
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1093 else:
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1094 cmpr = None
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1095 while 1:
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1096 buf = fp.read(1024 * 8)
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1097 if not buf:
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1098 break
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1099 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
1100 CRC = crc32(buf, CRC) & 0xffffffff
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1101 if cmpr:
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1102 buf = cmpr.compress(buf)
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1103 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
1104 self.fp.write(buf)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1105 if cmpr:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1106 buf = cmpr.flush()
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1107 compress_size = compress_size + len(buf)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1108 self.fp.write(buf)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1109 zinfo.compress_size = compress_size
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1110 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1111 zinfo.compress_size = file_size
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1112 zinfo.CRC = CRC
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1113 zinfo.file_size = file_size
19737
56c549a253a6 [ #458701 ] Patch to zipfile.py for Java
Finn Bock <bckfnn@worldonline.dk>
parents: 18746
diff changeset
1114 # Seek backwards and write CRC and file sizes
19953
0f9df563b65d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 19737
diff changeset
1115 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
1116 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
1117 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
1118 zinfo.file_size))
19737
56c549a253a6 [ #458701 ] Patch to zipfile.py for Java
Finn Bock <bckfnn@worldonline.dk>
parents: 18746
diff changeset
1119 self.fp.seek(position, 0)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1120 self.filelist.append(zinfo)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1121 self.NameToInfo[zinfo.filename] = zinfo
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1122
58821
a280844b68f4 Issue 6003: ZipFile.writestr "compression_type" argument
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58551
diff changeset
1123 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
1124 """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
1125 '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
1126 the name of the file in the archive."""
8771993c9aac Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents: 25756
diff changeset
1127 if not isinstance(zinfo_or_arcname, ZipInfo):
8771993c9aac Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents: 25756
diff changeset
1128 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
1129 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
1130
26611
8771993c9aac Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents: 25756
diff changeset
1131 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
1132 zinfo.external_attr = 0600 << 16
26611
8771993c9aac Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents: 25756
diff changeset
1133 else:
8771993c9aac Patch #651621, approved by MvL.
Just van Rossum <just@letterror.com>
parents: 25756
diff changeset
1134 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
1135
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
1136 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
1137 raise RuntimeError(
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41133
diff changeset
1138 "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
1139
58821
a280844b68f4 Issue 6003: ZipFile.writestr "compression_type" argument
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58551
diff changeset
1140 if compress_type is not None:
a280844b68f4 Issue 6003: ZipFile.writestr "compression_type" argument
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58551
diff changeset
1141 zinfo.compress_type = compress_type
a280844b68f4 Issue 6003: ZipFile.writestr "compression_type" argument
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58551
diff changeset
1142
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1143 zinfo.file_size = len(bytes) # Uncompressed size
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1144 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
1145 self._writecheck(zinfo)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1146 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
1147 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
1148 if zinfo.compress_type == ZIP_DEFLATED:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1149 co = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION,
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1150 zlib.DEFLATED, -15)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1151 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
1152 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
1153 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1154 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
1155 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
1156 self.fp.write(zinfo.FileHeader())
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1157 self.fp.write(bytes)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1158 self.fp.flush()
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1159 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
1160 # 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
1161 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
1162 zinfo.file_size))
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1163 self.filelist.append(zinfo)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1164 self.NameToInfo[zinfo.filename] = zinfo
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1165
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1166 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
1167 """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
1168 self.close()
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1169
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1170 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
1171 """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
1172 records."""
21098
82d8413636e7 SF bug 486480: zipfile __del__ is broken
Tim Peters <tim.peters@gmail.com>
parents: 20020
diff changeset
1173 if self.fp is None:
82d8413636e7 SF bug 486480: zipfile __del__ is broken
Tim Peters <tim.peters@gmail.com>
parents: 20020
diff changeset
1174 return
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
1175
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1176 if self.mode in ("w", "a") and self._didModify: # write ending records
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1177 count = 0
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1178 pos1 = self.fp.tell()
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 14975
diff changeset
1179 for zinfo in self.filelist: # write central directory
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1180 count = count + 1
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1181 dt = zinfo.date_time
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1182 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
1183 dostime = dt[3] << 11 | dt[4] << 5 | (dt[5] // 2)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1184 extra = []
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1185 if zinfo.file_size > ZIP64_LIMIT \
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1186 or zinfo.compress_size > ZIP64_LIMIT:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1187 extra.append(zinfo.file_size)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1188 extra.append(zinfo.compress_size)
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
1189 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
1190 compress_size = 0xffffffff
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1191 else:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1192 file_size = zinfo.file_size
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1193 compress_size = zinfo.compress_size
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1194
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1195 if zinfo.header_offset > ZIP64_LIMIT:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1196 extra.append(zinfo.header_offset)
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
1197 header_offset = 0xffffffffL
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1198 else:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1199 header_offset = zinfo.header_offset
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1200
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1201 extra_data = zinfo.extra
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1202 if extra:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1203 # Append a ZIP64 field to the extra's
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1204 extra_data = struct.pack(
45896
733c3ee19899 Use zlib's crc32 routine instead of binascii when available. zlib's is faster
Gregory P. Smith <greg@mad-scientist.com>
parents: 44809
diff changeset
1205 '<HH' + 'Q'*len(extra),
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1206 1, 8*len(extra), *extra) + extra_data
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
1207
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1208 extract_version = max(45, zinfo.extract_version)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1209 create_version = max(45, zinfo.create_version)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1210 else:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1211 extract_version = zinfo.extract_version
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1212 create_version = zinfo.create_version
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1213
45902
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1214 try:
46671
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
1215 filename, flag_bits = zinfo._encodeFilenameFlags()
45902
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1216 centdir = struct.pack(structCentralDir,
47932
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
1217 stringCentralDir, create_version,
45902
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1218 zinfo.create_system, extract_version, zinfo.reserved,
46671
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
1219 flag_bits, zinfo.compress_type, dostime, dosdate,
45902
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1220 zinfo.CRC, compress_size, file_size,
46671
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
1221 len(filename), len(extra_data), len(zinfo.comment),
45902
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1222 0, zinfo.internal_attr, zinfo.external_attr,
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1223 header_offset)
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1224 except DeprecationWarning:
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1225 print >>sys.stderr, (structCentralDir,
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1226 stringCentralDir, create_version,
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1227 zinfo.create_system, extract_version, zinfo.reserved,
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1228 zinfo.flag_bits, zinfo.compress_type, dostime, dosdate,
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1229 zinfo.CRC, compress_size, file_size,
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1230 len(zinfo.filename), len(extra_data), len(zinfo.comment),
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1231 0, zinfo.internal_attr, zinfo.external_attr,
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1232 header_offset)
d4e1db8797f6 Fix the struct module DeprecationWarnings that zipfile was triggering by
Gregory P. Smith <greg@mad-scientist.com>
parents: 45896
diff changeset
1233 raise
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1234 self.fp.write(centdir)
46671
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 45902
diff changeset
1235 self.fp.write(filename)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1236 self.fp.write(extra_data)
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1237 self.fp.write(zinfo.comment)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1238
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1239 pos2 = self.fp.tell()
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1240 # Write end-of-zip-archive record
50826
486e2579c978 Merged revisions 68661 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49036
diff changeset
1241 centDirCount = count
486e2579c978 Merged revisions 68661 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49036
diff changeset
1242 centDirSize = pos2 - pos1
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
1243 centDirOffset = pos1
50826
486e2579c978 Merged revisions 68661 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49036
diff changeset
1244 if (centDirCount >= ZIP_FILECOUNT_LIMIT or
486e2579c978 Merged revisions 68661 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49036
diff changeset
1245 centDirOffset > ZIP64_LIMIT or
486e2579c978 Merged revisions 68661 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49036
diff changeset
1246 centDirSize > ZIP64_LIMIT):
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1247 # Need to write the ZIP64 end-of-archive records
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1248 zip64endrec = struct.pack(
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1249 structEndArchive64, stringEndArchive64,
50826
486e2579c978 Merged revisions 68661 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49036
diff changeset
1250 44, 45, 45, 0, 0, centDirCount, centDirCount,
486e2579c978 Merged revisions 68661 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49036
diff changeset
1251 centDirSize, centDirOffset)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1252 self.fp.write(zip64endrec)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1253
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1254 zip64locrec = struct.pack(
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
1255 structEndArchive64Locator,
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1256 stringEndArchive64Locator, 0, pos2, 1)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1257 self.fp.write(zip64locrec)
50826
486e2579c978 Merged revisions 68661 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49036
diff changeset
1258 centDirCount = min(centDirCount, 0xFFFF)
486e2579c978 Merged revisions 68661 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49036
diff changeset
1259 centDirSize = min(centDirSize, 0xFFFFFFFF)
486e2579c978 Merged revisions 68661 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49036
diff changeset
1260 centDirOffset = min(centDirOffset, 0xFFFFFFFF)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1261
47932
13e525d974b1 #3317 in zipfile module, restore the previous names of global variables:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47882
diff changeset
1262 endrec = struct.pack(structEndArchive, stringEndArchive,
50826
486e2579c978 Merged revisions 68661 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49036
diff changeset
1263 0, 0, centDirCount, centDirCount,
76269
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
1264 centDirSize, centDirOffset, len(self._comment))
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
1265 self.fp.write(endrec)
76269
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
1266 self.fp.write(self._comment)
17517
1e23d91c0a82 Mark Favas points out that there's an 'self.fp.flush()' call in the
Guido van Rossum <guido@python.org>
parents: 17377
diff changeset
1267 self.fp.flush()
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
1268
17224
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
1269 if not self._filePassed:
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
1270 self.fp.close()
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1271 self.fp = None
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1272
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1273
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1274 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
1275 """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
1276
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1277 def writepy(self, pathname, basename = ""):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1278 """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
1279
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
1280 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
1281 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
1282 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
1283 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
1284 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
1285 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
1286 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
1287 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
1288 """
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1289 dir, name = os.path.split(pathname)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1290 if os.path.isdir(pathname):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1291 initname = os.path.join(pathname, "__init__.py")
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1292 if os.path.isfile(initname):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1293 # This is a package directory, add it
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1294 if basename:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1295 basename = "%s/%s" % (basename, name)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1296 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1297 basename = name
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1298 if self.debug:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1299 print "Adding package in", pathname, "as", basename
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1300 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
1301 if self.debug:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1302 print "Adding", arcname
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1303 self.write(fname, arcname)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1304 dirlist = os.listdir(pathname)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1305 dirlist.remove("__init__.py")
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1306 # Add all *.py files and package subdirectories
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1307 for filename in dirlist:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1308 path = os.path.join(pathname, filename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1309 root, ext = os.path.splitext(filename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1310 if os.path.isdir(path):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1311 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
1312 # This is a package directory, add it
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1313 self.writepy(path, basename) # Recursive call
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1314 elif ext == ".py":
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1315 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
1316 basename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1317 if self.debug:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1318 print "Adding", arcname
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1319 self.write(fname, arcname)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1320 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1321 # 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
1322 if self.debug:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1323 print "Adding files from directory", pathname
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1324 for filename in os.listdir(pathname):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1325 path = os.path.join(pathname, filename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1326 root, ext = os.path.splitext(filename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1327 if ext == ".py":
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1328 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
1329 basename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1330 if self.debug:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1331 print "Adding", arcname
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1332 self.write(fname, arcname)
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 if pathname[-3:] != ".py":
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1335 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
1336 '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
1337 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
1338 if self.debug:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1339 print "Adding file", 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
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1342 def _get_codename(self, pathname, basename):
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1343 """Return (filename, archivename) for the path.
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1344
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
1345 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
1346 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
1347 /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
1348 """
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1349 file_py = pathname + ".py"
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1350 file_pyc = pathname + ".pyc"
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1351 file_pyo = pathname + ".pyo"
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1352 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
1353 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
1354 fname = file_pyo # Use .pyo file
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1355 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
1356 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
1357 import py_compile
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1358 if self.debug:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1359 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
1360 try:
16a876746285 Patch #661719: Expose compilation errors as exceptions on request.
Martin v. Löwis <martin@v.loewis.de>
parents: 26611
diff changeset
1361 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
1362 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
1363 print err.msg
12197
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1364 fname = file_pyc
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1365 else:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1366 fname = file_pyc
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1367 archivename = os.path.split(fname)[1]
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1368 if basename:
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1369 archivename = "%s/%s" % (basename, archivename)
7f18f979a93a Zip file handling module, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1370 return (fname, archivename)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1371
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1372
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1373 def main(args = None):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1374 import textwrap
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1375 USAGE=textwrap.dedent("""\
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1376 Usage:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1377 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
1378 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
1379 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
1380 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
1381 """)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1382 if args is None:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1383 args = sys.argv[1:]
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1384
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1385 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
1386 print USAGE
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1387 sys.exit(1)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1388
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1389 if args[0] == '-l':
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1390 if len(args) != 2:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1391 print USAGE
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1392 sys.exit(1)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1393 zf = ZipFile(args[1], 'r')
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1394 zf.printdir()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1395 zf.close()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1396
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1397 elif args[0] == '-t':
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1398 if len(args) != 2:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1399 print USAGE
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1400 sys.exit(1)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1401 zf = ZipFile(args[1], 'r')
63935
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
1402 badfile = zf.testzip()
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
1403 if badfile:
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
1404 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
1405 print "Done testing"
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1406
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1407 elif args[0] == '-e':
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1408 if len(args) != 3:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1409 print USAGE
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1410 sys.exit(1)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1411
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1412 zf = ZipFile(args[1], 'r')
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1413 out = args[2]
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1414 for path in zf.namelist():
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
1415 if path.startswith('./'):
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1416 tgt = os.path.join(out, path[2:])
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1417 else:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1418 tgt = os.path.join(out, path)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1419
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1420 tgtdir = os.path.dirname(tgt)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1421 if not os.path.exists(tgtdir):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1422 os.makedirs(tgtdir)
53964
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1423 with open(tgt, 'wb') as fp:
3e84d60126b7 make sure files are closed using the with statement
Benjamin Peterson <benjamin@python.org>
parents: 53752
diff changeset
1424 fp.write(zf.read(path))
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1425 zf.close()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1426
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1427 elif args[0] == '-c':
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1428 if len(args) < 3:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1429 print USAGE
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1430 sys.exit(1)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1431
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1432 def addToZip(zf, path, zippath):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1433 if os.path.isfile(path):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1434 zf.write(path, zippath, ZIP_DEFLATED)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1435 elif os.path.isdir(path):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1436 for nm in os.listdir(path):
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
1437 addToZip(zf,
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1438 os.path.join(path, nm), os.path.join(zippath, nm))
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
1439 # else: ignore
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1440
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1441 zf = ZipFile(args[1], 'w', allowZip64=True)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1442 for src in args[2:]:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1443 addToZip(zf, src, os.path.basename(src))
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1444
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1445 zf.close()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1446
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1447 if __name__ == "__main__":
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36609
diff changeset
1448 main()