[英]How to validate that a file is a password protected ZIP file, using C#
Given a path to a file, how can I validate that the file is a password-protected zip file? 给定文件的路径,如何验证该文件是受密码保护的zip文件?
ie, how would I implement this function? 即,我该如何实现这个功能?
bool IsPasswordProtectedZipFile(string pathToFile)
I don't need to unzip the file -- I just need to verify that it's a ZIP and has been protected with some password. 我不需要解压缩文件 - 我只需要验证它是ZIP并且已经受到一些密码的保护。
Thanks 谢谢
Using SharpZipLib , the following code works. 使用SharpZipLib ,以下代码可以正常工作。 And by works I mean
entry.IsCrypted
returns true or false based on whether or not there is a password for the first entry in the zip file. 并且通过工作我的意思是
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);
There's a simple tutorial on using SharpZipLib on CodeProject . 有一个关于在CodeProject上使用SharpZipLib的简单教程。
Thus a simple implementation looks something like: 因此,一个简单的实现看起来像:
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;
}
}
Note there's no real error handling or anything... 请注意,没有真正的错误处理或任何事情......
In ZIP archives, the password is not placed on the file, but on the individual entries within the file. 在ZIP存档中,密码不会放在文件中,而是放在文件中的各个条目上。 A zip can contain some entries encrypted and some not.
zip可以包含一些加密的条目,一些不加密。 Here's some example code to check for encryption on entries in DotNetZip:
以下是检查DotNetZip中条目加密的一些示例代码:
int encryptedEntries = 0;
using (var zip = ZipFile.Read(nameOfZipFile))
{
// check a specific, named entry:
if (zip["nameOfEntry.doc"].UsesEncryption)
Console.WriteLine("Entry 'nameOfEntry.doc' uses encryption");
// check all entries:
foreach (var e in zip)
{
if (e.UsesEncryption)
{
Console.WriteLine("Entry {0} uses encryption", e.FileName);
encryptedEntries++;
}
}
}
if (encryptedEntries > 0)
Console.WriteLine("That zip file uses encryption on {0} entrie(s)", encryptedEntries);
If you'd prefer, you can use LINQ: 如果您愿意,可以使用LINQ:
private bool ZipUsesEncryption(string archiveToRead)
{
using (var zip = ZipFile.Read(archiveToRead))
{
var selection = from e in zip.Entries
where e.UsesEncryption
select e;
return selection.Count > 0;
}
}
At this point in the .NET Framework maturity you will need to use a 3rd party tool. 在.NET Framework成熟度的这一点上,您将需要使用第三方工具。 There are many commercial libraries that can be Googled.
有很多商业图书馆可以用Google搜索。 I'm suggesting one free one from Microsoft's Codeplex website DotNetZip .
我建议微软的Codeplex网站DotNetZip免费提供一个。 The front page states " the library supports zip passwords ".
首页说明“ 库支持zip密码 ”。
There is no 100% correct way to check if all zip entries is encrypted. 没有100%正确的方法来检查所有zip条目是否已加密。 every entry in a zipfile is independent and could has its own password/encrypted method.
zipfile中的每个条目都是独立的,可以有自己的密码/加密方法。
for most cases, zipfile is zipped by some software, these software will ensure every entry in a zipfile has a common password and encrypted method. 对于大多数情况,zipfile是由某些软件压缩的,这些软件将确保zipfile中的每个条目都有一个通用密码和加密方法。
So, using the first zipentry(not a directory) to check if that zipfile is encrypted can cover most cases. 因此,使用第一个zipentry(不是目录)来检查该zipfile是否已加密可以涵盖大多数情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.