如何使用 C# 验证文件是否是受密码保护的 ZIP 文件

发布于 2024-08-09 04:13:01 字数 192 浏览 2 评论 0原文

给定文件路径,如何验证该文件是否是受密码保护的 zip 文件?

即,我将如何实现这个功能?

bool IsPasswordProtectedZipFile(string pathToFile)

我不需要解压缩该文件——我只需要验证它是否是 ZIP 且已受密码保护。

谢谢

Given a path to a file, how can I validate that the file is a password-protected zip file?

i.e., 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.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情场扛把子 2024-08-16 04:13:01

使用 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);

CodeProject 上有一个关于使用 SharpZipLib 的简单教程。

因此,一个简单的实现看起来像这样:

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;
    }
}

注意,没有真正的错误处理或任何东西......

Using SharpZipLib, the following code works. 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.

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.

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

情何以堪。 2024-08-16 04:13:01

在 ZIP 存档中,密码不是放在文件上,而是放在文件中的各个条目上。 zip 可能包含一些加密的条目,一些则未加密。下面是一些用于检查 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); 

如果您愿意,可以使用 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;
    }
}

In ZIP archives, the password is not placed on the file, but on the individual entries within the file. A zip can contain some entries encrypted and some not. Here's some example code to check for encryption on entries in 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:

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;
    }
}
清风不识月 2024-08-16 04:13:01

此时,在 .NET Framework 成熟度中,您将需要使用第 3 方工具。有许多商业图书馆可以通过谷歌搜索。我建议从 Microsoft 的 Codeplex 网站 DotNetZip 获取一个免费的。首页声明“该库支持 zip 密码”。

At this point in the .NET Framework maturity you will need to use a 3rd party tool. There are many commercial libraries that can be Googled. I'm suggesting one free one from Microsoft's Codeplex website DotNetZip. The front page states "the library supports zip passwords".

无语# 2024-08-16 04:13:01

没有 100% 正确的方法来检查所有 zip 条目是否都已加密。
zip 文件中的每个条目都是独立的,并且可以有自己的密码/加密方法。

大多数情况下,zip文件是由某些软件压缩的,这些软件将确保zip文件中的每个条目都有一个共同的密码和加密方法。

因此,使用第一个 zipentry(不是目录)来检查该 zip 文件是否已加密可以覆盖大多数情况。

There is no 100% correct way to check if all zip entries is encrypted.
every entry in a zipfile is independent and could has its own password/encrypted method.

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.

So, using the first zipentry(not a directory) to check if that zipfile is encrypted can cover most cases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文