This project is read-only.

Adding files to zip with specific name

Jul 19, 2009 at 7:25 AM

Basically, I have an openFileDialog box that allows the user to add a file...I want to name this file "import.sql". But I also want to have the ability to update this zip with a new file and put the same name "import.sql" the second go around. I tried my code like this:


And it seems to change the file as it adds it, but on updates, it adds another entry with the same name "import.sql" in the same zip file, i have multiple import.sql files.

How can I allow the user to choose a file, and add it to the zip file with a specific name, and later be able to update the import.sql file?




Jul 19, 2009 at 3:41 PM

That's a bug.   It should do what you want - replace the existing file by that name.  I will fix the library.  If you need a workaround in advance of the fix, you can do something like this:

     using (var zip = new ZipFile())
         var e = zip.UpdateFile(SomeRandomFilename);
         e.FileName= fixedFilename;
         e.Comment = "1st entry added";
         e = zip.UpdateFile(differentFileName);
         if (zip.EntryFileNames.Contains(fixedFilename))
         e.FileName= fixedFilename;
         e.Comment = "2nd entry added";
Jul 19, 2009 at 3:47 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Jul 19, 2009 at 4:59 PM

ok, fixed.  v1.8.4.7.

Jul 19, 2009 at 5:21 PM


So just to verify, the usages is like so: zip.UpdateFile(openFileDialogInstall.FileName.ToString()).FileName="import.sql";


Also, wanted to verify, if I do this:



I get an error that the instance name doesnt exist or this by design?

Jul 19, 2009 at 8:15 PM
Edited Jul 20, 2009 at 6:31 PM

yes, the first line of code is ok.

And yes, the error you get is expected. The problem with the 2nd sequence of code is that typically filenames have a drive letter, and that is not allowed in a zip file.  

  ZipEntry e = AddFile(ofd.Filename.ToString(); 
  // e.FileName != ofd.Filename.ToString() 
  // if ofd.Filename.ToString() == "c:\\directory\\file.txt"
  // then e.Filename == /directory/file.text

The filename used to add the entry is transformed in 2 ways: the drive letter is removed and the backslashes get transformed into forward slashes.   This is done to comply with the zipfile format.  Backwards and forward slashes are treated as equal by the string indexer on the ZipFile class.  So, when using the string indexer, you cannot use the drive letter, but it doesn't matter if you use forward slahes or backslashes. zip["\\directory\\file.txt"]==zip["/directory/file.txt"].

Considering all this, I think the reason you get "name doesn't exist" (or whatever) is because of the drive letter. If you want to get the entry you just added, then why not do this:

  ZipEntry e = AddFile(ofd.Filename.ToString()); 
  e.FileName = "import.sql"; 

rather than this:

  ZipEntry e = AddFile(ofd.Filename.ToString()); 
  zip[ofd.Filename.ToString()].FileName = "import.sql"; 

The former seems clearer to me.