How to add content of folder (without the folder itself) when creating zip archive?

0 votes
asked Jul 29, 2015 by Rebex KB (8,210 points)
edited Jul 29, 2015 by Rebex KB

Is there an option to exclude the root folder from the archive when creating a zip from a directory? Sometimes you want only the folder content, not the actual folder itself.

Applies to: Rebex ZIP

1 Answer

0 votes
answered Jul 29, 2015 by Tomas Knopp (58,890 points)
selected Jul 29, 2015 by Rebex KB
 
Best answer

Yes, zipping all content from a specified directory is possible in C# with Rebex ZIP as it supports wildcards.

There are 2 ways to do it, either specify the content of the directory with the "*" mask like this:

ZipArchive zip = new ZipArchive("archive.zip");

//add the content of the "C:\MyData" directory (the "C:\MyData" directory itself will not be added to the archive).
zip.Add(@"C:\MyData\*");

zip.Close();

or use the FileSet class in combination with the "*" pattern to match the content of the directory like this:

ZipArchive zip = new ZipArchive("archive.zip");

// or aternatively use FileSet class to specify all content of the C:\MyData folder:
var fileSet = new FileSet(@"C:\MyData\", "*");
zip.Add(fileSet);

zip.Close();
...