micropython 实现tar 文件解包
import upip_utarfile as tar import os def run(src,dest_dir): t = tar.TarFile(src) if not dest_dir in os.listdir(): os.mkdir(dest_dir) dest_dir=dest_dir+"/" prefix=dest_dir for i in t: i.name=i.name.rstrip('/') print(i,i.name) i.name=prefix+i.name print(i.name) if i.type == tar.DIRTYPE: try: os.mkdir(i.name) except OSError as e: if e.args[0] != errno.EEXIST and e.args[0] != errno.EISDIR: raise e else: f = t.extractfile(i) print(i.name) copyfileobj(f, open(i.name, "wb+")) def rmtree(top): for path, dirs, files in os.walk(top, False): for f in files: os.unlink(path + "/" + f) os.rmdir(path) def copyfileobj(src, dest, length=512): if hasattr(src, "readinto"): buf = bytearray(length) while True: sz = src.readinto(buf) if not sz: break if sz == length: dest.write(buf) else: b = memoryview(buf)[:sz] dest.write(b) else: while True: buf = src.read(length) if not buf: break dest.write(buf) run("/2.tar","/tt")
run("/2.tar","/tt") 第一参数为解包文件,第二个参数为解包目录