简体   繁体   English

使用Ionic.Zip在ASP中创建zip文件时,如何将文件夹添加到文件夹

[英]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 :- 我可以使用AddDirectoryByNameAddFile成功创建一个.zip文件,我的代码如下:-

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). 但是发生的是,它创建了一个名称为A的文件夹,并在其中添加了文件(例如.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 ?? 但是我想将此文件夹A放置在另一个名为“ Root”的已创建文件夹中,因此现在如何在.zip中创建一个名为Root的文件夹并将文件夹A添加到该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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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