Is this possible?

Mar 11, 2009 at 6:06 AM
Hi, quick questions to the devs.  First - I thank you for your hard work!  I'm working on an application that cleans up your computer and I have an algorithm that does something like the following:

for(loop)
{
open zip
add file to zip
save zip
delete file
}

Rinse and repeat a hundred thousand times and it appears to progressively get slower and slower.  Is there a method that will allow me to add a file to a zip while maintaining the zip open and ready to accept another file WHILE allowing me to delete the file just added?  What I'm finding is that the file cannot be deleted until the save.  If I call save then it appears that the stream to the zip is closed out yielding performance problems.

Best regards,

Obsidience
Coordinator
Mar 15, 2009 at 1:01 AM
Yes, it should get slower and slower each cycle through that loop. Every time you call ZipFile.Save(), the library writes out all the content in the ZipFile - every ZipEntry needs to be written to the disk.  If you open a zipfile with 10000 entries and add an entry to it, then call Save(), you will write out 10001 entries to the disk.  So obviously, that takes longer and longer.  There is an optiization where it does not re-compress the entries that are already compressed.  I'm thinking about it now... I'm not sure if that optimization is being taken in the case where you repeatedly Add() an entry and then Save().  I think it may not, in which case, in your scenario, it's actually worse - because you are recompressing each of the 10000 entries.   As I said I am not sure that this is the case, I would have to inspect the code closely.  But if it were, then you it would be better for you to re-read the Zip file each time through the loop.  In other words, instead of this:
using (Zipfile zip = new ZipFile())
{
   foreach (file in collection)
  {
     zip.Add(file);
    zip.Save(MyLocation);
  }
}

It would actually be better to do this:
foreach (file in collection)
{
  using (Zipfile zip = ZipFile.Read(MyLocation))
  {
    zip.Add(file);
    zip.Save();
  }
}

Can you try that out and see which is better for you?
There's probably a crossover point, at some number of files. 50? 500? 5000? where the second approach becomes faster than the first.
Mar 15, 2009 at 7:40 AM
Cheeso,

Thanks for your reply.  I'll give that a shot and will reply back with the result.

Obsidience