I have many large gzip files (approximately 10 MB - 200 MB) that I downloaded from ftp for unpacking.
So, I tried to find Google and find a solution for gzip decompression.
static byte[] Decompress(byte[] gzip) { using (GZipStream stream = new GZipStream(new MemoryStream(gzip), CompressionMode.Decompress)) { const int size = 4096; byte[] buffer = new byte[size]; using (MemoryStream memory = new MemoryStream()) { int count = 0; do { count = stream.Read(buffer, 0, size); if (count > 0) { memory.Write(buffer, 0, count); } } while (count > 0); return memory.ToArray(); } } }
It works well for any files below 50 MB, but as soon as I have input of more than 50 MB, I got an exception from memory. The last position and memory length before the exception is 134217728. I do not think that it is related to my physical memory, I understand that I cannot have an object larger than 2 GB, since I use the 32-bit version.
I also need to process the data after unpacking the files. I am not sure if it is best to use a memory stream here, but I do not like to write to a file and then read the files again.
My questions
- Why did I get a System.OutMemoryException?
- What is the best possible solution for unzipping gzip files and subsequent text processing?
source share