[
  {
    "Id": "644426",
    "ThreadId": "265747",
    "Html": "\r\n<p>Hi</p>\r\n<p>I'm using this library in a console app to zip up files on different network locations.&nbsp; It works good everywhere except one particular location, which I can't explain.</p>\r\n<p>The App is being run from a windows server 2003 system and is written in C# 2010 Express.&nbsp; I have it setup so it will only zip files that are older than 5 days.&nbsp; They are collected into a string collection and then I loop through and add to the\r\n zip file.&nbsp; I've seen it work when I step through the code, but as an EXE it seems like it's not working properly.&nbsp; Is it possible that I'm getting to the Delete file section before the zip.save finishes?&nbsp; So I'm deleting the file I tried zipping\r\n before it's done zipping, and then it doesn't get into the zip file.&nbsp;&nbsp; Is that possible?&nbsp; Please let me know if anything stands out in my code that could be tweaked too (I'm kind of a C# newb)</p>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>\r\n<div style=\"color:black; background-color:white\">\r\n<pre>List&lt;String&gt; contents, <span style=\"color:blue\">string</span> pZipFile,<span style=\"color:blue\">bool</span> storepath\r\n</pre>\r\n</div>\r\n<p><br>\r\n<a href=\"file:///M:/MGI2008/printstream/MGI0001_20110719101123.DBF\"></a></p>\r\n<div style=\"color:black; background-color:white\">\r\n<pre><a>            <span style=\"color:blue\">bool</span> zipexists = <span style=\"color:blue\">false</span>;\r\n            Ionic.Zip.ZipFile zip = <span style=\"color:blue\">new</span> Ionic.Zip.ZipFile();\r\n            <span style=\"color:blue\">if</span> (File.Exists(pZipFile))\r\n                zipexists = <span style=\"color:blue\">true</span>;\r\n            <span style=\"color:blue\">else</span>\r\n                zipexists = <span style=\"color:blue\">false</span>;\r\n\r\n            <span style=\"color:blue\">if</span> (zipexists)\r\n                zip = Ionic.Zip.ZipFile.Read(pZipFile);\r\n\r\n\r\n            <span style=\"color:green\">//using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile(pZipFile))</span>\r\n            <span style=\"color:blue\">using</span> (zip)\r\n            {\r\n                \r\n                zip.ZipError &#43;= MyZipError;\r\n                zip.UseZip64WhenSaving = Zip64Option.AsNecessary;\r\n                zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;\r\n                zip.ZipErrorAction = ZipErrorAction.InvokeErrorEvent;\r\n                <span style=\"color:blue\">foreach</span> (<span style=\"color:blue\">string</span> FileToZip <span style=\"color:blue\">in</span> contents)\r\n                {\r\n                    <span style=\"color:blue\">try</span>\r\n                    {\r\n                        <span style=\"color:blue\">if</span> (storepath == <span style=\"color:blue\">true</span>)\r\n                            zip.UpdateFile(FileToZip);\r\n                        <span style=\"color:blue\">else</span>\r\n                            zip.UpdateFile(FileToZip, <span style=\"color:#a31515\">&quot;&quot;</span>);\r\n                        <span style=\"color:green\">//zip.AddFile(FileToZip);</span>\r\n                    }\r\n                    <span style=\"color:blue\">catch</span> (Exception ex)\r\n                    {\r\n                        nlog.WriteLog(<span style=\"color:#a31515\">&quot;Error adding &quot;</span> &#43; FileToZip &#43; Environment.NewLine &#43; ex.Message &#43; Environment.NewLine &#43; ex.StackTrace);\r\n                        zip.Dispose();\r\n                        <span style=\"color:blue\">return</span> <span style=\"color:blue\">false</span>;\r\n                    }\r\n                }\r\n                <span style=\"color:blue\">try</span>\r\n                {\r\n                    Console.WriteLine(<span style=\"color:#a31515\">&quot;Zipping &quot;</span> &#43; pZipFile);\r\n                    zip.Save(pZipFile);\r\n                }\r\n                <span style=\"color:blue\">catch</span> (Exception ex)\r\n                {\r\n                    nlog.WriteLog(<span style=\"color:#a31515\">&quot;Error zipping file(s) into &quot;</span> &#43; pZipFile &#43; Environment.NewLine &#43; ex.Message &#43; Environment.NewLine &#43; ex.StackTrace);\r\n                    zip.Dispose();\r\n                    <span style=\"color:blue\">return</span> <span style=\"color:blue\">false</span>;\r\n                }\r\n            }\r\n            BatchError berror = <span style=\"color:blue\">new</span> BatchError();\r\n            Console.WriteLine(<span style=\"color:#a31515\">&quot;Deleting files&quot;</span>);\r\n            <span style=\"color:blue\">foreach</span> (<span style=\"color:blue\">string</span> FileToZip <span style=\"color:blue\">in</span> contents)\r\n            {\r\n                <span style=\"color:blue\">try</span>\r\n                {\r\n                    File.Delete(FileToZip);\r\n                }\r\n                <span style=\"color:blue\">catch</span> (Exception ex)\r\n                {\r\n                    berror.AddError(FileToZip, ex.Message);\r\n                    <span style=\"color:green\">//nlog.WriteLog(&quot;Error deleting file: &quot;&#43;FileToZip&#43;Environment.NewLine&#43;ex.Message&#43;Environment.NewLine&#43;ex.StackTrace);</span>\r\n                }\r\n            }\r\n            <span style=\"color:blue\">if</span> (berror.HasErrors)\r\n            {\r\n                nlog.WriteLog(<span style=\"color:#a31515\">&quot;Error Deleting File(s):&quot;</span> &#43; Environment.NewLine &#43; berror.GetErrorMessage());\r\n                <span style=\"color:blue\">return</span> <span style=\"color:blue\">false</span>;\r\n            }\r\n            <span style=\"color:blue\">return</span> <span style=\"color:blue\">true</span>;\r\n</a></pre>\r\n</div>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>\r\n",
    "PostedDate": "2011-07-19T08:37:01.517-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "644468",
    "ThreadId": "265747",
    "Html": "<p>I'll have a look and send you a response a little later. At first glance it appears that you are not taking advantage of the using clause as well as you could.</p>",
    "PostedDate": "2011-07-19T09:18:25.88-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "644707",
    "ThreadId": "265747",
    "Html": "<p>Newb, this works for me:</p>\r\n<div style=\"color: black; background-color: white;\">\r\n<pre>        <span style=\"color: blue;\">void</span> MyZipError (<span style=\"color: blue;\">object</span> sender, Ionic.Zip.ZipErrorEventArgs e)\r\n        {\r\n            Console.WriteLine(<span style=\"color: #a31515;\">\"Error!\"</span>);\r\n        }\r\n\r\n        <span style=\"color: blue;\">void</span> MySaveProgress (<span style=\"color: blue;\">object</span> sender, Ionic.Zip.SaveProgressEventArgs e)\r\n        {\r\n            <span style=\"color: blue;\">switch</span> (e.EventType)\r\n            {\r\n                <span style=\"color: blue;\">case</span> ZipProgressEventType.Saving_Started:\r\n                    Console.WriteLine(<span style=\"color: #a31515;\">\"status saving started...\"</span>);\r\n                    <span style=\"color: blue;\">break</span>;\r\n\r\n                <span style=\"color: blue;\">case</span> ZipProgressEventType.Saving_BeforeWriteEntry:\r\n                    Console.WriteLine(<span style=\"color: #a31515;\">\"Compressing {0}\"</span>, e.CurrentEntry.FileName);\r\n                    <span style=\"color: blue;\">break</span>;\r\n                <span style=\"color: blue;\">case</span> ZipProgressEventType.Saving_Completed:\r\n                    Console.WriteLine(<span style=\"color: #a31515;\">\"status Save completed\"</span>);\r\n                    <span style=\"color: blue;\">break</span>;\r\n            }\r\n        }\r\n\r\n\r\n        <span style=\"color: blue;\">public</span> <span style=\"color: blue;\">bool</span> Run()\r\n        {\r\n            CreateLinks();\r\n            List&lt;String&gt; contents = <span style=\"color: blue;\">new</span> List&lt;String&gt;(Directory.GetFiles(subdir));\r\n            <span style=\"color: blue;\">string</span> pZipFile = <span style=\"color: #a31515;\">\"newb.zip\"</span>;\r\n            <span style=\"color: blue;\">bool</span> storepath = <span style=\"color: blue;\">true</span>;\r\n\r\n            <span style=\"color: blue;\">try</span>\r\n            {\r\n                <span style=\"color: green;\">// You can use the parameterized constructor for ZipFile,</span>\r\n                <span style=\"color: green;\">// regardless whether the file exists or not.  If it exists,</span>\r\n                <span style=\"color: green;\">// it will be read.  If it does not exist, it will be</span>\r\n                <span style=\"color: green;\">// created.</span>\r\n                <span style=\"color: blue;\">using</span> (Ionic.Zip.ZipFile zip = <span style=\"color: blue;\">new</span> Ionic.Zip.ZipFile(pZipFile))\r\n                {\r\n                    zip.ZipError += MyZipError;\r\n                    zip.SaveProgress += MySaveProgress;\r\n                    zip.ZipErrorAction = ZipErrorAction.InvokeErrorEvent;\r\n                    zip.UseZip64WhenSaving = Zip64Option.AsNecessary;\r\n                    zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;\r\n\r\n                    <span style=\"color: blue;\">foreach</span> (<span style=\"color: blue;\">string</span> fileToZip <span style=\"color: blue;\">in</span> contents)\r\n                    {\r\n                        Console.WriteLine(<span style=\"color: #a31515;\">\"adding {0}\"</span>, fileToZip);\r\n\r\n                        <span style=\"color: blue;\">if</span> (storepath == <span style=\"color: blue;\">true</span>)\r\n                            zip.UpdateFile(fileToZip);\r\n                        <span style=\"color: blue;\">else</span>\r\n                            zip.UpdateFile(fileToZip, <span style=\"color: #a31515;\">\"\"</span>);\r\n                    }\r\n\r\n                    Console.WriteLine(<span style=\"color: #a31515;\">\"Saving \"</span> + pZipFile);\r\n                    zip.Save(pZipFile);\r\n                }\r\n\r\n            }\r\n            <span style=\"color: blue;\">catch</span> (Exception ex)\r\n            {\r\n                Console.WriteLine(<span style=\"color: #a31515;\">\"Error zipping file(s) into \"</span> + pZipFile + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace);\r\n                <span style=\"color: blue;\">return</span> <span style=\"color: blue;\">false</span>;\r\n            }\r\n\r\n\r\n            Console.WriteLine(<span style=\"color: #a31515;\">\"Deleting files..\"</span>);\r\n            <span style=\"color: blue;\">int</span> nErrors = 0;\r\n            <span style=\"color: blue;\">foreach</span> (<span style=\"color: blue;\">string</span> fileToZip <span style=\"color: blue;\">in</span> contents)\r\n            {\r\n                <span style=\"color: blue;\">try</span>\r\n                {\r\n                    File.Delete(fileToZip);\r\n                }\r\n                <span style=\"color: blue;\">catch</span> (Exception ex)\r\n                {\r\n                    Console.WriteLine(<span style=\"color: #a31515;\">\"Error deleting {0} : {1}\"</span>,\r\n                                      fileToZip, ex.Message);\r\n                    nErrors++;\r\n                }\r\n            }\r\n            <span style=\"color: blue;\">if</span> (nErrors &gt; 0)\r\n            {\r\n                <span style=\"color: green;\">//Console.WriteLine(\"Error Deleting File(s):\" + Environment.NewLine + berror.GetErrorMessage());</span>\r\n                <span style=\"color: blue;\">return</span> <span style=\"color: blue;\">false</span>;\r\n            }\r\n            Directory.Delete(subdir, <span style=\"color: blue;\">true</span>);\r\n            <span style=\"color: blue;\">return</span> <span style=\"color: blue;\">true</span>;\r\n        }\r\n\r\n</pre>\r\n</div>\r\n<p>Some tips: You do not need a try..catch within the using clause. You do not need to explicitly call .Dispose() on the ZipFile instance - the using clause does it for you. You do not need to use 2 different ways to instantiate a ZipFile instance - you can use a single constructor whether the zip file exists or not.</p>\r\n<p>I don't know why your code wasn't working, but it's better to use correct code, then try to debug, rather than debugging incorrect code.</p>",
    "PostedDate": "2011-07-19T16:59:42.397-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "645007",
    "ThreadId": "265747",
    "Html": "<p>Thanks Cheeso!</p>\r\n<p>&nbsp;</p>\r\n<p>I'm going to go through your code and make sure I understand what's going on and then try debugging that.</p>",
    "PostedDate": "2011-07-20T07:58:03.013-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  }
]