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

3

6

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?

Volva answered 10/1, 2012 at 4:46 Comment(3)
Bumping to superusers, because this is explicit about the use of a 7zip approach. (Don't seem very programming related.)Brinkman
I added the c# tag because your answer indicates that you actually wanted to do this programmatically, in C#.Foliole
@GregHewgill. Yeah. I wouldnt mind doing it in C++ or maybe python i just happen to use C#/.NET first. Originally i tried using the stdout but it appeared i cant get that data from stdout/CLIVolva
V
2

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
Volva answered 11/1, 2012 at 5:27 Comment(2)
This is not a good test. If archive corrupted, in that case also it will show this error. Also, if we select a non-first volume in a multivolume archive, in that case too this will be thrown.Telephone
@ShashankJain: I don't have any other answers so I marked this. Also this may give me a false positive but for my needs I need no false negatives which this offersVolva
F
7

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
Fulmination answered 17/5, 2017 at 22:25 Comment(1)
Couldn't there be another reason for a non-zero errorlevel though? This doesn't seem like it would tell you for sure that a password exists.Diametral
V
2

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
Volva answered 11/1, 2012 at 5:27 Comment(2)
This is not a good test. If archive corrupted, in that case also it will show this error. Also, if we select a non-first volume in a multivolume archive, in that case too this will be thrown.Telephone
@ShashankJain: I don't have any other answers so I marked this. Also this may give me a false positive but for my needs I need no false negatives which this offersVolva
W
2
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;
}
Worrell answered 23/4, 2019 at 13:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.