Updating and Renaming at the same time

May 21, 2009 at 5:22 PM

Hi,

Say I create a zip and add a file and rename it.  How do I update the file but at the same time use the name I originally used when I first added it? Is there a simple way to do it or do I need to delete the original entry first?

Thanks.

--Lenard

 

Coordinator
May 21, 2009 at 6:34 PM
Edited May 21, 2009 at 9:23 PM

?

I'm not understanding what you want to do.

using (var zip = new ZipFile())
{
  ZipEntry e = zip.AddFile("OriginalFileName.txt", "");
  e.FileName= "NewFileName.txt";
  zip.Save("Archive.zip");
}

Ok, with that code you've added a file to an archive, and you renamed the file entry within the archive, before saving it. Next, you want to Update the file, but use the original file name?  I don't know exactly what that means. In the context of DotNetZip, "Update" means to set new content for an entry given the same filename. So you cannot "Update" an entry using a name that no longer exists.

To illustrate, if you followed the above code with something like this:

using (var zip = ZipFile.Read("Archive.zip"))
{
  zip.UpdateFile("OriginalFileName.txt", "");
  zip.Save();
}

You would then have a zipfile with 2 entries in it. One named "NewFileName.txt" and one named "OriginalFileName.txt".  That's because UpdateFile() will add or update the entry - the method should actually be called "Add or Update File".  If the entry exists in the archive, it will be updated.  If the entry doesn't exist, then it will be added.  Both entries would have the same content (assuming the contents of the actual file on disk named "OriginalFileName.txt" did not change between the two sections of code).

So the point is, it is not really meaningful to "Update" the entry in the zip file using the original file name, if the original file name is no longer associated to the entry. Do you see?

I'm sure I have not understood what you intend.

Maybe there is something else. Maybe you want to rename the entry, so that, instead of the 2nd stanza of code above, you did this:

using (var zip = ZipFile.Read("Archive.zip"))
{
  ZipEntry e = zip["NewFileName.txt"];
  e.FileName = "OriginalFileName.txt";
  zip.UpdateFile("OriginalFileName.txt", "");
  zip.Save();
}

In this case you would have a single entry, with updated content, but using the original name.

You can also do something like this:

using (var zip = ZipFile.Read("Archive.zip"))
{
  ZipEntry e = zip["NewFileName.txt"];
  e.FileName = "OriginalFileName.txt";
  e= zip.UpdateFile("OriginalFileName.txt", "");
  e.FileName = "AnotherNewName.txt";
  zip.Save();
}

It's starting to look convoluted. But as I said, I don't understand what you want to do.

May 21, 2009 at 7:07 PM

Hi Cheeso,

What I was trying to say was that the file inside the zip is named differently than the file in the OS file system and that I wanted to update the zipped file later using the internal name.  That said, you've provided enough info to help me with my question.

Thanks for the quick response!

--Lenard