如何验证文件是受密码保护的ZIP文件,使用C# [英] How to validate that a file is a password protected ZIP file, using C#

查看:1124
本文介绍了如何验证文件是受密码保护的ZIP文件,使用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给一个文件的路径,如何可以验证该文件是受密码保护的zip文件?



也就是说,我将如何实现这个功能?

 布尔IsPasswordProtectedZipFile(字符串pathToFile)

我不需要解压文件 - 我只需要确认它是一个ZIP和被保护的一些密码



感谢

解决方案

使用的 nofollow的> SharpZipLib ,下面的代码工作。并通过作品我的意思是 entry.IsCrypted 根据是否有是在zip文件中的第一个条目的密码返回true或false。

  var文件= @C:\testfile.zip 
的FileStream fileStreamIn =新的FileStream(文件,FileMode.Open,FileAccess.Read);
ZipInputStream zipInStream =新ZipInputStream(fileStreamIn);
ZipEntry的入门= zipInStream.GetNextEntry();
Console.WriteLine(IsCrypted:+ entry.IsCrypted);



有上的 CodeProject上



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

 公共静态布尔IsPasswordProtectedZipFile(字符串路径)
{使用
(的FileStream fileStreamIn =新的FileStream(路径的FileMode。开放,FileAccess.Read))
使用(ZipInputStream zipInStream =新ZipInputStream(fileStreamIn))
{
ZipEntry的入门= zipInStream.GetNextEntry();
返回entry.IsCrypted;
}
}

请注意有没有真正的错误处理或什么...


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

解决方案

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

这篇关于如何验证文件是受密码保护的ZIP文件,使用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆