Burf2000
Burf2000

Reputation: 5193

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

Upvotes: 0

Views: 514

Answers (1)

Neil Bostrom
Neil Bostrom

Reputation: 2349

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

Upvotes: 2

Related Questions