I often find myself forced to zip or unzip files if not for the sake of compression then just for bundling files together.
There are actually quite a few libraries for zipping out there but they do exists. One example is SharpZip which I´ve started using, it’s lightweight and simple.
When using this library I once found myself in the need to compress an entire file structure which unfortunately wasn’t so easy. I tried to add entries in the following manner
But this gives us the following zip structure, notice the folder and file being on the same level.
I tried to use the zipFile.Add(string filename, string entryName) but this
screwed up my file.
After looking about the source code for a while I found the FastZip.Create method using the NameConverter
to trim the basefolder from the filenames. To avoid having to hack into the library simply use this code instead:
zipFile.EntryFactory.NameTransform = new ZipNameTransform(directory); zipFile.Add("c:MyFoldermyFilename.txt");
Which´ll give you:
There you go!