python - 确定 zip 中有多少个文件

标签 python zip byte

我试图通过读取每个字节来读取 zip 文件(在 python 2.7.2 中)。我能够获取本地文件头和数据。但是,当我尝试读取中央文件头时,我陷入了困境。

这有很大帮助http://en.wikipedia.org/wiki/File:ZIP-64_Internal_Layout.svg

我不知道如何找出存档中有多少项目,以便我可以切换到格式化中央文件头,或者如何知道如何从格式化文件切换到中央文件头。

这就是我现在拥有的 -

import sys

def main(debug=0,arg_file=''):
    if debug==2:
        print "- Opening %s" % arg_file
    with open(arg_file) as archive: 
        if debug==2:
            print "- Reading %s" % arg_file

        bytes = archive.read()
        if debug==2:
            print "-------------Binary-------------"
            print bytes

        #Read file headers
        end = 0
        while end != bytes.__len__():
            print end
            end = process_sub_file(debug,end,bytes)

def process_sub_file(debug,startbytes, bytes): 
    header = bytes[startbytes + 0] + bytes[startbytes + 1] + bytes[startbytes + 2] + bytes[startbytes + 3]
    version = bytes[startbytes + 4] + bytes[startbytes + 5]
    flags = bytes[startbytes + 6] + bytes[startbytes + 7]
    comp_method = bytes[startbytes + 8] + bytes[startbytes + 9]
    mod_time = bytes[startbytes + 10] + bytes[startbytes + 11]
    mod_date = bytes[startbytes + 12] + bytes[startbytes + 13]
    crc = bytes[startbytes + 14] + bytes[startbytes + 15] + bytes[startbytes + 16] + bytes[startbytes + 17]
    comp_size_bytes = bytes[startbytes + 18] + bytes[startbytes + 19] + bytes[startbytes + 20] + bytes[startbytes + 21]
    comp_size = ord(comp_size_bytes[0]) + ord(comp_size_bytes[1]) + ord(comp_size_bytes[2]) + ord(comp_size_bytes[3])
    uncomp_size_bytes = bytes[startbytes + 22] + bytes[startbytes + 23] + bytes[startbytes + 24] + bytes[startbytes + 25]
    uncomp_size = ord(uncomp_size_bytes[0]) + ord(uncomp_size_bytes[1]) + ord(uncomp_size_bytes[2]) + ord(uncomp_size_bytes[3])
    name_len_bytes = bytes[startbytes + 26] + bytes[startbytes + 27]
    name_len = int(ord(name_len_bytes[0])+ord(name_len_bytes[1]))
    extra_len_bytes = bytes[startbytes + 28] + bytes[startbytes + 29]
    extra_len = int(ord(extra_len_bytes[0])+ord(extra_len_bytes[1]))
    file_name = ""
    for i in range(name_len):
        file_name = file_name + bytes[startbytes + 30 + i]
    extra_field = "" 
    for i in range(extra_len):
        file_name = file_name + bytes[startbytes + 30 + name_len + i]
    data = ""
    for i in range(comp_size):
        data = data + bytes[startbytes + 30 + name_len + extra_len + i]
    if debug>=1:
        print "-------------Header-------------"
        print "Header Signature: %s" % header
        print "Version: %s" % version
        print "Flags: %s" % flags
        print "Compression Method: %s" % comp_method
        print "Modification Time: %s" % (ord(mod_time[0]) + ord(mod_time[1]))
        print "Modification Date: %s" % (ord(mod_date[0]) + ord(mod_time[1]))
        print "CRC-32: %s" % crc
        print "Compressed Size: %s" % comp_size
        print "Uncompressed Size: %s" % uncomp_size
        print "File Name Length: %s" % name_len
        print "Extra Field Length: %s" % extra_len
        print "File Name: %s" % file_name
        print "Extra Field: %s" % extra_field
        print "Data:\n%s" % data
    return startbytes + 30 + name_len + extra_len + comp_size

最佳答案

您想要在文件中向后搜索“中央目录末尾” block 。它包含中央目录中的条目总数。

在以下位置搜索“中央目录记录结束:” http://www.pkware.com/documents/casestudies/APPNOTE.TXT

如果中央目录中的条目总数 = 0xffff,则您必须搜索位于“中央目录末尾” block 之前的“Zip64 中央目录末尾” block 。在这种情况下,Zip64 block 将包含 zip 文件中央目录中的实际条目数。

“EofCD” block 包含到中央目录开头的偏移量,您可以转到该目录,开始迭代整个中央目录中的所有文件头 block 。

关于python - 确定 zip 中有多少个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11621481/

相关文章:

javascript - 将 Google App Engine 数据存储模型传递给 javascript 代码

ant - 如何从 Ant 构建创建的 .zip 解析 7-zip 的 'Warnings: Headers Error'?

c - 4字节(32位)的IEEE 754算术

python - 需要帮助才能在 Python 中处理超过 2 个或更多字节的字符

python - Pandas 将索引复制到数据帧

python - 有效地找到多列的低中位数

python - Kivy 应用程序具有多个屏幕

Java move 具有特定文件扩展名的文件

c# - 用于拆分卷 zip 文件的 .Net 库?

c - 计算有符号整数的最大大小