How To Add Files To The Root Of A Zip File With DotNetZip
[C#, .NET]
I was recently overhauling some code to add a new requirement to generate some files, process them, and add them to the root of the Zip file.
This is something we have talked about before on the post “How To Zip Multiple Files In C# & .NET”
However the caveat here is that this is already written codebase that is using the DotNetZip library, and therefore replacing that with System.IO.Compression is not an option.
The initial code was as follows:
var files = Directory.GetFiles(rootFolder);
var zipFile = Path.Combine("/Users/Rad/Target", $"Generated.zip");
using (var zip = new ZipFile())
{
zip.AddFiles(files);
zip.Save(zipFile);
}
The magic is taking place here:
zip.AddFiles(files);
This will add files to the Zip and preserve their existing hierarchy.
If you don’t want this, i.e. you want to add them to the root, use this overload:
zip.AddFiles(files, "");
This will add the files to the root level of the Zip file.
TLDR
To add files to the root of a Zip file without hierarchy, i.e. in the root, use the overload zip.AddFiles(files, "");
Happy hacking!