给定文件路径,如何验证该文件是否是受密码保护的 zip 文件?
即,我将如何实现这个功能?
bool IsPasswordProtectedZipFile(string pathToFile)
我不需要解压缩该文件——我只需要验证它是否是 ZIP 且已受密码保护。
Thanks
Using SharpZipLib http://www.icsharpcode.net/OpenSource/SharpZipLib/,以下代码有效。我所说的作品是指entry.IsCrypted
根据 zip 文件中第一个条目是否有密码返回 true 或 false。
var file = @"c:\testfile.zip";
FileStream fileStreamIn = new FileStream(file, FileMode.Open, FileAccess.Read);
ZipInputStream zipInStream = new ZipInputStream(fileStreamIn);
ZipEntry entry = zipInStream.GetNextEntry();
Console.WriteLine("IsCrypted: " + entry.IsCrypted);
有一个关于使用 SharpZipLib 的简单教程代码项目 http://www.codeproject.com/KB/cs/Zip_UnZip.aspx.
因此,一个简单的实现看起来像这样:
public static bool IsPasswordProtectedZipFile(string path)
{
using (FileStream fileStreamIn = new FileStream(path, FileMode.Open, FileAccess.Read))
using (ZipInputStream zipInStream = new ZipInputStream(fileStreamIn))
{
ZipEntry entry = zipInStream.GetNextEntry();
return entry.IsCrypted;
}
}
请注意,没有真正的错误处理或任何东西......
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)