C# 如何在 7z.exe 中完全转义密码?
C# How one would completely escape password in 7z.exe?
出于各种原因,我使用 7z.exe,而不是包装器,解压缩如下所示:
var args = new StringBuilder();
args.AppendFormat("x \"{0}\"", source);
args.AppendFormat(" -o\"{0}\"", destination);
args.Append(" -y");
args.AppendFormat(" -p{0}", PassEscape(password)); // the password may contain special characters, etc
var code = ProcessHelper.Run(
new ProcessStartInfo
{
FileName = _zipPath,
Arguments = args.ToString()
},
token,
DefaultTimeout);
error = code != 0 ? ExitCodeTable.GetOrDefault(code, "Unknown 7z error") : null;
return code == 0;
我省略了一些琐碎的代码部分,例如 ProcessHelper,它只是启动进程并 运行 它完成。我用于测试的示例包含测试密码 !@#$%^&*()_+";
并且使用上面的代码总是说密码错误。
我完全不知道 PassEscape 函数,因为我找不到任何信息可以帮助我完全转义所有这些特殊字符(包括其他编码),但目前它非常简单:
private static string PassEscape(string input)
{
if (string.IsNullOrEmpty(input))
return input;
var b = new StringBuilder();
b.Append('"');
b.Append(input);
b.Append('"');
return b.ToString();
}
有什么帮助吗?
这对我有用:
string source = @"C:\Temp\New folder.7z";
string destination = @"C:\Temp\destination";
string password = "!@#$%^&*()_+\";";
Process sevenzip = Process.Start(
new ProcessStartInfo
{
FileName = @"C:\Program Files-Zipz.exe",
Arguments = $"x \"{source}\" -o\"{destination}\" -y -sccutf-8",
RedirectStandardInput = true,
UseShellExecute = false
});
sevenzip.StandardInput.WriteLine(password);
sevenzip.WaitForExit();
出于各种原因,我使用 7z.exe,而不是包装器,解压缩如下所示:
var args = new StringBuilder();
args.AppendFormat("x \"{0}\"", source);
args.AppendFormat(" -o\"{0}\"", destination);
args.Append(" -y");
args.AppendFormat(" -p{0}", PassEscape(password)); // the password may contain special characters, etc
var code = ProcessHelper.Run(
new ProcessStartInfo
{
FileName = _zipPath,
Arguments = args.ToString()
},
token,
DefaultTimeout);
error = code != 0 ? ExitCodeTable.GetOrDefault(code, "Unknown 7z error") : null;
return code == 0;
我省略了一些琐碎的代码部分,例如 ProcessHelper,它只是启动进程并 运行 它完成。我用于测试的示例包含测试密码 !@#$%^&*()_+";
并且使用上面的代码总是说密码错误。
我完全不知道 PassEscape 函数,因为我找不到任何信息可以帮助我完全转义所有这些特殊字符(包括其他编码),但目前它非常简单:
private static string PassEscape(string input)
{
if (string.IsNullOrEmpty(input))
return input;
var b = new StringBuilder();
b.Append('"');
b.Append(input);
b.Append('"');
return b.ToString();
}
有什么帮助吗?
这对我有用:
string source = @"C:\Temp\New folder.7z";
string destination = @"C:\Temp\destination";
string password = "!@#$%^&*()_+\";";
Process sevenzip = Process.Start(
new ProcessStartInfo
{
FileName = @"C:\Program Files-Zipz.exe",
Arguments = $"x \"{source}\" -o\"{destination}\" -y -sccutf-8",
RedirectStandardInput = true,
UseShellExecute = false
});
sevenzip.StandardInput.WriteLine(password);
sevenzip.WaitForExit();