Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 1 | "Read and write ZIP files." |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 2 | # Written by James C. Ahlstrom jim@interet.com |
| 3 | # All rights transferred to CNRI pursuant to the Python contribution agreement |
| 4 | |
| 5 | import struct, os, time |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 6 | import binascii |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 7 | |
| 8 | try: |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 9 | import zlib # We may need its compression method |
Guido van Rossum | 9c673f3 | 2001-04-10 15:37:12 +0000 | [diff] [blame] | 10 | except ImportError: |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 11 | zlib = None |
| 12 | |
Skip Montanaro | 40fc160 | 2001-03-01 04:27:19 +0000 | [diff] [blame] | 13 | __all__ = ["BadZipfile", "error", "ZIP_STORED", "ZIP_DEFLATED", "is_zipfile", |
| 14 | "ZipInfo", "ZipFile", "PyZipFile"] |
| 15 | |
Fred Drake | 5db246d | 2000-09-29 20:44:48 +0000 | [diff] [blame] | 16 | class BadZipfile(Exception): |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 17 | pass |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 18 | error = BadZipfile # The exception raised by this module |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 19 | |
| 20 | # constants for Zip file compression methods |
| 21 | ZIP_STORED = 0 |
| 22 | ZIP_DEFLATED = 8 |
| 23 | # Other ZIP compression methods not supported |
| 24 | |
| 25 | # Here are some struct module formats for reading headers |
| 26 | structEndArchive = "<4s4H2lH" # 9 items, end of archive, 22 bytes |
| 27 | stringEndArchive = "PK\005\006" # magic number for end of archive record |
| 28 | structCentralDir = "<4s4B4H3l5H2l"# 19 items, central directory, 46 bytes |
| 29 | stringCentralDir = "PK\001\002" # magic number for central directory |
| 30 | structFileHeader = "<4s2B4H3l2H" # 12 items, file header record, 30 bytes |
| 31 | stringFileHeader = "PK\003\004" # magic number for file header |
| 32 | |
Fred Drake | 3e038e5 | 2001-02-28 17:56:26 +0000 | [diff] [blame] | 33 | # indexes of entries in the central directory structure |
| 34 | _CD_SIGNATURE = 0 |
| 35 | _CD_CREATE_VERSION = 1 |
| 36 | _CD_CREATE_SYSTEM = 2 |
| 37 | _CD_EXTRACT_VERSION = 3 |
| 38 | _CD_EXTRACT_SYSTEM = 4 # is this meaningful? |
| 39 | _CD_FLAG_BITS = 5 |
| 40 | _CD_COMPRESS_TYPE = 6 |
| 41 | _CD_TIME = 7 |
| 42 | _CD_DATE = 8 |
| 43 | _CD_CRC = 9 |
| 44 | _CD_COMPRESSED_SIZE = 10 |
| 45 | _CD_UNCOMPRESSED_SIZE = 11 |
| 46 | _CD_FILENAME_LENGTH = 12 |
| 47 | _CD_EXTRA_FIELD_LENGTH = 13 |
| 48 | _CD_COMMENT_LENGTH = 14 |
| 49 | _CD_DISK_NUMBER_START = 15 |
| 50 | _CD_INTERNAL_FILE_ATTRIBUTES = 16 |
| 51 | _CD_EXTERNAL_FILE_ATTRIBUTES = 17 |
| 52 | _CD_LOCAL_HEADER_OFFSET = 18 |
| 53 | |
| 54 | # indexes of entries in the local file header structure |
| 55 | _FH_SIGNATURE = 0 |
| 56 | _FH_EXTRACT_VERSION = 1 |
| 57 | _FH_EXTRACT_SYSTEM = 2 # is this meaningful? |
| 58 | _FH_GENERAL_PURPOSE_FLAG_BITS = 3 |
| 59 | _FH_COMPRESSION_METHOD = 4 |
| 60 | _FH_LAST_MOD_TIME = 5 |
| 61 | _FH_LAST_MOD_DATE = 6 |
| 62 | _FH_CRC = 7 |
| 63 | _FH_COMPRESSED_SIZE = 8 |
| 64 | _FH_UNCOMPRESSED_SIZE = 9 |
| 65 | _FH_FILENAME_LENGTH = 10 |
| 66 | _FH_EXTRA_FIELD_LENGTH = 11 |
| 67 | |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 68 | # Used to compare file passed to ZipFile |
Guido van Rossum | dbb718f | 2001-09-21 19:22:34 +0000 | [diff] [blame] | 69 | import types |
| 70 | _STRING_TYPES = (types.StringType,) |
| 71 | if hasattr(types, "UnicodeType"): |
| 72 | _STRING_TYPES = _STRING_TYPES + (types.UnicodeType,) |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 73 | |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 75 | def is_zipfile(filename): |
| 76 | """Quickly see if file is a ZIP file by checking the magic number. |
| 77 | |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 78 | Will not accept a ZIP archive with an ending comment. |
| 79 | """ |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 80 | try: |
| 81 | fpin = open(filename, "rb") |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 82 | fpin.seek(-22, 2) # Seek to end-of-file record |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 83 | endrec = fpin.read() |
| 84 | fpin.close() |
| 85 | if endrec[0:4] == "PK\005\006" and endrec[-2:] == "\000\000": |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 86 | return True # file has correct magic number |
Fred Drake | 7e47380 | 2001-05-11 19:52:57 +0000 | [diff] [blame] | 87 | except IOError: |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 88 | pass |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 89 | return False |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 90 | |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 91 | |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 92 | class ZipInfo: |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 93 | """Class with attributes describing each file in the ZIP archive.""" |
| 94 | |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 95 | def __init__(self, filename="NoName", date_time=(1980,1,1,0,0,0)): |
Fred Drake | a58947f | 2001-07-19 19:44:25 +0000 | [diff] [blame] | 96 | self.filename = _normpath(filename) # Name of the file in the archive |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 97 | self.date_time = date_time # year, month, day, hour, min, sec |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 98 | # Standard values: |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 99 | self.compress_type = ZIP_STORED # Type of compression for the file |
| 100 | self.comment = "" # Comment for each file |
| 101 | self.extra = "" # ZIP extra data |
| 102 | self.create_system = 0 # System which created ZIP archive |
| 103 | self.create_version = 20 # Version which created ZIP archive |
| 104 | self.extract_version = 20 # Version needed to extract archive |
| 105 | self.reserved = 0 # Must be zero |
| 106 | self.flag_bits = 0 # ZIP flag bits |
| 107 | self.volume = 0 # Volume number of file header |
| 108 | self.internal_attr = 0 # Internal attributes |
| 109 | self.external_attr = 0 # External file attributes |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 110 | # Other attributes are set by class ZipFile: |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 111 | # header_offset Byte offset to the file header |
| 112 | # file_offset Byte offset to the start of the file data |
| 113 | # CRC CRC-32 of the uncompressed file |
| 114 | # compress_size Size of the compressed file |
| 115 | # file_size Size of the uncompressed file |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 116 | |
| 117 | def FileHeader(self): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 118 | """Return the per-file header as a string.""" |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 119 | dt = self.date_time |
| 120 | dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2] |
Tim Peters | 3caca23 | 2001-12-06 06:23:26 +0000 | [diff] [blame] | 121 | dostime = dt[3] << 11 | dt[4] << 5 | (dt[5] // 2) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 122 | if self.flag_bits & 0x08: |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 123 | # Set these to zero because we write them after the file data |
| 124 | CRC = compress_size = file_size = 0 |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 125 | else: |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 126 | CRC = self.CRC |
| 127 | compress_size = self.compress_size |
| 128 | file_size = self.file_size |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 129 | header = struct.pack(structFileHeader, stringFileHeader, |
| 130 | self.extract_version, self.reserved, self.flag_bits, |
| 131 | self.compress_type, dostime, dosdate, CRC, |
| 132 | compress_size, file_size, |
| 133 | len(self.filename), len(self.extra)) |
| 134 | return header + self.filename + self.extra |
| 135 | |
| 136 | |
Fred Drake | a58947f | 2001-07-19 19:44:25 +0000 | [diff] [blame] | 137 | # This is used to ensure paths in generated ZIP files always use |
| 138 | # forward slashes as the directory separator, as required by the |
| 139 | # ZIP format specification. |
| 140 | if os.sep != "/": |
| 141 | def _normpath(path): |
| 142 | return path.replace(os.sep, "/") |
| 143 | else: |
| 144 | def _normpath(path): |
| 145 | return path |
| 146 | |
| 147 | |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 148 | class ZipFile: |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 149 | """ Class with methods to open, read, write, close, list zip files. |
| 150 | |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 151 | z = ZipFile(file, mode="r", compression=ZIP_STORED) |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 152 | |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 153 | file: Either the path to the file, or a file-like object. |
| 154 | If it is a path, the file will be opened and closed by ZipFile. |
| 155 | mode: The mode can be either read "r", write "w" or append "a". |
| 156 | compression: ZIP_STORED (no compression) or ZIP_DEFLATED (requires zlib). |
| 157 | """ |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 158 | |
Fred Drake | 90eac28 | 2001-02-28 05:29:34 +0000 | [diff] [blame] | 159 | fp = None # Set here since __del__ checks it |
| 160 | |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 161 | def __init__(self, file, mode="r", compression=ZIP_STORED): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 162 | """Open the ZIP file with mode read "r", write "w" or append "a".""" |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 163 | if compression == ZIP_STORED: |
| 164 | pass |
| 165 | elif compression == ZIP_DEFLATED: |
| 166 | if not zlib: |
| 167 | raise RuntimeError,\ |
Fred Drake | 5db246d | 2000-09-29 20:44:48 +0000 | [diff] [blame] | 168 | "Compression requires the (missing) zlib module" |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 169 | else: |
| 170 | raise RuntimeError, "That compression method is not supported" |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 171 | self.debug = 0 # Level of printing: 0 through 3 |
| 172 | self.NameToInfo = {} # Find file info given name |
| 173 | self.filelist = [] # List of ZipInfo instances for archive |
| 174 | self.compression = compression # Method of compression |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 175 | self.mode = key = mode[0] |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 176 | |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 177 | # Check if we were passed a file-like object |
| 178 | if type(file) in _STRING_TYPES: |
| 179 | self._filePassed = 0 |
| 180 | self.filename = file |
| 181 | modeDict = {'r' : 'rb', 'w': 'wb', 'a' : 'r+b'} |
| 182 | self.fp = open(file, modeDict[mode]) |
| 183 | else: |
| 184 | self._filePassed = 1 |
| 185 | self.fp = file |
| 186 | self.filename = getattr(file, 'name', None) |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 187 | |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 188 | if key == 'r': |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 189 | self._GetContents() |
| 190 | elif key == 'w': |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 191 | pass |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 192 | elif key == 'a': |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 193 | fp = self.fp |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 194 | fp.seek(-22, 2) # Seek to end-of-file record |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 195 | endrec = fp.read() |
| 196 | if endrec[0:4] == stringEndArchive and \ |
| 197 | endrec[-2:] == "\000\000": |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 198 | self._GetContents() # file is a zip file |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 199 | # seek to start of directory and overwrite |
| 200 | fp.seek(self.start_dir, 0) |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 201 | else: # file is not a zip file, just append |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 202 | fp.seek(0, 2) |
| 203 | else: |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 204 | if not self._filePassed: |
| 205 | self.fp.close() |
| 206 | self.fp = None |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 207 | raise RuntimeError, 'Mode must be "r", "w" or "a"' |
| 208 | |
| 209 | def _GetContents(self): |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 210 | """Read the directory, making sure we close the file if the format |
| 211 | is bad.""" |
| 212 | try: |
| 213 | self._RealGetContents() |
| 214 | except BadZipfile: |
| 215 | if not self._filePassed: |
| 216 | self.fp.close() |
| 217 | self.fp = None |
| 218 | raise |
| 219 | |
| 220 | def _RealGetContents(self): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 221 | """Read in the table of contents for the ZIP file.""" |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 222 | fp = self.fp |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 223 | fp.seek(-22, 2) # Start of end-of-archive record |
| 224 | filesize = fp.tell() + 22 # Get file size |
| 225 | endrec = fp.read(22) # Archive must not end with a comment! |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 226 | if endrec[0:4] != stringEndArchive or endrec[-2:] != "\000\000": |
| 227 | raise BadZipfile, "File is not a zip file, or ends with a comment" |
| 228 | endrec = struct.unpack(structEndArchive, endrec) |
| 229 | if self.debug > 1: |
| 230 | print endrec |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 231 | size_cd = endrec[5] # bytes in central directory |
| 232 | offset_cd = endrec[6] # offset of central directory |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 233 | x = filesize - 22 - size_cd |
| 234 | # "concat" is zero, unless zip was concatenated to another file |
| 235 | concat = x - offset_cd |
| 236 | if self.debug > 2: |
| 237 | print "given, inferred, offset", offset_cd, x, concat |
| 238 | # self.start_dir: Position of start of central directory |
| 239 | self.start_dir = offset_cd + concat |
| 240 | fp.seek(self.start_dir, 0) |
| 241 | total = 0 |
| 242 | while total < size_cd: |
| 243 | centdir = fp.read(46) |
| 244 | total = total + 46 |
| 245 | if centdir[0:4] != stringCentralDir: |
| 246 | raise BadZipfile, "Bad magic number for central directory" |
| 247 | centdir = struct.unpack(structCentralDir, centdir) |
| 248 | if self.debug > 2: |
| 249 | print centdir |
Fred Drake | 3e038e5 | 2001-02-28 17:56:26 +0000 | [diff] [blame] | 250 | filename = fp.read(centdir[_CD_FILENAME_LENGTH]) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 251 | # Create ZipInfo instance to store file information |
| 252 | x = ZipInfo(filename) |
Fred Drake | 3e038e5 | 2001-02-28 17:56:26 +0000 | [diff] [blame] | 253 | x.extra = fp.read(centdir[_CD_EXTRA_FIELD_LENGTH]) |
| 254 | x.comment = fp.read(centdir[_CD_COMMENT_LENGTH]) |
| 255 | total = (total + centdir[_CD_FILENAME_LENGTH] |
| 256 | + centdir[_CD_EXTRA_FIELD_LENGTH] |
| 257 | + centdir[_CD_COMMENT_LENGTH]) |
| 258 | x.header_offset = centdir[_CD_LOCAL_HEADER_OFFSET] + concat |
| 259 | # file_offset must be computed below... |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 260 | (x.create_version, x.create_system, x.extract_version, x.reserved, |
| 261 | x.flag_bits, x.compress_type, t, d, |
| 262 | x.CRC, x.compress_size, x.file_size) = centdir[1:12] |
| 263 | x.volume, x.internal_attr, x.external_attr = centdir[15:18] |
| 264 | # Convert date/time code to (year, month, day, hour, min, sec) |
| 265 | x.date_time = ( (d>>9)+1980, (d>>5)&0xF, d&0x1F, |
Fred Drake | 414ca66 | 2000-06-13 18:49:53 +0000 | [diff] [blame] | 266 | t>>11, (t>>5)&0x3F, (t&0x1F) * 2 ) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 267 | self.filelist.append(x) |
| 268 | self.NameToInfo[x.filename] = x |
| 269 | if self.debug > 2: |
| 270 | print "total", total |
| 271 | for data in self.filelist: |
| 272 | fp.seek(data.header_offset, 0) |
| 273 | fheader = fp.read(30) |
| 274 | if fheader[0:4] != stringFileHeader: |
| 275 | raise BadZipfile, "Bad magic number for file header" |
| 276 | fheader = struct.unpack(structFileHeader, fheader) |
Fred Drake | 3e038e5 | 2001-02-28 17:56:26 +0000 | [diff] [blame] | 277 | # file_offset is computed here, since the extra field for |
| 278 | # the central directory and for the local file header |
| 279 | # refer to different fields, and they can have different |
| 280 | # lengths |
| 281 | data.file_offset = (data.header_offset + 30 |
| 282 | + fheader[_FH_FILENAME_LENGTH] |
| 283 | + fheader[_FH_EXTRA_FIELD_LENGTH]) |
| 284 | fname = fp.read(fheader[_FH_FILENAME_LENGTH]) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 285 | if fname != data.filename: |
| 286 | raise RuntimeError, \ |
Fred Drake | 5db246d | 2000-09-29 20:44:48 +0000 | [diff] [blame] | 287 | 'File name in directory "%s" and header "%s" differ.' % ( |
| 288 | data.filename, fname) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 289 | |
| 290 | def namelist(self): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 291 | """Return a list of file names in the archive.""" |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 292 | l = [] |
| 293 | for data in self.filelist: |
| 294 | l.append(data.filename) |
| 295 | return l |
| 296 | |
| 297 | def infolist(self): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 298 | """Return a list of class ZipInfo instances for files in the |
| 299 | archive.""" |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 300 | return self.filelist |
| 301 | |
| 302 | def printdir(self): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 303 | """Print a table of contents for the zip file.""" |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 304 | print "%-46s %19s %12s" % ("File Name", "Modified ", "Size") |
| 305 | for zinfo in self.filelist: |
| 306 | date = "%d-%02d-%02d %02d:%02d:%02d" % zinfo.date_time |
| 307 | print "%-46s %s %12d" % (zinfo.filename, date, zinfo.file_size) |
| 308 | |
| 309 | def testzip(self): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 310 | """Read all the files and check the CRC.""" |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 311 | for zinfo in self.filelist: |
| 312 | try: |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 313 | self.read(zinfo.filename) # Check CRC-32 |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 314 | except: |
| 315 | return zinfo.filename |
| 316 | |
| 317 | def getinfo(self, name): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 318 | """Return the instance of ZipInfo given 'name'.""" |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 319 | return self.NameToInfo[name] |
| 320 | |
| 321 | def read(self, name): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 322 | """Return file bytes (as a string) for name.""" |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 323 | if self.mode not in ("r", "a"): |
| 324 | raise RuntimeError, 'read() requires mode "r" or "a"' |
| 325 | if not self.fp: |
| 326 | raise RuntimeError, \ |
Fred Drake | 5db246d | 2000-09-29 20:44:48 +0000 | [diff] [blame] | 327 | "Attempt to read ZIP archive that was already closed" |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 328 | zinfo = self.getinfo(name) |
| 329 | filepos = self.fp.tell() |
| 330 | self.fp.seek(zinfo.file_offset, 0) |
| 331 | bytes = self.fp.read(zinfo.compress_size) |
| 332 | self.fp.seek(filepos, 0) |
| 333 | if zinfo.compress_type == ZIP_STORED: |
| 334 | pass |
| 335 | elif zinfo.compress_type == ZIP_DEFLATED: |
| 336 | if not zlib: |
| 337 | raise RuntimeError, \ |
Fred Drake | 5db246d | 2000-09-29 20:44:48 +0000 | [diff] [blame] | 338 | "De-compression requires the (missing) zlib module" |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 339 | # zlib compress/decompress code by Jeremy Hylton of CNRI |
| 340 | dc = zlib.decompressobj(-15) |
| 341 | bytes = dc.decompress(bytes) |
| 342 | # need to feed in unused pad byte so that zlib won't choke |
| 343 | ex = dc.decompress('Z') + dc.flush() |
| 344 | if ex: |
| 345 | bytes = bytes + ex |
| 346 | else: |
| 347 | raise BadZipfile, \ |
Fred Drake | 5db246d | 2000-09-29 20:44:48 +0000 | [diff] [blame] | 348 | "Unsupported compression method %d for file %s" % \ |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 349 | (zinfo.compress_type, name) |
| 350 | crc = binascii.crc32(bytes) |
| 351 | if crc != zinfo.CRC: |
| 352 | raise BadZipfile, "Bad CRC-32 for file %s" % name |
| 353 | return bytes |
| 354 | |
| 355 | def _writecheck(self, zinfo): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 356 | """Check for errors before writing a file to the archive.""" |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 357 | if self.NameToInfo.has_key(zinfo.filename): |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 358 | if self.debug: # Warning for duplicate names |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 359 | print "Duplicate name:", zinfo.filename |
| 360 | if self.mode not in ("w", "a"): |
| 361 | raise RuntimeError, 'write() requires mode "w" or "a"' |
| 362 | if not self.fp: |
| 363 | raise RuntimeError, \ |
Fred Drake | 5db246d | 2000-09-29 20:44:48 +0000 | [diff] [blame] | 364 | "Attempt to write ZIP archive that was already closed" |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 365 | if zinfo.compress_type == ZIP_DEFLATED and not zlib: |
| 366 | raise RuntimeError, \ |
Fred Drake | 5db246d | 2000-09-29 20:44:48 +0000 | [diff] [blame] | 367 | "Compression requires the (missing) zlib module" |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 368 | if zinfo.compress_type not in (ZIP_STORED, ZIP_DEFLATED): |
| 369 | raise RuntimeError, \ |
Fred Drake | 5db246d | 2000-09-29 20:44:48 +0000 | [diff] [blame] | 370 | "That compression method is not supported" |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 371 | |
| 372 | def write(self, filename, arcname=None, compress_type=None): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 373 | """Put the bytes from filename into the archive under the name |
| 374 | arcname.""" |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 375 | st = os.stat(filename) |
| 376 | mtime = time.localtime(st[8]) |
| 377 | date_time = mtime[0:6] |
| 378 | # Create ZipInfo instance to store file information |
| 379 | if arcname is None: |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 380 | zinfo = ZipInfo(filename, date_time) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 381 | else: |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 382 | zinfo = ZipInfo(arcname, date_time) |
| 383 | zinfo.external_attr = st[0] << 16 # Unix attributes |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 384 | if compress_type is None: |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 385 | zinfo.compress_type = self.compression |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 386 | else: |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 387 | zinfo.compress_type = compress_type |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 388 | self._writecheck(zinfo) |
| 389 | fp = open(filename, "rb") |
Finn Bock | 03a3bb8 | 2001-09-05 18:40:33 +0000 | [diff] [blame] | 390 | zinfo.flag_bits = 0x00 |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 391 | zinfo.header_offset = self.fp.tell() # Start of header bytes |
Finn Bock | 03a3bb8 | 2001-09-05 18:40:33 +0000 | [diff] [blame] | 392 | # Must overwrite CRC and sizes with correct data later |
| 393 | zinfo.CRC = CRC = 0 |
| 394 | zinfo.compress_size = compress_size = 0 |
| 395 | zinfo.file_size = file_size = 0 |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 396 | self.fp.write(zinfo.FileHeader()) |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 397 | zinfo.file_offset = self.fp.tell() # Start of file bytes |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 398 | if zinfo.compress_type == ZIP_DEFLATED: |
| 399 | cmpr = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION, |
| 400 | zlib.DEFLATED, -15) |
| 401 | else: |
| 402 | cmpr = None |
| 403 | while 1: |
| 404 | buf = fp.read(1024 * 8) |
| 405 | if not buf: |
| 406 | break |
| 407 | file_size = file_size + len(buf) |
| 408 | CRC = binascii.crc32(buf, CRC) |
| 409 | if cmpr: |
| 410 | buf = cmpr.compress(buf) |
| 411 | compress_size = compress_size + len(buf) |
| 412 | self.fp.write(buf) |
| 413 | fp.close() |
| 414 | if cmpr: |
| 415 | buf = cmpr.flush() |
| 416 | compress_size = compress_size + len(buf) |
| 417 | self.fp.write(buf) |
| 418 | zinfo.compress_size = compress_size |
| 419 | else: |
| 420 | zinfo.compress_size = file_size |
| 421 | zinfo.CRC = CRC |
| 422 | zinfo.file_size = file_size |
Finn Bock | 03a3bb8 | 2001-09-05 18:40:33 +0000 | [diff] [blame] | 423 | # Seek backwards and write CRC and file sizes |
Tim Peters | b64bec3 | 2001-09-18 02:26:39 +0000 | [diff] [blame] | 424 | position = self.fp.tell() # Preserve current position in file |
Finn Bock | 03a3bb8 | 2001-09-05 18:40:33 +0000 | [diff] [blame] | 425 | self.fp.seek(zinfo.header_offset + 14, 0) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 426 | self.fp.write(struct.pack("<lll", zinfo.CRC, zinfo.compress_size, |
| 427 | zinfo.file_size)) |
Finn Bock | 03a3bb8 | 2001-09-05 18:40:33 +0000 | [diff] [blame] | 428 | self.fp.seek(position, 0) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 429 | self.filelist.append(zinfo) |
| 430 | self.NameToInfo[zinfo.filename] = zinfo |
| 431 | |
| 432 | def writestr(self, zinfo, bytes): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 433 | """Write a file into the archive. The contents is the string |
| 434 | 'bytes'.""" |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 435 | self._writecheck(zinfo) |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 436 | zinfo.file_size = len(bytes) # Uncompressed size |
| 437 | zinfo.CRC = binascii.crc32(bytes) # CRC-32 checksum |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 438 | if zinfo.compress_type == ZIP_DEFLATED: |
| 439 | co = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION, |
| 440 | zlib.DEFLATED, -15) |
| 441 | bytes = co.compress(bytes) + co.flush() |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 442 | zinfo.compress_size = len(bytes) # Compressed size |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 443 | else: |
| 444 | zinfo.compress_size = zinfo.file_size |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 445 | zinfo.header_offset = self.fp.tell() # Start of header bytes |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 446 | self.fp.write(zinfo.FileHeader()) |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 447 | zinfo.file_offset = self.fp.tell() # Start of file bytes |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 448 | self.fp.write(bytes) |
| 449 | if zinfo.flag_bits & 0x08: |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 450 | # Write CRC and file sizes after the file data |
| 451 | self.fp.write(struct.pack("<lll", zinfo.CRC, zinfo.compress_size, |
| 452 | zinfo.file_size)) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 453 | self.filelist.append(zinfo) |
| 454 | self.NameToInfo[zinfo.filename] = zinfo |
| 455 | |
| 456 | def __del__(self): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 457 | """Call the "close()" method in case the user forgot.""" |
Tim Peters | d15f8bb | 2001-11-28 23:16:40 +0000 | [diff] [blame] | 458 | self.close() |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 459 | |
| 460 | def close(self): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 461 | """Close the file, and for mode "w" and "a" write the ending |
| 462 | records.""" |
Tim Peters | d15f8bb | 2001-11-28 23:16:40 +0000 | [diff] [blame] | 463 | if self.fp is None: |
| 464 | return |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 465 | if self.mode in ("w", "a"): # write ending records |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 466 | count = 0 |
| 467 | pos1 = self.fp.tell() |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 468 | for zinfo in self.filelist: # write central directory |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 469 | count = count + 1 |
| 470 | dt = zinfo.date_time |
| 471 | dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2] |
Tim Peters | 3caca23 | 2001-12-06 06:23:26 +0000 | [diff] [blame] | 472 | dostime = dt[3] << 11 | dt[4] << 5 | (dt[5] // 2) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 473 | centdir = struct.pack(structCentralDir, |
| 474 | stringCentralDir, zinfo.create_version, |
| 475 | zinfo.create_system, zinfo.extract_version, zinfo.reserved, |
| 476 | zinfo.flag_bits, zinfo.compress_type, dostime, dosdate, |
| 477 | zinfo.CRC, zinfo.compress_size, zinfo.file_size, |
| 478 | len(zinfo.filename), len(zinfo.extra), len(zinfo.comment), |
| 479 | 0, zinfo.internal_attr, zinfo.external_attr, |
| 480 | zinfo.header_offset) |
| 481 | self.fp.write(centdir) |
| 482 | self.fp.write(zinfo.filename) |
| 483 | self.fp.write(zinfo.extra) |
| 484 | self.fp.write(zinfo.comment) |
| 485 | pos2 = self.fp.tell() |
| 486 | # Write end-of-zip-archive record |
| 487 | endrec = struct.pack(structEndArchive, stringEndArchive, |
| 488 | 0, 0, count, count, pos2 - pos1, pos1, 0) |
| 489 | self.fp.write(endrec) |
Guido van Rossum | f85af61 | 2001-04-14 16:45:14 +0000 | [diff] [blame] | 490 | self.fp.flush() |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 491 | if not self._filePassed: |
| 492 | self.fp.close() |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 493 | self.fp = None |
| 494 | |
| 495 | |
| 496 | class PyZipFile(ZipFile): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 497 | """Class to create ZIP archives with Python library files and packages.""" |
| 498 | |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 499 | def writepy(self, pathname, basename = ""): |
| 500 | """Add all files from "pathname" to the ZIP archive. |
| 501 | |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 502 | If pathname is a package directory, search the directory and |
| 503 | all package subdirectories recursively for all *.py and enter |
| 504 | the modules into the archive. If pathname is a plain |
| 505 | directory, listdir *.py and enter all modules. Else, pathname |
| 506 | must be a Python *.py file and the module will be put into the |
| 507 | archive. Added modules are always module.pyo or module.pyc. |
| 508 | This method will compile the module.py into module.pyc if |
| 509 | necessary. |
| 510 | """ |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 511 | dir, name = os.path.split(pathname) |
| 512 | if os.path.isdir(pathname): |
| 513 | initname = os.path.join(pathname, "__init__.py") |
| 514 | if os.path.isfile(initname): |
| 515 | # This is a package directory, add it |
| 516 | if basename: |
| 517 | basename = "%s/%s" % (basename, name) |
| 518 | else: |
| 519 | basename = name |
| 520 | if self.debug: |
| 521 | print "Adding package in", pathname, "as", basename |
| 522 | fname, arcname = self._get_codename(initname[0:-3], basename) |
| 523 | if self.debug: |
| 524 | print "Adding", arcname |
| 525 | self.write(fname, arcname) |
| 526 | dirlist = os.listdir(pathname) |
| 527 | dirlist.remove("__init__.py") |
| 528 | # Add all *.py files and package subdirectories |
| 529 | for filename in dirlist: |
| 530 | path = os.path.join(pathname, filename) |
| 531 | root, ext = os.path.splitext(filename) |
| 532 | if os.path.isdir(path): |
| 533 | if os.path.isfile(os.path.join(path, "__init__.py")): |
| 534 | # This is a package directory, add it |
| 535 | self.writepy(path, basename) # Recursive call |
| 536 | elif ext == ".py": |
| 537 | fname, arcname = self._get_codename(path[0:-3], |
| 538 | basename) |
| 539 | if self.debug: |
| 540 | print "Adding", arcname |
| 541 | self.write(fname, arcname) |
| 542 | else: |
| 543 | # This is NOT a package directory, add its files at top level |
| 544 | if self.debug: |
| 545 | print "Adding files from directory", pathname |
| 546 | for filename in os.listdir(pathname): |
| 547 | path = os.path.join(pathname, filename) |
| 548 | root, ext = os.path.splitext(filename) |
| 549 | if ext == ".py": |
| 550 | fname, arcname = self._get_codename(path[0:-3], |
| 551 | basename) |
| 552 | if self.debug: |
| 553 | print "Adding", arcname |
| 554 | self.write(fname, arcname) |
| 555 | else: |
| 556 | if pathname[-3:] != ".py": |
| 557 | raise RuntimeError, \ |
Fred Drake | 5db246d | 2000-09-29 20:44:48 +0000 | [diff] [blame] | 558 | 'Files added with writepy() must end with ".py"' |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 559 | fname, arcname = self._get_codename(pathname[0:-3], basename) |
| 560 | if self.debug: |
| 561 | print "Adding file", arcname |
| 562 | self.write(fname, arcname) |
| 563 | |
| 564 | def _get_codename(self, pathname, basename): |
| 565 | """Return (filename, archivename) for the path. |
| 566 | |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 567 | Given a module name path, return the correct file path and |
| 568 | archive name, compiling if necessary. For example, given |
| 569 | /python/lib/string, return (/python/lib/string.pyc, string). |
| 570 | """ |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 571 | file_py = pathname + ".py" |
| 572 | file_pyc = pathname + ".pyc" |
| 573 | file_pyo = pathname + ".pyo" |
| 574 | if os.path.isfile(file_pyo) and \ |
| 575 | os.stat(file_pyo)[8] >= os.stat(file_py)[8]: |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 576 | fname = file_pyo # Use .pyo file |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 577 | elif not os.path.isfile(file_pyc) or \ |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 578 | os.stat(file_pyc)[8] < os.stat(file_py)[8]: |
| 579 | import py_compile |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 580 | if self.debug: |
| 581 | print "Compiling", file_py |
| 582 | py_compile.compile(file_py, file_pyc) |
| 583 | fname = file_pyc |
| 584 | else: |
| 585 | fname = file_pyc |
| 586 | archivename = os.path.split(fname)[1] |
| 587 | if basename: |
| 588 | archivename = "%s/%s" % (basename, archivename) |
| 589 | return (fname, archivename) |