Search code examples
c#asp.netzipdotnetzip

How to add a folder to a folder when creating a zip file in asp using Ionic.Zip


I am able to successfully create a .zip file using AddDirectoryByName and AddFile , my code below :-

using (ZipFile zip = new ZipFile())
{
    zip.AlternateEncodingUsage = ZipOption.AsNecessary;
    zip.AddDirectoryByName("A");
    zip.AddFile("~/.png", "A");
}

but what happens is that, it creates a folder by name A and inside it, it adds a file (eg. .png).

But I want to place this folder A inside another created folder called "Root", so now how can I create a folder called Root to my .zip and add folder A to that Root ??

Any help thankfully appreciated.


Solution

  • Simply use the full path name when creating a new directory.

    using(ZipFile zip = new ZipFile())
    {
        string directoryA = "Root/A";
        string directoryB = "Root/B";
    
        zip.AddEntry($"{directoryA}/readmeA.txt", "Success from Directory A");           
        zip.AddEntry($"{directoryB}/readmeB.txt", "Success from Directory B");
    
        zip.Save("file.zip");
    }