# SPDX-License-Identifier: MIT """Minimal read-only asar extractor: copies selected files out of an asar archive. usage: python -I asar_extract.py [prefix ...] Only entries whose path starts with one of the prefixes are written. """ import json import os import struct import sys def walk(node, path=""): for name, child in node.get("files", {}).items(): p = f"{path}/{name}" if path else name if "files" in child: yield from walk(child, p) else: yield p, child def main(): asar, out, *prefixes = sys.argv[1:] with open(asar, "rb") as f: _, header_size, _, json_len = struct.unpack("<4I", f.read(16)) header = json.loads(f.read(json_len)) base = 8 + header_size n = 0 for p, e in walk(header): if prefixes and not any(p.startswith(x) for x in prefixes): continue if e.get("unpacked") or "offset" not in e: continue dest = os.path.normpath(os.path.join(out, p)) if not dest.startswith(os.path.normpath(out) + os.sep): continue # path traversal guard os.makedirs(os.path.dirname(dest), exist_ok=True) f.seek(base + int(e["offset"])) with open(dest, "wb") as o: o.write(f.read(e["size"])) n += 1 print(f"{n} files extracted") main()