Reputation: 436
I am developing a small utility in c#, where I am inserting many compressed files. These files are in different extensions(such as .zip, .rar, .tar, .uue) Now I do not want to extract these files, but I just want to check if these files are password protected or not.
I have used DotNetZip.dll for the files with .zip extension, which is working fine. I found Chilkat dll for .rar files.
Can anyone provide me other dlls for other extensions or a better solution for all compressed files? Thank you in advance.
Upvotes: 0
Views: 1147
Reputation: 4504
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...
ref:How to validate that a file is a password protected ZIP file, using C#
Upvotes: 1