0

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.

Divya
  • 416
  • 5
  • 10
  • you can also use SharpZipLib , it is written in C# for .NET platform and works with most compression formats. https://www.nuget.org/packages/SharpZipLib/ – Maverick Jul 22 '16 at 06:24

1 Answers1

1

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#

Community
  • 1
  • 1
sms247
  • 4,404
  • 5
  • 35
  • 45
  • this works with .zip extension. I want solution for .rar, .tar, .uue extensions as well. – Divya Jul 22 '16 at 09:32
  • 1
    just change all types or formats to zip and after checking again change into old extensions good luck – sms247 Jul 22 '16 at 09:43
  • these files are extreme crucial and I cant play with their extensions. :) – Divya Jul 22 '16 at 10:35
  • your file will not lost any data it will just change your file extension header nothing else so dont be afraid and try this trick good luck brother – sms247 Jul 29 '16 at 06:25