如何使用python获取zip文件中所有文件(或给定文件名)的偏移值?

问题描述 投票:0回答:2

我真的不知道复杂性或前进的道路。如有任何帮助,我们将不胜感激。

python zip
2个回答
4
投票

ZipFile
类包含您需要的所有内容:

zf = ZipFile(...)

# for all files
for zinfo in zf.infolist():
    print(f'{zinfo.filename}: offset {zinfo.header_offset}')

# for specific file
zinfo = zf.getinfo(filename)
print(f'{zinfo.filename}: offset {zinfo.header_offset}')

0
投票

@bakatrouble 的答案结合 @f0k 的评论在大多数情况下都有效,但是对于具有对齐文件的 Android APK,需要以下方法来获取实际的文件偏移量:

import zipfile

with open(..., "rb") as f:
    zf = zipfile.ZipFile(f)

    for zinfo in zf.infolist():
        f.seek(zinfo.header_offset + 26)
        namelen = int.from_bytes(f.read(2), "little")
        extralen = int.from_bytes(f.read(2), "little")
        file_offset = zinfo.header_offset + 30 + namelen + extralen
        print(zinfo.filename, hex(file_offset))

(不幸的是python的zipfile模块似乎没有记录本地文件头的

extra
长度,所以我们需要手动提取)

© www.soinside.com 2019 - 2024. All rights reserved.