How to zip an entire folder using c#

+1 vote
asked Sep 7, 2010 by Rebex KB (8,430 points)
edited Apr 5, 2011

I want to compress the whole content of the directory and it's subdirs into ZIP archive.

Applies to: Rebex ZIP

2 Answers

0 votes
answered Sep 7, 2010 by Rebex KB (8,430 points)
edited Apr 5, 2011
 
Best answer

Following code uses Rebex ZIP and shows how to add the whole directory into the ZIP archive using a single command:

using Rebex.IO.Compression; 
...
ZipArchive.Add(
  @"c:\temp\myarchive.zip", 
  @"c:\dir.to.archive\*", 
  @"\", 
  ArchiveTraversalMode.Recursive, 
  ArchiveActionOnExistingFile.OverwriteAll
);

For more info see ZIP tutorial.

0 votes
answered Sep 7, 2010 by Rebex KB (8,430 points)
edited Sep 7, 2010

If you want to add multiple directories at once use following code:

using (ZipArchive archive = new ZipArchive(@"c:\temp\myarchive.zip"))
{
    archive.Add(
        @"c:\temp\first.dir\*",
        "/first", 
        ArchiveTraversalMode.Recursive, 
        ArchiveActionOnExistingFile.OverwriteAll);

    archive.Add(
        @"c:\temp\second.dir\*", 
        "/second", 
        ArchiveTraversalMode.Recursive, 
        ArchiveActionOnExistingFile.OverwriteAll);
}
...