Pinte Dani
Pinte Dani

Reputation: 2049

Adding large files to IO.Compression.ZipArchiveEntry throws OutOfMemoryException Exception

I am trying to add a large video file(~500MB) to an ArchiveEntry by using this code:

using (var zipFile = ZipFile.Open(outputZipFile, ZipArchiveMode.Update))
{
    var zipEntry = zipFile.CreateEntry("largeVideoFile.avi");
    using (var writer = new BinaryWriter(zipEntry.Open()))
    {
        using (FileStream fs = File.Open(@"largeVideoFile.avi", FileMode.Open))
        {
            var buffer = new byte[16 * 1024];
            using (var data = new BinaryReader(fs))
            {
                int read;
                while ((read = data.Read(buffer, 0, buffer.Length)) > 0)
                {
                    writer.Write(buffer, 0, read);
                }
            }
        }
    }
}

I am getting the error

System.OutOfMemoryException

when writer.Write is called, alltought I used a intermediate buffer....

Any idea how to solve this?

Upvotes: 5

Views: 2657

Answers (1)

Ignacio Soler Garcia
Ignacio Soler Garcia

Reputation: 21855

Build the application as any CPU and execute it in a x64 machine. This should fix the issue. (Or directly build the application as x64).

Videos normally cannot be compressed a lot and the zip file probably remains in memory until the are completely created.

Upvotes: 2

Related Questions