0

Trying to extract files over 1 GB using 7zip library. Code in c#. When trying to extract manually it works well, bit through c# code it takes longer or sometimes even hangs.

Below code I'm using

     string arg = $"x -y -mx=1 -mmt=off <zipfile> -o<Outputpath>";
     string sevenzip = @"<path_to_7zip>\7z.exe";
                    
     Process p = new Process()
     {
        StartInfo = new ProcessStartInfo()
        {
           FileName = sevenzip,
           Arguments = arg,
           UseShellExecute = false,
           RedirectStandardOutput = true,
           RedirectStandardError = true,
           WindowStyle = ProcessWindowStyle.Hidden,
           CreateNoWindow = true
        }
     };
 
     p.Start();
     string outputUnicode = null; 
     int exitCode=0;
     CancellationToken token = default(CancellationToken);
     GetProcessOutputWithTimeout(p, 7200000, token, out outputUnicode, out exitCode);

        public void GetProcessOutputWithTimeout(Process process, int timeoutSec, CancellationToken token, out string output, out int exitCode)
        {
            string outputLocal = ""; 
            int localExitCode = -1;
            var task = System.Threading.Tasks.Task.Factory.StartNew(() =>
            {
                outputLocal = process.StandardOutput.ReadToEnd();
                process.WaitForExit();
                localExitCode = process.ExitCode;
            }, token);

            if (task.Wait(timeoutSec, token))
            {
                output = outputLocal;
                exitCode = localExitCode;
            }
            else
            {
                exitCode = -1;
                output = "";
            }
            if (process.HasExited == false)
            {
                process.Kill();
            }
            process.Close();
        }

This code works well with smaller files but larger files takes more than 2 hours. I have kept timeout of 2 hours, because I don't want it to hang in between. Tried every possible way but no luck. Not sure what I'm doing wrong.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
spookky
  • 31
  • 3
  • I suggest you check memory, CPU, disk space metrics during both runs and see if there are any bottlenecks/peaking out. Exactly how much longer (in precent) does the C# code take and is it consistent over repeated tests? – Nick.Mc Jul 23 '23 at 23:48
  • [This](https://stackoverflow.com/questions/3774278/extracting-a-7-zip-file-silently-command-line-option?rq=2) is probably relevant. – 500 - Internal Server Error Jul 23 '23 at 23:59
  • 2
    If you're extracting a zip file in C#, wouldn't it be better to use https://learn.microsoft.com/en-us/dotnet/api/system.io.compression.zipfile? Why do you need to launch an external program? – Etienne de Martel Jul 24 '23 at 00:07
  • Does your system have enough memory? – Theodor Zoulias Jul 24 '23 at 00:19
  • If you aren't going to read from standard error, then you probably shouldn't redirect it. – Jeremy Lakeman Jul 24 '23 at 01:08
  • _"Trying to extract files over 1 GB using 7zip library"_ - that's not what you are doing. You are deflating using the 7zip _executable_ out-of-process. You may recheck usage of your cmdline args if they are optimal for your endevour. – Fildor Jul 24 '23 at 07:33

0 Answers0