+1 vote
by (8.4k points)
edited

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
by (8.4k points)
edited
 
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
by (8.4k points)
edited

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);
}
...