简体   繁体   中英

how can i check if a compressed file(of all formats- zip/rar/tar/uue) is password protected, without extracting it in c#?

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.

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#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM