annotate Lib/zipfile.py @ 58765:2ba7677bfa95 legacy-trunk

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