Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8280707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:46:38+00:00 2026-06-08T09:46:38+00:00

I am trying to read a zip file (in python 2.7.2) by reading each

  • 0

I am trying to read a zip file (in python 2.7.2) by reading each of the bytes. I am able to get through the local file headers and the data. However I am stuck when trying to read the Central file header.

This helps alot http://en.wikipedia.org/wiki/File:ZIP-64_Internal_Layout.svg

I dont know how to find out how many items there are in the archive so I can switch to formating the central file header or how else to know how to switch from formating file to the central file header.

This is what I have right now –

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T09:46:40+00:00Added an answer on June 8, 2026 at 9:46 am

    You want to search through the file backwards for the “End of Central Directory” block. It contains the total number of entries in the central directory.

    Search for “End of central directory record:” in:
    http://www.pkware.com/documents/casestudies/APPNOTE.TXT

    If the total number of entries in the central directory = 0xffff, then you have to search for the “Zip64 End of Central Directory” block which is located directly before the “End of Central Directory” block. And in that case the Zip64 block would contain the actual number of entries in the central directory for the zip file.

    The “EofCD” block contains the offset to the start of the central directory which you can then go to, to begin iterating through all the file header blocks in the entire central directory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looping through a zip file trying to add the file name of each
Trying to read headers for a csv file with: reader = csv.DictReader(open(PATH_FILE),skipinitialspace=True) headers =
I am trying to read from a file and encrypt the data using AES
I've uploaded a ZIP file containing both the XML file I'm trying to read
I am trying to use DotNetZip 1.9 to read an uploaded zip file in
I am trying to read a bzip2-compressed CSV file in Python 3.2. For an
I am trying to read in data from a text file using numpy.loadtxt with
I'm trying to create a Zip file from .Net that can be read from
I am trying to convert an array of bytes into a ZIP file. I
I am trying to read a single file from a java.util.zip.ZipInputStream , and copy

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.