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

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

问题描述

我正在尝试查找 Zip 文件中第一个中央目录文件头的位置.

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

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

I'm reading these: http://en.wikipedia.org/wiki/Zip_(file_format) http://www.pkware.com/documents/casestudies/APPNOTE.TXT

在我看来,我只能扫描 Zip 数据,通过标题识别我所在的部分,然后这样做,直到我点击中央目录标题.我显然会在那之前阅读文件头并使用压缩大小"来跳过实际数据,而不是循环遍历文件中的每个字节......

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...

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

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.

据我了解,中央目录的目的是列出文件元数据,以及实际数据在 Zip 文件中的位置,这样您就不需要扫描整个文件?

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:

这种顺序允许一次创建一个 zip 文件,但它是通常通过首先读取中央目录来解压缩结束.

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.

如何轻松找到 End of Central Directory 记录?我们需要记住,它可以有任意大小的注释,所以我可能不知道它位于数据流末尾的多少字节.我只是扫描它吗?

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?

附:我正在写一个 Zip 文件阅读器.

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

推荐答案

我最终从末尾开始循环遍历字节.如果找到匹配的字节序列、索引低于零或已经经过 64k 字节,则循环停止.

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天全站免登陆