Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Geek Culture / assistance with coding zip format

Author
Message
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 28th Jan 2013 23:53
As the title suggest, it's my new DB project. If the archive has only a single file then I'm ok and am able to extract it. But multiple files I'm having a little trouble. The central directory, which keeps a list of files, is designed to be at the end of the file and to allow tools to retrieve a list of filenames without having to scan the entire file first. However, the header information contains fields with N number of bytes, so I'm not sure exactly how to find this directory entry without knowing exactly how many bytes from the end it starts.

I'm hoping maybe someone here can help me find what I'm missing.

http://en.wikipedia.org/wiki/Zip_(file_format)

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Jeff032
18
Years of Service
User Offline
Joined: 13th Aug 2007
Location:
Posted: 29th Jan 2013 00:31
It looks like one approach is to scan backwards from the end looking for the "End of central directory signature" (0x06054b50). From there you can get the offset to the start of the central directory.
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 29th Jan 2013 00:52
That's what I just started trying to do. I'll have to make the file into a memblock so I have random access. The downside to scanning backwards is the comment field can potentially have 64kb of data in it. Unlikely, but a possibility.

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 29th Jan 2013 01:34 Edited at: 29th Jan 2013 01:39
Well, that's not going to work so well afterall. The central directory file header which starts with 0x02014b50 uses the same signature for each file in the directory. So if I have 3 files in the zip archive, moving backwards from the end I'll hit only the last entry in the directory. Meaning I'd basically have to read the entire file backwards all the way to the beginning and keep track of the last position I found that signature. This is such a retarded format!

It's not that it would be hard to do, I already wrote the loop. But it's taking a few seconds to scan a 150kb archive, and that's a relatively small file. There's got to be a better way.

I'm thinking that since no file in the archive can come after directory starts, that every time I find a directory signature I can read the file offset value for that particular entry. Keeping track of the highest value will allow me to reduce how far back I'd have to read.

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Jeff032
18
Years of Service
User Offline
Joined: 13th Aug 2007
Location:
Posted: 29th Jan 2013 14:04
Don't look for the central directory file headers, look for the end of central directory record. The signature for that is 0x06054b50. The end of central directory record contains the size of the central directory, and the offset to the start of the central directory relative to the start of the archive.
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 29th Jan 2013 18:15
Quote: "end of central directory record contains the size of the central directory"


ahhh, how did I miss that!

"You're not going crazy. You're going sane in a crazy world!" ~Tick

Login to post a reply

Server time is: 2026-07-16 02:35:32
Your offset time is: 2026-07-16 02:35:32