How do I overwrite the existing zip file?

Dec 17, 2013 at 3:11 PM
Hi,
I've been using the library for a few months on Windows 2008, but now that I've moved it to Windows 2012 R2 it's throwing an exception every time I run it.

My program updates a ZIP which contains a single file, by overwriting the ZIP file:
using (ZipFile zip = new ZipFile())
{
    zip.ParallelDeflateThreshold = -1;
    zip.AddFile(settings.SQLiteDbFilePath, String.Empty);
    zip.Save(settings.ZipFilePath);
}
However this now throws the following exception:
Unhandled Exception: System.ArgumentException: An item with the same key has already been added.
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at Ionic.Zip.ZipFile.InternalAddEntry(String name, ZipEntry entry)
   at Ionic.Zip.ZipFile._InternalAddEntry(ZipEntry ze)
   at Ionic.Zip.ZipFile.AddFile(String fileName, String directoryPathInArchive)
I thought that calling Save('filename') would simply overwrite the existing file, but as I'm getting an exception, I presume it's somehow trying to merge the temporary zip file with the file I've told it to save over.

Am I misunderstanding how it works? All I want to do is totally replace the destination zip with the new one. I realise I could delete the file first, but it's non-ideal to have this missing for any period of time (even a few seconds) and if you Delete the file programatically, it still seems to be there after a few seconds.

Any help appreciated!

Nick
Dec 17, 2013 at 7:55 PM
Check the call stack. It's throwing the exception when adding the file not when saving the ZIP file. As I recall, there is a method to update a file that's already there.

Eric
Dec 18, 2013 at 3:52 PM
But at that point, it doesn't even know about the existing zip file as it hasn't been passed the file name yet, so I'm not sure that's what's happening.

Nick