Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Out of memory exception while updating zip

I am getting OutofMemoryException while trying to add files to a .zip file. I am using 32-bit architecture for building and running the application.

string[] filePaths = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\capture\\capture");
System.IO.Compression.ZipArchive zip = ZipFile.Open(filePaths1[c], ZipArchiveMode.Update);

foreach (String filePath in filePaths)
{
    string nm = Path.GetFileName(filePath);
    zip.CreateEntryFromFile(filePath, "capture/" + nm, CompressionLevel.Optimal);
}

zip.Dispose();
zip = null;

I am unable to understand the reason behind it.

like image 589
user3269550 Avatar asked May 01 '15 07:05

user3269550


1 Answers

The exact reason depends on a variety of factors, but most likely you are simply just adding too much to the archive. Try using the ZipArchiveMode.Create option instead, which writes the archive directly to disk without caching it in memory.

If you are really trying to update an existing archive, you can still use ZipArchiveMode.Create. But it will require opening the existing archive, copying all of its contents to a new archive (using Create), and then adding the new content.

Without a good, minimal, complete code example, it would not be possible to say for sure where the exception is coming from, never mind how to fix it.

EDIT:

Here is what I mean by "…opening the existing archive, copying all of its contents to a new archive (using Create), and then adding the new content":

string[] filePaths = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\capture\\capture");

using (ZipArchive zipFrom = ZipFile.Open(filePaths1[c], ZipArchiveMode.Read))
using (ZipArchive zipTo = ZipFile.Open(filePaths1[c] + ".tmp", ZipArchiveMode.Create))
{
    foreach (ZipArchiveEntry entryFrom in zipFrom.Entries)
    {
        ZipArchiveEntry entryTo = zipTo.CreateEntry(entryFrom.FullName);

        using (Stream streamFrom = entryFrom.Open())
        using (Stream streamTo = entryTo.Open())
        {
            streamFrom.CopyTo(streamTo);
        }
    }

    foreach (String filePath in filePaths)
    {
        string nm = Path.GetFileName(filePath);
        zipTo.CreateEntryFromFile(filePath, "capture/" + nm, CompressionLevel.Optimal);
    }
}

File.Delete(filePaths1[c]);
File.Move(filePaths1[c] + ".tmp", filePaths1[c]);

Or something like that. Lacking a good, minimal, complete code example, I just wrote the above in my browser. I didn't try to compile it, never mind test it. And you may want to adjust some specifics (e.g. the handling of the temp file). But hopefully you get the idea.

like image 185
Peter Duniho Avatar answered Sep 30 '22 06:09

Peter Duniho