简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM