如何通过7zip检查文件是否受密码保护?

问题描述 投票:0回答:3

我使用7zip(命令行)来查看zip / rar / 7z文件。我基本上检查了它有多少文件和扩展名。比...我得到密码保护的文件。当整个文件受密码保护时(所以你不能查看文件名或其中的任何内容)我知道。但是,如果我能看到该文件,我无法判断它们是否受密码保护。我用另一个没有密码将两个文件压缩成一个。 7z l filename.zip显示两个文件中的文件相同

如何使用7zip检测存档中的文件是否受密码保护?

c# command-line 7zip
3个回答
1
投票

对于.7z存档 - 使用垃圾密码测试时,如果存在密码,则设置非零错误级别。

7z t -pxoxoxoxoxoxoxo archive.7z >nul 2>nul
if errorlevel 1 echo Password exists

0
投票

使用sevenzipsharp。它没有真正记录,但不难理解。

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

0
投票
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;
}
© www.soinside.com 2019 - 2024. All rights reserved.