MTn
MTn

Reputation: 103

Can I minimize somehow the 7-zip output/log in C#?

I am using this code to compress folders:

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);

When the app is compiling I get this output:

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

But I only want this: 60% U Folder\thisisatext.txt Can I do this somehow? Thanks for any response.

Upvotes: 3

Views: 144

Answers (1)

Michael Hancock
Michael Hancock

Reputation: 2855

If you set your process’s standard output to redirect, like so:

p.RedirectStandardOutput = true;

(read more about this here)

You can then read 7 Zips output into a stream reader:

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

Now that your programs output is stored in a string you can get your 60% value back. If it is always the last line of the output you could use Linq to get it:

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

In this case, we are splitting the lines of the output into an array .Split('\n') then selecting the last line .Last(). We then remove any whitespace that might be before or after the string using .Trim().

Upvotes: 3

Related Questions