0

我正在用 c# 开发一个小实用程序,我在其中插入许多压缩文件。这些文件具有不同的扩展名(例如 .zip、.rar、.tar、.uue) 现在我不想提取这些文件,但我只想检查这些文件是否受密码保护。

我已将 DotNetZip.dll 用于扩展名为 .zip 的文件,效果很好。我为 .rar 文件找到了 Chilkat dll。

谁能为我提供其他扩展的其他 dll 或所有压缩文件的更好解决方案?先感谢您。

4

1 回答 1

1

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

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

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

于 2016-07-22T07:01:03.187 回答