Changed decompress_sd0 to operate on bytes instead of files.

This commit is contained in:
lcdr 2015-09-26 16:29:40 +02:00
parent e2e7ff5183
commit 3e10476cd6

View File

@ -2,18 +2,16 @@ import argparse
import os.path import os.path
import zlib import zlib
def decompress(in_path, out_path): def decompress(data):
with open(in_path, "rb") as in_file:
data = in_file.read()
assert data[:5] == b"sd0\x01\xff" assert data[:5] == b"sd0\x01\xff"
pos = 5 pos = 5
with open(out_path, "wb") as out_file: out = b""
while pos < len(data): while pos < len(data):
length = int.from_bytes(data[pos:pos+4], "little") length = int.from_bytes(data[pos:pos+4], "little")
pos += 4 pos += 4
out_file.write(zlib.decompress(data[pos:pos+length])) out += zlib.decompress(data[pos:pos+length])
pos += length pos += length
return out
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
@ -24,6 +22,10 @@ if __name__ == "__main__":
filename, ext = os.path.splitext(os.path.basename(args.in_path)) filename, ext = os.path.splitext(os.path.basename(args.in_path))
args.out_path = filename+"_decompressed"+ext args.out_path = filename+"_decompressed"+ext
decompress(args.in_path, args.out_path) with open(args.in_path, "rb") as file:
data = in_file.read()
with open(args.out_path, "wb") as file:
file.write(decompress(data))
print("Decompressed file:", args.out_path) print("Decompressed file:", args.out_path)