I want to merge multiple files to one archive. and I used python 2.7 to do this. Here is my code:
the code to read binary data from every file(some is txt with utf8 and some is png or jpeg):
for we in self.entries:
with open(we['fullname'], 'rb') as f:
data = f.read()
if not data:
print("mpq flush error. the file no data or error:"+we['fullname'])
break
self.file.write(data)
this is ok, and all the binary data is archived into one file. and now I want to extract the archive, I do this like :
def read_file(self, filename):
if self._writable():
print("curr mode is write, cannot read")
return None
entry = self._get_entry(filename)
if not entry:
print("the file is not in curr archive:"+filename)
return None
self.file.seek(entry.file_offset)
data = self.file.read(entry.file_size)
if not data:
print("file read error:"+filename)
return None
return data
def extract_file(self, filename):
print("extract_file:"+filename)
data = self.read_file(filename)
if not data:
return
targetFile = self._fullname(filename)
self._makepath(targetFile)
with open(targetFile, 'wb') as f:
f.write(data or b'')
def extract_all(self):
for entry in self.entries:
self.extract_file(entry.file_name)
every file extract success, but all data in every file extract is binary data, I want them to be what they are before.
I tried encode and decode, but not help~
anyone can help me ? thanks very much!!!