简体   繁体   中英

How do i check if a file is password protected via 7zip?

I am using 7zip (command line) to look at zip/rar/7z files. I essentially check how many files and what extension it has. Than... i got to password protected files. When the entire file is password protected (so you can not look at the filenames or anything within) i know. However if i can see the file i can NOT tell if they are password protected. I zipped two files one with the other without a password. 7z l filename.zip shows the files in both zip the same

How do i detect if a file is password protected in an archive using 7zip?

For a .7z archive -- when tested with a garbage password a non-zero errorlevel is set if a password exists.

7z t -pxoxoxoxoxoxoxo archive.7z >nul 2>nul
if errorlevel 1 echo Password exists
static bool IsPasswordProtected(string filename)
{
    string _7z = @"C:\Program Files\7-Zip\7z.exe";

    bool result = false;
    using (Process p = new Process())
    {
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.FileName = _7z;
        p.StartInfo.Arguments = $"l -slt \"{filename}\"";
        p.Start();
        string stdout = p.StandardOutput.ReadToEnd();
        string stderr = p.StandardError.ReadToEnd();
        p.WaitForExit();

        if (stdout.Contains("Encrypted = +"))
        {
            result = true;
        }                
    }

    return result;
}

Use sevenzipsharp . Its not really documented but its not hard to figure out.

SevenZipExtractor.SetLibraryPath(@"path\7-Zip\7z.dll");
using (var extractor = new SevenZipExtractor(fn1))
{
        if(extractor.Check()) { //is not password protected

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