Reputation:
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?
Upvotes: 6
Views: 8978
Reputation: 7397
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;
}
Upvotes: 2
Reputation: 735
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
Upvotes: 7
Reputation:
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
Upvotes: 2