
[英]how can i check if a compressed file(of all formats- zip/rar/tar/uue) is password protected, without extracting it in c#?
[英]How do i check if a file is password protected via 7zip?
我正在使用7zip(命令行)查看zip / rar / 7z文件。 我实质上检查了多少文件及其扩展名。 比...我要密码保护的文件。 当整个文件受密码保护(因此您无法查看文件名或其中的任何内容)时,我知道。 但是,如果我可以看到该文件,则无法确定它们是否受密码保护。 我压缩了两个文件,另一个没有密码。 7z l filename.zip显示两个zip中的文件相同
如何使用7zip检测档案中的文件是否受密码保护?
对于.7z归档文件-使用垃圾密码进行测试时,如果存在密码,则会设置非零错误级别。
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;
}
使用sevenzipsharp 。 它没有真正记录下来,但不难发现。
SevenZipExtractor.SetLibraryPath(@"path\7-Zip\7z.dll");
using (var extractor = new SevenZipExtractor(fn1))
{
if(extractor.Check()) { //is not password protected
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.