c# - 我可以以某种方式最小化 C# 中的 7-zip 输出/日志吗?

标签 c# compression output 7zip

我正在使用此代码来压缩文件夹:

ProcessStartInfo p = new ProcessStartInfo();
                        p.FileName = @"C:\Program Files\7-Zip\7z.exe";
                        p.Arguments = "a -t7z \"" + targetName + "\" \"" + item.ToString() + "\" -mx=9";
                        p.WindowStyle = ProcessWindowStyle.Minimized;
                        Process x = Process.Start(p);
                        x.WaitForExit();
                        Directory.Delete(dirPath + "\\" + item.Name, true);

当应用程序编译时,我得到以下输出:

7-Zip 19.00 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2019-02-21

Open archive: C:\a\b\folders\compress.7z
--
Path = C:\a\b\folders\compress.7z
Type = 7z
Physical Size = 881619
Headers Size = 273
Method = LZMA2:23
Solid = +
Blocks = 1

    Scanning the drive:
    1 folder, 2 files, 8258668 bytes (8066 KiB)

    Updating archive: C:\a\b\folders\compress.7z

    Add new data to archive: 1 folder, 2 files, 8258668 bytes (8066 KiB)

     60% U Folder\thisisatext.txt

但我只想要这个:60% U文件夹\thisisatext.txt 我可以以某种方式做到这一点吗?感谢您的回复。

最佳答案

如果您将进程的标准输出设置为重定向,如下所示:

p.RedirectStandardOutput = true;

( read more about this here )

然后您可以将 7 Zips 输出读取到流读取器中:

var reader = x.StandardOutput;
var output = reader.ReadToEnd();

现在您的程序输出已存储在字符串中,您可以取回 60% 的值。如果它始终是输出的最后一行,您可以使用 Linq获取它:

var lastLine = output.Split('\n').Last().Trim();
Console.WriteLine(lastLine); // 60% U Folder\\thisisatext.txt"

在本例中,我们将输出行拆分为数组 .Split('\n'),然后选择最后一行 .Last()。然后,我们使用 .Trim() 删除字符串之前或之后的任何空格。

关于c# - 我可以以某种方式最小化 C# 中的 7-zip 输出/日志吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59983445/

相关文章:

c# - JwtSecurityToken 过期时间无效 .NET Core 3.1

c# - 计算 TcpClient 通过 NetworkStream BinaryReader/BinaryWriter 传输的字节数

c# - 如何将数据作为序列化对象插入数据库

c - 如何修改 zlib MIN_MATCH 值?

python - Tensorflow LSTM-Cell 的输出

c# - 无法在 .Net Standard 2.1 项目中添加 EF 6.4 迁移

compression - DXT 压缩产生与原始 PNG 相同的文件大小

java - 使用 apache common compress/org.tukaani.xz 在 java 中解码 LZMA 压缩 zip 文件的问题

date - 显示值的日期 - 时间序列数据

java - C 程序在运行时执行 shell 相对于 JAVA 中的输入和输出