OutOfMemoryException when creating a large ZIP file using System.IO.Packaging

I am trying to debug an OutOfMemoryException that occurs when creating a fairly large ZIP file using System.IO.Packaging.ZipPackage .

The code iterates through a large list of objects, doing the following for each object:

  • Serialization of object data to a temporary file.
  • Create a PackagePart for the file.
  • Copy from System.IO.Stream source to another:
    • Source Stream: FileStream
    • Target stream: PackagePart::GetStream() => MS.Internal.IO.Zip.ZipIOModeEnforcingStream

Finally, it calls Package::Close() , which saves the file.

The problem I'm experiencing is that for a particularly large list of objects, I see an OutOfMemoryException (the x86 process reaches a size of about 1.2 GB).

I was thinking about dividing the object data into pieces, so I only process a smaller amount per loop (i.e. steps 1-3 above). The idea is that I will create n ZIP files in a temporary directory and then find a way to combine them into one archive.

Is this possible with System.IO.Packaging ? What would I like to use to combine parts?

Or is there a better way to fix this?

+6
source share
2 answers

Calling the Flush method of the package object between creating a new package should probably solve the problem, as this will flush the memory buffer to disk.

+3
source

I would use the DotNetZip library ( http://dotnetzip.codeplex.com/ ). I tried several zip libraries (System.IO that you are currently using, as well as SharpZibLib) and of course, the easiest way is to use the DotNetZip library.

You will almost certainly end up with fewer lines of code, and I think the memory usage is very good (there was a problem in the virtual machine environment that I reported, and a new release fixed it).

0
source

Source: https://habr.com/ru/post/899754/


All Articles