Memory exception when updating zip in C # .net

I get an OutofMemoryException when I try to add files to a zip file in C # .net. I use a 32-bit architecture to launch and run 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 can not understand the resonance behind him

+4
source share
2 answers

The exact reason depends on many factors, but most likely you just simply add too much to the archive. Instead, try using a parameter ZipArchiveMode.Createthat writes the archive directly to disk without caching it in memory.

, ZipArchiveMode.Create. , ( Create), .

, , , , , , .

EDIT:

"& hellip; , ( Create), ":

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]);

- . , , , . , . (, ). , , .

+7

. OutOfMemoryException , .

. , . , .

1. 32-, 64- ( System.Diagnostics.Process.Start). , 64- , 32- . , , .

2. - , . ZipArchive.Dispose . , ZipArchive, .

            foreach (String filePath in filePaths)
            {
                System.IO.Compression.ZipArchive zip = ZipFile.Open(filePaths1[c], ZipArchiveMode.Update);
                string nm = Path.GetFileName(filePath);
                zip.CreateEntryFromFile(filePath, "capture/" + nm, CompressionLevel.Optimal);
                zip.Dispose();
            }

, , .

-1

All Articles