[
  {
    "Id": "460073",
    "ThreadId": "217371",
    "Html": "<p>This method takes a String array with file names from a variety of locations. I am wanting to zip those files into one directory. However if two files have the same name, I get an error and the operation does not complete. Is there a way to deal with this with DotNetZip? For example if there are two files named &quot;Sample.txt&quot;, could the next one be automatically named &quot;Sample(2).txt&quot;? This way all the specified files will be saved without worries of overwriting. Thanks for your help btw the Parameters object just contains the String[], A date object, and a String destination Path. Let me know if you need any more of my other classes.</p>\r\n<p>&nbsp;</p>\r\n<div style=\"color:black;background-color:white\">\r\n<pre><span style=\"color:blue\">class</span> ZipNow\r\n    {\r\n        \r\n        String[] files;\r\n        DateTime date;\r\n        String dPath;\r\n        <span style=\"color:blue\">public</span> Parameters param;\r\n\r\n        <span style=\"color:blue\">public</span> ZipNow() { }\r\n\r\n        \r\n        <span style=\"color:blue\">public</span> <span style=\"color:blue\">bool</span> zip(Object param) <span style=\"color:green\">//save files in one folder from multiple locations</span>\r\n        {\r\n            Parameters prm = (Parameters)param;\r\n\r\n            <span style=\"color:blue\">try</span>\r\n            {\r\n                ZipFile zip = <span style=\"color:blue\">new</span> ZipFile();\r\n\r\n                <span style=\"color:green\">//add all files to the zip file</span>\r\n                <span style=\"color:blue\">for</span> (<span style=\"color:blue\">int</span> i = 0; i &lt; prm.getFiles().Length; i++)\r\n                {\r\n                    <span style=\"color:blue\">if</span> (File.Exists(prm.getFiles()[i]))\r\n                    {\r\n                        zip.AddFile(prm.getFiles()[i],<span style=\"color:#a31515\">&quot;Archive&quot;</span>);\r\n                        \r\n                    }\r\n                    <span style=\"color:blue\">else</span>\r\n                    {\r\n                        MessageBox.Show(<span style=\"color:#a31515\">&quot;File &quot;</span> + prm.getFiles()[i].ToString() + <span style=\"color:#a31515\">&quot; Does not exist.  File will not be included in zip Package&quot;</span>);\r\n                        <span style=\"color:blue\">continue</span>;\r\n                    }\r\n                }\r\n                \r\n                <span style=\"color:green\">//save the zip file to the specified FilePath</span>\r\n                zip.Save(prm.getDPath() + <span style=\"color:#a31515\">&quot;\\\\&quot; + prm.getDate().Month.ToString() + &quot;</span>_<span style=\"color:#a31515\">&quot; + prm.getDate().Day.ToString() + &quot;</span>_<span style=\"color:#a31515\">&quot; + prm.getDate().Year.ToString() + &quot;</span>.zip&quot;);\r\n                <span style=\"color:blue\">return</span> <span style=\"color:blue\">true</span>;\r\n                <span style=\"color:green\">//MessageBox.Show(&quot;Operation Complete&quot;);</span>\r\n            }\r\n            <span style=\"color:blue\">catch</span> (FileNotFoundException exFile)\r\n            {\r\n                MessageBox.Show(<span style=\"color:#a31515\">&quot;File not found &quot;</span> + exFile.Message);\r\n                <span style=\"color:blue\">return</span> <span style=\"color:blue\">false</span>;\r\n            }\r\n            <span style=\"color:blue\">catch</span> (DirectoryNotFoundException exDirectory)\r\n            {\r\n                MessageBox.Show(<span style=\"color:#a31515\">&quot;Directory not Found &quot;</span> + exDirectory.Message);\r\n                <span style=\"color:blue\">return</span> <span style=\"color:blue\">false</span>;\r\n            }\r\n            <span style=\"color:blue\">catch</span> (Exception ex)\r\n            {\r\n                MessageBox.Show(<span style=\"color:#a31515\">&quot;Exeption&quot;</span> + ex.Message);\r\n                <span style=\"color:blue\">return</span> <span style=\"color:blue\">false</span>;\r\n            }\r\n            <span style=\"color:green\">//return true;</span>\r\n\r\n\r\n        }\r\n\r\n\r\n        <span style=\"color:blue\">public</span> String[] getFiles()\r\n        {\r\n            <span style=\"color:blue\">return</span> <span style=\"color:blue\">this</span>.files;\r\n        }\r\n    }\r\n</pre>\r\n</div>",
    "PostedDate": "2010-06-25T13:28:33.447-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  }
]