Simple overwrite ...without testing for existing file?

Jul 17, 2009 at 7:54 AM

I am currently creating my zip file with a bunch of different xml files from memory stream as follows:

            using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile(saveFileDialog1.FileName.ToString()))
                zip.AddFileFromString("File1.xml", "", xmlSerializedFile1);
                zip.AddFileFromString("File2.xml", "", xmlSerializedFile2);
                zip.AddFileFromString("File3.xml", "", xmlSerializedFile3);

The above is done in a button click event. Now on the first click, the files doesnt exist within the zip file, so it works just fine. Now when I click the button again to do the save, i get an ArgumentException - The entry 'File1' already exists in the zip archive.

What is the best and easiest way to just overwrite the existing Files? I dont care to loose the data in the existing files (File1, File2, File3) within the zip, I just want to overwrite with the new Files. Do I have to test for an existing file? Or is there a simpler way to just brute force overwrite?

Jul 17, 2009 at 10:21 AM
Edited Jan 10, 2010 at 2:50 PM

If you are using v1.8 or later, you can call UpdateEntry() .  It will add a new file if none exists, and it will replace the existing file if it does exist.

Also in v1.8, the ZipFile.AddFileFromString() method was renamed to AddEntry().


Jul 17, 2009 at 5:22 PM

I wonder if this is a bug. I am serializing a class into XML using the following serializer code:

        public static string SerializeObject<T>(Encoding encoding, T obj)
                MemoryStream memoryStream = new MemoryStream();

                using (XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, encoding))
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                    xmlSerializer.Serialize(xmlTextWriter, obj);

                    memoryStream = (MemoryStream)xmlTextWriter.BaseStream;

                return ByteArrayToString(encoding, memoryStream.ToArray());
                return string.Empty;

I am currently using the 1.7 version of DotNetZip, and using the zip save code listed above (initial post), i am serializing the class and saving it as an xml file within the zip. I can extract the zip after its saved, and the xml file can be opened easily with IE and the data displayed.

I then downloaded the latest version 1.8, and modified the zip code to zip.AddEntry (cause it was complaining that AddFileFromString is obsolete) and when i serialize the same class and save it, I cant seem to open the xml file in IE. When I edit the xml file, it looks like there is an extra leading '?' that is causing this.

I tried saving some text via string into a text file, and then zipping it, but i dont have the same problem. I know the xmlserializing code works fine because i didnt have this problem with the 1.7 version.

Could this be a bug in the 1.8 version?

Jul 17, 2009 at 6:15 PM

It could be.  I'd have to take a look at it.

Jul 17, 2009 at 6:23 PM

One more thought - text encoding

In v1.8, the AddEntry/UpdateEntry methods use System.Text.Encoding.Default.  In the v1.7, the AddFileFromString() used UTF-8 to do the encoding. 

There are overloads of AddEntry/UpdateEntry  that accept an encoding.  If you pass UTF8 there, you should get the same results as you had in v1.7.


Jul 18, 2009 at 3:13 AM

it was the encoding, i changed the serialization encoding to Encoding.Default. I wasnt paying attention to this since it wasnt in the 1.7 version.

Thanks, and great job on DotNetZip..very useful.

Jul 18, 2009 at 3:33 AM

glad you are finding it useful.