Search code examples
c#.netsharpziplib

.Net : Unzipping Gz file : Insufficient memory to continue the execution of the program


So I have been using the method below to unzip .gz files and its been working really well.

It uses SharpZip.

Now I am using larger files and it seems to be trying to unzip everything in memory giving me : Insufficient memory to continue the execution of the program..

Should I be reading each line instead of using ReadToEnd()?

public static void DecompressGZip(String fileRoot, String destRoot)
        {
            using FileStream fileStram = new FileStream(fileRoot, FileMode.Open, FileAccess.Read);
            using GZipInputStream zipStream = new GZipInputStream(fileStram);
            using StreamReader sr = new StreamReader(zipStream);
            var data = sr.ReadToEnd();
            File.WriteAllText(destRoot, data);
        }

Solution

  • Like @alexei-levenkov suggests, CopyTo will chunk the copy without using up all the memory.

    Bonus points, use the Async version for the threading goodness.

    public static async Task DecompressGZipAsync(String fileRoot, String destRoot)
    {
          using (Stream zipFileStream = File.OpenRead(fileRoot))
          using (Stream outputFileStream = File.Create(destRoot))
          using (Stream zipStream = new GZipInputStream(zipFileStream))
          {
                await zipStream.CopyToAsync(outputFileStream);
          }
    }