如何找到一个Zip文件中央目录的位置? [英] How to find the position of Central Directory in a Zip file?

查看:258
本文介绍了如何找到一个Zip文件中央目录的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在阅读这些内容:
http://en.wikipedia.org/wiki/Zip_(file_format
http://www.pkware.com/documents/casestudies/APPNOTE.TXT



正如我所看到的,我只能通过Zip数据扫描,通过标题标识我在哪个部分,然后做到这一点,直到我打中央目录标题,我显然会阅读文件标题之前并使用压缩大小来跳过实际的数据,而不是for-loop通过文件中的每个字节...

如果我这样做,那么我实际上已经知道Zip文件中的所有文件和文件夹,在这种情况下,我再也看不到中央目录的多少用处了。



中央目录是列出文件元数据,以及在Zip文件中的实际数据的位置,所以你不需要扫描整个文件?



在阅读完中央目录记录后,维基百科说:


这个顺序允许一次创建一个zip文件,但是它通常是通过首先读取中央目录在
结束。

如何轻松找到中央目录结尾?我们需要记住,它可以有一个任意大小的评论,所以我可能不知道从它所在的数据流的末尾有多少个字节。我只是扫描它?



我写了一个Zip文件阅读器。

解决方案

我从最后开始循环遍历字节。如果循环找到匹配的字节序列,则索引在零以下,或者已经经过了64k字节。循环会停止。


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.pkware.com/documents/casestudies/APPNOTE.TXT

As I see it, I can only scan through the Zip data, identify by the header what kind of section I am at, and then do that until I hit the Central Directory header. I would obviously read the File Headers before that and use the "compressed size" to skip the actual data, and not for-loop through every byte in the file...

If I do it like that, then I practically already know all the files and folders inside the Zip file in which case I don't see much use for the Central Directory anymore.

To my understanding the purpose of Central Directory is to list file metadata, and the position of the actual data in the Zip file so you wouldn't need to scan the whole file?

After reading about End Of Central Directory record, Wikipedia says:

This ordering allows a zip file to be created in one pass, but it is usually decompressed by first reading the central directory at the end.

How would I find End of Central Directory record easily? We need to remember that it can have an arbitrary sized comment there, so I may not know how many bytes from the end of the data stream it is located at. Do I just scan it?

P.S. I'm writing a Zip file reader.

解决方案

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.

这篇关于如何找到一个Zip文件中央目录的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆