foo*_*ion 2 python backup android zlib tar
我想使用 python 解压 Android 备份文件。
根据http://nelenkov.blogspot.com/2012/06/unpacking-android-backups.html可以使用解压缩未加密的 adb 文件
dd if=mybackup.ab bs=24 skip=1|openssl zlib -d > mybackup.tar
Run Code Online (Sandbox Code Playgroud)
和
tar xvf mybackup.tar
Run Code Online (Sandbox Code Playgroud)
这些可以在python中完成吗?Python 有zlib,gzip和tarfile,它们似乎应该可用。无论如何,如果他们可以做到,该怎么做?
将tarfile.open('filename.tar', 'r:')第二步工作?
我在窗户上,顺便说一句。
如果文件不是太大以至于无法在内存中轻松容纳所有内容,则在从标准库导入所需的内容之后:
with open('mybackup.ab', 'rb') as f:
f.seek(24) # skip 24 bytes
data = f.read() # read the rest
tarstream = zlib.decompress(data)
tf = tarfile.open(fileobj=io.BytesIO(tarstream))
Run Code Online (Sandbox Code Playgroud)
现在,tf您有一个https://docs.python.org/2/library/tarfile.html#tarfile-objects 中TarFile记录的实例,因此您可以例如获取其内容列表、提取一个或多个成员等。
如果备份太大而无法将所有这些位轻松放入内存中,您当然可以将任何或所有中间结果写入磁盘;但是如果它足够小,将所有内容都保存在内存中应该会导致执行速度更快。