使用 7zip 检测文件是否为存档
我想使用 SevenZipSharp 来确定文件是否是存档。我知道这是可能的,因为在资源管理器中,如果我将 .zip 重命名为 .bmp,7zip 仍然会将其识别为存档。
--edit:换句话说,我希望 7zip 告诉我文件(无论扩展名如何)是否包含某种受支持的存档(zip、tar、rar、iso 等),
谢谢, 菲德尔
I would like to use SevenZipSharp in order to determine if a file is an archive. I know that it's possible because in explorer if I rename a .zip to .bmp, 7zip still recognises it as an archive.
--edit: In other words, I want 7zip to tell me if a file (no matter the extension) contains some kind of supported archive (zip, tar, rar, iso etc.)
Thanks,
Fidel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
确定文件是否为存档的方法是实际尝试将其输入 SevenZipSharp 库,然后查看它是否成功或失败。然而,这将是一个非常缓慢的过程,就像您的示例一样,您有一堆标有扩展名 .bmp 的 .zip 文件。
The way you would determine if the file is an archive, is to actually try to feed it in to the SevenZipSharp library, and see if it succeeds or fails. However this is going to be a really slow process like your example you have a bunch of .zip files marked with the extension .bmp.
您不需要使用 Sevenzip 来仅了解该文件是否是存档,
检查各种文件的魔术字节就足够了。
例如:
Zip 的初始 2 字节为 50 4B (PK)
RAR 的初始为 3 字节 52 61 72 (Rar!)
You don't need to use sevenzip to only know whether the file is an archive or not,
It is suffice to check for the magic byte for various files.
For example:
Zip has initial 2 bytes 50 4B (PK)
RAR has initial 3 bytes 52 61 72 (Rar!)
SharpCompress 也可以轻松做到这一点。
SharpCompress does this easily as well.
我没有使用过该库,而且事实上没有 没有文档 并没有帮助,但通常会尝试一下打开存档,如果出现任何错误,则可能意味着该文件不是存档(可能存在特定错误)。
I haven't used that library and the fact that there's no documentation doesn't help, but typically one tries to open the archive and if any error comes out, it might mean the file is not an archive (there's probably a specific error for that).
我不熟悉 SevenZipSharp,但 ZIP 是一种有据可查的文件格式,例如: ZIP文件格式
请注意文件和条目开头的幻数。您不需要任何特殊的 API/库来检测 zip 文件,只需将其作为普通文件读取并检查它是否符合格式即可。如果您不想解析整个文件,您可能会偷懒,只需检查文件签名是否是您要查找的文件签名(或其中之一):文件签名列表
I'm not familiar with SevenZipSharp, but ZIP is a well documented file format, for example: ZIP File Format
Note the magic numbers at the start of the file and entries. You don't need any special API/library to detect a zip file, just read it as a normal file and check if it conforms to the format. If you don't feel like parsing the whole file, you could be lazy and just check the file signature is the one (or one of the ones) you're looking for: List of file signatures
7z.exe
可用于确定文件是否为存档:7z.exe
can be used to determine if a file is an archive: