How to find the position of Central Directory in a Zip file?

前端 未结 3 1291
一向
一向 2021-02-20 13:02

I am trying to find the position of the first Central Directory file header in a Zip file.

I\'m reading these: http://en.wikipedia.org/wiki/Zip_(file_format) http://www.

相关标签:
3条回答
  • 2021-02-20 13:31

    I ended up looping through the bytes starting from the end. The loop stops if it finds a matching byte sequence, the index is below zero or if it already went through 64k bytes.

    0 讨论(0)
  • 2021-02-20 13:41

    Just cross your fingers and hope that there isn't an entry with the CRC, timestamp or datestamp as 06054B50, or any other sequence of four bytes that happen to be 06054B50.

    0 讨论(0)
  • 2021-02-20 13:53

    Start at the end and scan towards the beginning, looking for the end of directory signature and counting the number of bytes you have scanned. When you find a candidate, get the byte 20 offset for the comment length (L). Check if L + 20 matches your current count. Then check that the start of the central directory (pointed to by the byte 12 offset) has an appropriate signature.

    If you assumed the bits were pretty random when the signature check happened to be a wild guess (e.g. a guess landing into a data segment), the probability of getting all the signature bits correct is pretty low. You could refine this and figure out the chance of landing in a data segment and the chance of hitting a legitimate header (as a function of the number of such headers), but this is already sounded like a low likelihood to me. You could increase your confidence level by then checking the signature of the first file record listed, but be sure to handle the boundary case of an empty zip file.

    0 讨论(0)
提交回复
热议问题