[
  {
    "Id": "148704",
    "ThreadId": "44663",
    "Html": "Hi @all!<br>\r\nI'm using this nice ZipDll in my software and I have a problem with &quot;<span style=\"font-size:13px\">UpdateFileStream&quot; depending on how fast the target computer is.<br>\r\nWhat I want to do: Unzip a file from the Zip Archive by loading it into MemoryStream -&gt; change this XML-File -&gt; Update the Zip-File (the file inside the Zip-Archive) with the new MemoryStream.<br>\r\n<br>\r\nHere's my code:<br>\r\n<span style=\"font-size:13px\">\r\n<p>myZip = </p>\r\n</span>\r\n<p><span style=\"font-size:13px;color:#0000ff\">new</span><span style=\"font-size:13px\"> </span><span style=\"font-size:13px;color:#2b91af\">ZipFile</span><span style=\"font-size:13px\">(sFileName);<br>\r\n</span><span style=\"font-size:13px\">myZip.Password = </span><span style=\"font-size:13px;color:#2b91af\">Program</span><span style=\"font-size:13px\">.sZipPW;<br>\r\n</span><span style=\"font-size:13px;color:#2b91af\">MemoryStream</span><span style=\"font-size:13px\"> machineDataStream = </span><span style=\"font-size:13px;color:#0000ff\">new</span><span style=\"font-size:13px\"> </span><span style=\"font-size:13px;color:#2b91af\">MemoryStream</span><span style=\"font-size:13px\">();<br>\r\n</span><span style=\"font-size:13px;color:#0000ff\">string</span><span style=\"font-size:13px\"> sInnerFileToLoadAsDataTable = </span><span style=\"font-size:13px;color:#a31515\">&quot;tblMachineData.xml&quot;</span><span style=\"font-size:13px\">;<br>\r\n</span><span style=\"font-size:13px;color:#0000ff\">string</span><span style=\"font-size:13px\"> sFolderName = </span><span style=\"font-size:13px;color:#2b91af\">PLC_Fill_Methods</span><span style=\"font-size:13px\">.getFolderNameInZip(sFileName); // This method only checks if there is a folder inside the archive and if, what is its name - unimportant<br>\r\nmyZip.Extract(sFolderName + sInnerFileToLoadAsDataTable, machineDataStream);<br>\r\n<br>\r\n// These two lines are new but didn't solve the problem<br>\r\nmyZip.Dispose(); <br>\r\nmyZip = </span><span style=\"font-size:13px;color:#0000ff\">null</span><span style=\"font-size:13px\">;\r\n<p>&nbsp;</p>\r\n</span></p>\r\n<p>&nbsp;</p>\r\n<p><span style=\"font-size:13px;color:#2b91af\">XmlDocument</span><span style=\"font-size:13px\"> x_doc = </span><span style=\"font-size:13px;color:#0000ff\">new</span><span style=\"font-size:13px\"> </span><span style=\"font-size:13px;color:#2b91af\">XmlDocument</span><span style=\"font-size:13px\">();<br>\r\nmachineDataStream.Seek(0, </span><span style=\"font-size:13px;color:#2b91af\">SeekOrigin</span><span style=\"font-size:13px\">.Begin);<br>\r\nx_doc.Load(machineDataStream);<br>\r\nmachineDataStream.Dispose();<br>\r\nmachineDataStream = </span><span style=\"font-size:13px;color:#0000ff\">null</span><span style=\"font-size:13px\">;</span> </p>\r\n<p>&nbsp;</p>\r\n<p><span style=\"font-size:13px;color:#2b91af\">//update the XML File<br>\r\nXmlNode</span><span style=\"font-size:13px\"> x_nodeToChange = x_doc.SelectSingleNode(</span><span style=\"font-size:13px;color:#a31515\">&quot;NewDataSet/tblMachineData[ParameterId = '&quot;</span><span style=\"font-size:13px\"> + sParamId + </span><span style=\"font-size:13px;color:#a31515\">&quot;']&quot;</span><span style=\"font-size:13px\">);<br>\r\n</span><span style=\"font-size:13px;color:#0000ff\">if</span><span style=\"font-size:13px\"> (x_nodeToChange != </span><span style=\"font-size:13px;color:#0000ff\">null</span><span style=\"font-size:13px\">)<br>\r\n{<br>\r\n</span><span style=\"font-size:13px;color:#2b91af\">XmlNode</span><span style=\"font-size:13px\"> x_ParamNode = x_nodeToChange.SelectSingleNode(</span><span style=\"font-size:13px;color:#a31515\">&quot;Value&quot;</span><span style=\"font-size:13px\">);<br>\r\nx_ParamNode.InnerText = sValue;<br>\r\n} </span></p>\r\n<p>&nbsp;</p>\r\n<p><span style=\"font-size:13px;color:#2b91af\">MemoryStream</span><span style=\"font-size:13px\"> newMachineDataStream = </span><span style=\"font-size:13px;color:#0000ff\">new</span><span style=\"font-size:13px\"> </span><span style=\"font-size:13px;color:#2b91af\">MemoryStream</span><span style=\"font-size:13px\">();<br>\r\nx_doc.Save(newMachineDataStream);<br>\r\nnewMachineDataStream.Seek(0, </span><span style=\"font-size:13px;color:#2b91af\">SeekOrigin</span><span style=\"font-size:13px\">.Begin);\r\n<p>myZip = </p>\r\n</span></p>\r\n<p><span style=\"font-size:13px;color:#0000ff\">new</span><span style=\"font-size:13px\"> </span><span style=\"font-size:13px;color:#2b91af\">ZipFile</span><span style=\"font-size:13px\">(sKpdFilePath); // these two lines are also new but didn't solve the problem<br>\r\nmyZip.Password = </span><span style=\"font-size:13px;color:#2b91af\">Program</span><span style=\"font-size:13px\">.sZipPW;\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>\r\n<p>myZip.UpdateFileStream(</p>\r\n</span></p>\r\n<p><span style=\"font-size:13px;color:#a31515\">&quot;tblMachineData.xml&quot;</span><span style=\"font-size:13px\">, sFolderName, newMachineDataStream);\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>\r\n</span></p>\r\n<p>&nbsp;</p>\r\n<span style=\"font-size:13px\">\r\n<p>myZip.Save(); // here I will get the exception; not in Visual Studio or on a Pentium 4 but on fatser PCs like Core2Duo I get &quot;The process cannot access the file &quot;...&quot; because it is being used by another process</p>\r\n<p>newMachineDataStream.Dispose();<br>\r\nnewMachineDataStream = </p>\r\n</span>\r\n<p><span style=\"font-size:13px;color:#0000ff\">null</span><span style=\"font-size:13px\">;<br>\r\n</span><span style=\"font-size:13px\">myZip.Dispose();<br>\r\nmyZip = </span><span style=\"font-size:13px;color:#0000ff\">null</span><span style=\"font-size:13px\">;<br>\r\n<br>\r\nWhat else can I do to manage my problem??? Any idea???\r\n<p> </p>\r\n</span></p>\r\n<p>&nbsp;</p>\r\n</span>\r\n",
    "PostedDate": "2009-01-20T05:12:56.317-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "148733",
    "ThreadId": "44663",
    "Html": "Nobody with an idea??? Is there an yway to set the fileaccess free? Has such a issue never been tested, I can't believe! Please help! <br>\r\nMaybe I open the file for reading in a false way. But if so, are there other ways to open it??? \r\n",
    "PostedDate": "2009-01-20T06:52:30.35-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "149298",
    "ThreadId": "44663",
    "Html": "Jochen, gimme some time to have a look. . .\r\n",
    "PostedDate": "2009-01-21T08:41:33.833-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "149342",
    "ThreadId": "44663",
    "Html": "I tried your general code - it worked for me, but I don't have a fast PC.<br>\r\n<br>\r\nIt's possible that there is a stream that is remaining open inadvertently after the call to x_doc.Save() .  <br>\r\nCould you please try to insert a the following lines into your code following the x_doc.Save() and before the myZip.Save()  ??<br>\r\n<br>\r\nx_doc = null;<br>\r\nGC.Collect(); <br>\r\n<br>\r\nHere is my example: <br>\r\n<br>\r\n<pre>        private void ModifyFileWithinZip()\r\n        {\r\n            using (var zip = ZipFile.Read(zipFileName))\r\n            {\r\n                XmlDocument doc = new XmlDocument();\r\n                using (MemoryStream machineDataStream = new MemoryStream())\r\n                {\r\n                    zip.Extract(xmlFileName, machineDataStream);\r\n\r\n                    machineDataStream.Seek(0, SeekOrigin.Begin);\r\n                    doc.Load(machineDataStream);\r\n                }\r\n\r\n \r\n                //update the XML File\r\n                XmlNode node = doc.SelectSingleNode(xpath);\r\n\r\n                if (node != null)\r\n                {\r\n                    Console.WriteLine(&quot;Node xpath: {0}&quot;, xpath);\r\n                    Console.WriteLine(&quot;current value: {0}&quot;, node.InnerText);\r\n                    Console.WriteLine(&quot;new value: {0}&quot;, newvalue);\r\n                    node.InnerText = newvalue;\r\n\r\n                    using (MemoryStream newMachineDataStream = new MemoryStream())\r\n                    {\r\n                        doc.Save(newMachineDataStream);\r\n\r\n                        // try this to close out any inadvertently open streams:\r\n                        doc= null;\r\n                        GC.Collect();\r\n\r\n                        newMachineDataStream.Seek(0, SeekOrigin.Begin); \r\n\r\n                        zip.UpdateFileStream(xmlFileName, &quot;&quot;, newMachineDataStream); \r\n\r\n                        // Here I will get the exception; not in Visual Studio or on a\r\n                        // Pentium 4 but on faster PCs like Core2Duo I get &quot;The process\r\n                        // cannot access the file &quot;...&quot; because it is being used by\r\n                        // another process.\r\n\r\n                        zip.Save();\r\n                    }\r\n\r\n                }\r\n            }\r\n        }\r\n\r\n</pre>\r\n",
    "PostedDate": "2009-01-21T09:58:07.857-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "570232",
    "ThreadId": "44663",
    "Html": "<p>Hey guys, especially Cheeso; long time lurker here, finally had to post:</p>\n<p>I would like to confirm the same&nbsp;behavior. You fix DOES help, but I am a little iffy about the reason and the fix approach. Specifically, there are cases where this explicit GC.Collect() call may not help.</p>\n<p>What I do not&nbsp;understand&nbsp;is why we need to kill off XMLDocument. I checked the docs and yes, XMLDocument does not have IDispose interfaces (no using) and no explicit .Close() either. The reason as far as I can see it that the XMLDocument is just a memory struct,  so why after .Save() is called on MemoryStream do we fail in .Save(ing) the zip file? Would it help to copy the stream into a new stream and then write *that* stream into the archive?</p>\n<p>Oh and PS. UpdateEntry does not take full path like AddEntry does :( Or, it is somewhat confusing. AddEntry(\"\\\\path\\\\filename.txt\") fails if entry is present but UpdateEntry(\"\\\\path\\\\filename.txt\") just places the file in root :)</p>\n<p>&nbsp;</p>\n<p>P.P.S. Thank you for your dedication and amazing work. Setup a donation page or something, I would most definitely drop some for you!</p>",
    "PostedDate": "2011-02-22T08:26:34.687-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "571210",
    "ThreadId": "44663",
    "Html": "<p>Kommi - not sure about this old story.</p>\r\n<p>I don't usually recommend GC.Collect(), ever, but this was a diagnostic and troubleshooting thing.</p>\r\n<p>If you're having a problem I suggest you open a new thread to cover it.&nbsp; This one is more than 2 years old, and surely is dealing with an older version of the library.</p>\r\n<p>Also - regarding donations - there is a donation page.&nbsp; Not sure how you missed it!</p>\r\n<p><a href=\"http://cheeso.members.winisp.net/DotNetZipDonate.aspx\">http://cheeso.members.winisp.net/DotNetZipDonate.aspx</a></p>",
    "PostedDate": "2011-02-23T18:05:42.86-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  }
]