Can't add file or folder without overwriting

Sep 4, 2009 at 7:04 AM

Hello, 

I'm having a problem adding file or folder to an archive. Here is the code I'm using:
public void ZipDir(string fileName, string src, string dirInZip)
{        
    using (ZipFile zip = new ZipFile())
    {
        zip.SaveProgress += save_Progress;
        zip.CompressionLevel = CompressionLevel.BestCompression;      
        zip.AddSelectedFiles(selection, src, dirInZip, true);
        zip.Save(fileName);
    }
} 


Works perfectly until I try adding a file or folder to the existing .zip file because instead of adding to the
archive it just overwrites it. I was under the impression that AddSelectedFiles will update and existing file.
Sorry if this is a redundant question but i couldn't find any discussions about it.

Thanks in advance

Coordinator
Sep 4, 2009 at 7:06 AM

I'm sorry I don't understand the problem you're having. 

you wrote

> I was under the impression that AddSelectedFiles will update and existing file.

 

and from your description, that is what's happening.

?

I don't see the problem.

 

Sep 4, 2009 at 1:33 PM

Ok lets say I want to add folder1 and folder2 to my archive, i will Write:

 ZipDir( File.zip, Folder1, PathInArchive);

then

ZipDir( File.zip, Folder2, PathInArchive);

 

but instead of the archive having Folder1 AND Folder2 it ends up with just folder 2 because it overwirtes the file instead of updating it. 

Coordinator
Sep 4, 2009 at 3:03 PM

Ah, ok.  Well looking at your code

public void ZipDir(string fileName, string src, string dirInZip)
{        
    using (ZipFile zip = new ZipFile())
    {
        zip.SaveProgress += save_Progress;
        zip.CompressionLevel = CompressionLevel.BestCompression;      
        zip.AddSelectedFiles(selection, src, dirInZip, true);
        zip.Save(fileName);
    }
} 

you create a zipfile instance, add files to it, and then save it to a zipfile.

If you do that three times in a row, you will have written different data to the same filename 3 times. if you want to update an existing zip file, you will have to open the existing zip file before adding entries to it.

public void ZipDir(string fileName, string src, string dirInZip)
{        
    using (ZipFile zip = ZipFile.Read(fileName))
    {
        zip.SaveProgress += save_Progress;
        zip.CompressionLevel = CompressionLevel.BestCompression;      
        zip.AddSelectedFiles(selection, src, dirInZip, true);
        zip.Save(fileName);
    }
} 
Sep 4, 2009 at 3:16 PM

That works perfectly! thanks cheeso, you rock.

Sep 9, 2009 at 12:22 AM

Ok one more small problem I'm having. Well i wouldn't say it's a problem but more of an inconvenience that I'd like to find a workaround for. Here is the method I use to add folders to a zip file:

 

            SetExclusions();
            if (File.Exists(fileName))
            {
                using (ZipFile zip = ZipFile.Read(fileName))
                {
                    zip.SaveProgress += save_Progress;                
                    if (dir)
                    {
                    zip.AddSelectedFiles(exclude, src, dirInZip, true);
                    }
                    else 
                    {
                        zip.AddFile(src);
                    }
                    zip.Save(fileName);
                    zip.Dispose();                   
                }
            }
            else
            {
                using (ZipFile zip = new ZipFile())
                {
                    zip.SaveProgress += save_Progress;
                    if (dir)
                    {
                        zip.AddSelectedFiles(exclude, src, dirInZip, true);
                    }
                    else
                    {
                        zip.AddFile(src);
                    }
                    zip.Save(fileName);
                    zip.Dispose();
                }
            }

 

This works perfectly except every time I add a new file or folder to the archive, it seems to "re-add" all the previous entries. Granted it "re-adds" them much faster than the first time but as you can imagine after the 5th or 6th entry this can take a good amount of time to complete. Is there any way I can avoid this? If not it's ok but it would greatly increase the performance of my program.

Thanks.

Coordinator
Sep 9, 2009 at 2:00 AM
Edited Sep 9, 2009 at 2:02 AM

I don't know what you mean by "re-add all the previous entries."

Can you tell me what the problem is, exactly?   Is the zip file corrupted?   Are you saying that it is just slow?

Here's the thing, when saving a zip file, all the entries need to be written out.

When updating a zipfile, all the entries are read in, and then when saving, all the entries will be written out.  This includes any entries that originally came from the on-disk zipfile, as well as any new entries added with AddFile(), AddDirectory(), AddEntry() and so on.   The entries that were in the original zipfile will just be streamed across - there's no compression performed because they are available in an already compressed. This is why it's faster the 2nd time through.  But the IO still happens.

There's no way to avoid writing out the entries.  I think you may be concerned that the entries get written again, even if they were in the original zipfile.  If you have a problem with IO speeds, don't save the ZipFile so often. 

By the way you can eliminate the big If statement by just using the ZipFile contructor that accepts a string.   If the file exists it will read it.  If the file does not exist it will create it.  Check the doc for more info.