我想将多个文件合并到一个存档中。我使用python 2.7来做到这一点。这是我的代码:
从每个文件读取二进制数据的代码(有些是使用utf8的txt,有些是png或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)
这没关系,所有二进制数据都存档到一个文件中。现在我想提取档案,我这样做:
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)
每个文件提取成功,但每个文件提取中的所有数据都是二进制数据,我希望它们是以前的。
我尝试过编码和解码,但没有帮助〜
谁能帮帮我?非常感谢!!!