Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

c# decompress a really large file

Posted on 2010-09-13
4
Medium Priority
?
4,370 Views
Last Modified: 2012-05-10
hi

i am using c# 3.5
i have a file that is just under 1GB.
 I am trying to unzip it (it has been Gzipped) and write the bytes to a new file.

I am getting an outofmemory exception when i try and do this.

here is a sample of my code (which works fine on small files):

using System.IO;
using System.IO.Compression;
using System.IO.Packaging;

//##############################################
//step 1
//first i read the bytes out the zipped file (this bit works fine)
//###############################################

byte[] zippedBytes;
using (var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
       using (var binaryReader = new BinaryReader(fileStream))
       {
          var numBytes = new FileInfo(fileName).Length;
          zippedBytes = binaryReader.ReadBytes((int)numBytes);
       }
}

//################################################################
//step 2
//then i try and decompress to a new memorystream
//and i get repeated "Exception of type 'System.OutOfMemoryException' was thrown."    
//in the following code...        
//################################################################

using (var gZipStream = new GZipStream(new MemoryStream(zippedBytes), CompressionMode.Decompress))
{
         const int size = 4096;
         var buffer = new byte[size];
         using (var memoryStream = new MemoryStream())
             {
               var count = 0;
                do
                {
                  count = gZipStream.Read(buffer, 0, size);
                  if (count > 0)
                    {
                      memoryStream.Write(buffer, 0, count);
                        }
                    } while (count > 0);

                    return memoryStream.ToArray();
               }
}


can anyone help please?!

thankyou for your time
0
Comment
Question by:MrKevorkian
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 2000 total points
ID: 33664004
MrKevorkian,
This is not a good approach, you are reading the entire file contents into memory, which overloads the memory. What will happen if you have a file of 4 GB? Also, consider that you are creating a decompressing buffer which is of the size of compressed file. It would be better to have a dynamic buffer.

The standard technique is to read in chunks and write to new file, like in the example shown in MSDN:
http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream(v=VS.90).aspx

If you are working with .net 4.0, then the solution is quiet straightforward with the new CopyTo method:


var ms = new MemoryStream(zippedBytes), 

using (GZipStream decompress = new GZipStream(new FileStream(fileName, FileMode.Open, FileAccess.Read), CompressionMode.Decompress))
                    {
decompress.CopyTo(ms);
return ms.ToArray();

// Notice you will have at least two buffers here: the 'ms' stream and the array generated before returning.
// To avoid this, you can decompress to a temporary filestream and read the contents into an array with ReadAllBytes

Open in new window

0
 
LVL 3

Expert Comment

by:vusov
ID: 33664056
Please try to use Ionic.Zip library. I've attached the GZipHelper sample of using.
Compress.zip
0
 
LVL 1

Author Comment

by:MrKevorkian
ID: 33669874
hi sorry for the delay!  im just looking at these two answers now. thanks
0
 
LVL 1

Author Closing Comment

by:MrKevorkian
ID: 33670760
hi i used the msdn example (well actually one of the comments at the bottom of the article)

heres my final code

 public void DecompressAndWriteToFile(FileDetails fileDetails)
        {
            const int bufferSize = 4096;
            var compressedfileInfo = new FileInfo(fileDetails.FullPath);

            using (var compressedFileStream = compressedfileInfo.OpenRead())
            {
                var newDecompressedPath = Path.Combine(directoryProvider.MyDropDirectory, fileDetails.UncompressedName);
                using (var decompressedFileSream = File.Create(newDecompressedPath))
                {
                    using (var gZipStream = new GZipStream(compressedFileStream, CompressionMode.Decompress))
                    {
                        var buffer = new byte[bufferSize];
                        int numRead;
                        while ((numRead = gZipStream.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            decompressedFileSream.Write(buffer, 0, numRead);
                        }
                    }
                }
            }
        }

it works great. thanks very much
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question