comparison zipfiles/zipfile31.py @ 170:b993d9257400

Updated zipfiles
author Oleg Oshmyan <chortos@inbox.lv>
date Thu, 16 Jun 2011 01:24:10 +0100
parents 45d4a9dc707b
children
comparison
equal deleted inserted replaced
169:d7f4b051ad79 170:b993d9257400
896 896
897 return info 897 return info
898 898
899 def setpassword(self, pwd): 899 def setpassword(self, pwd):
900 """Set default password for encrypted files.""" 900 """Set default password for encrypted files."""
901 assert isinstance(pwd, bytes) 901 if pwd and not isinstance(pwd, bytes):
902 self.pwd = pwd 902 raise TypeError("pwd: expected bytes, got %s" % type(pwd))
903 if pwd:
904 self.pwd = pwd
905 else:
906 self.pwd = None
903 907
904 def read(self, name, pwd=None): 908 def read(self, name, pwd=None):
905 """Return file bytes (as a string) for name.""" 909 """Return file bytes (as a string) for name."""
906 return self.open(name, "r", pwd).read() 910 return self.open(name, "r", pwd).read()
907 911
908 def open(self, name, mode="r", pwd=None): 912 def open(self, name, mode="r", pwd=None):
909 """Return file-like object for 'name'.""" 913 """Return file-like object for 'name'."""
910 if mode not in ("r", "U", "rU"): 914 if mode not in ("r", "U", "rU"):
911 raise RuntimeError('open() requires mode "r", "U", or "rU"') 915 raise RuntimeError('open() requires mode "r", "U", or "rU"')
916 if pwd and not isinstance(pwd, bytes):
917 raise TypeError("pwd: expected bytes, got %s" % type(pwd))
912 if not self.fp: 918 if not self.fp:
913 raise RuntimeError( 919 raise RuntimeError(
914 "Attempt to read ZIP archive that was already closed") 920 "Attempt to read ZIP archive that was already closed")
915 921
916 # Only open a new file for instances where we were not 922 # Only open a new file for instances where we were not
938 fheader = struct.unpack(structFileHeader, fheader) 944 fheader = struct.unpack(structFileHeader, fheader)
939 fname = zef_file.read(fheader[_FH_FILENAME_LENGTH]) 945 fname = zef_file.read(fheader[_FH_FILENAME_LENGTH])
940 if fheader[_FH_EXTRA_FIELD_LENGTH]: 946 if fheader[_FH_EXTRA_FIELD_LENGTH]:
941 zef_file.read(fheader[_FH_EXTRA_FIELD_LENGTH]) 947 zef_file.read(fheader[_FH_EXTRA_FIELD_LENGTH])
942 948
943 if fname != zinfo.orig_filename.encode("utf-8"): 949 if zinfo.flag_bits & 0x800:
950 # UTF-8 filename
951 fname_str = fname.decode("utf-8")
952 else:
953 fname_str = fname.decode("cp437")
954
955 if fname_str != zinfo.orig_filename:
944 raise BadZipfile( 956 raise BadZipfile(
945 'File name in directory %r and header %r differ.' 957 'File name in directory %r and header %r differ.'
946 % (zinfo.orig_filename, fname)) 958 % (zinfo.orig_filename, fname))
947 959
948 # check for encrypted flag & handle password 960 # check for encrypted flag & handle password
959 # The first 12 bytes in the cypher stream is an encryption header 971 # The first 12 bytes in the cypher stream is an encryption header
960 # used to strengthen the algorithm. The first 11 bytes are 972 # used to strengthen the algorithm. The first 11 bytes are
961 # completely random, while the 12th contains the MSB of the CRC, 973 # completely random, while the 12th contains the MSB of the CRC,
962 # or the MSB of the file time depending on the header type 974 # or the MSB of the file time depending on the header type
963 # and is used to check the correctness of the password. 975 # and is used to check the correctness of the password.
964 bytes = zef_file.read(12) 976 header = zef_file.read(12)
965 h = list(map(zd, bytes[0:12])) 977 h = list(map(zd, header[0:12]))
966 if zinfo.flag_bits & 0x8: 978 if zinfo.flag_bits & 0x8:
967 # compare against the file type from extended local headers 979 # compare against the file type from extended local headers
968 check_byte = (zinfo._raw_time >> 8) & 0xff 980 check_byte = (zinfo._raw_time >> 8) & 0xff
969 else: 981 else:
970 # compare against the CRC otherwise 982 # compare against the CRC otherwise