在 asp.net c# 中压缩文件夹时如何排除根目录

How to exclude root directories when you zip the folder in asp.net c#

我正在尝试压缩一个文件夹,该文件夹位于根文件夹下的 4 层, 我可以压缩文件夹,但它也包含根文件夹。

     using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())
           {

     zip.AddSelectedFiles("*",filepath,string.empty,false);
     zip.Save(HttpContext.Current.Response.OutputStream);
            }

我的需要是只压缩特定文件而不是整个文件夹结构。

感谢任何帮助。

按照这个例子:

using (ZipFile zip = new ZipFile())
{
    // files in the filesystem like MyDocuments\ProjectX\File1.txt, will be
    // stored in the zip archive as backup\File1.txt
    zip.AddDirectory(@"MyDocuments\ProjectX", "backup");

    // files in the filesystem like MyMusic\Santana\OyeComoVa.mp3, will be
    // stored in the zip archive as tunes\Santana\OyeComoVa.mp3
    zip.AddDirectory("MyMusic", "tunes");

    // The Readme.txt file in the filesystem will be stored in the zip
    // archive as documents\Readme.txt
    zip.AddDirectory("Readme.txt", "documents");

    zip.Comment = "This zip was created at " + DateTime.Now.ToString("G");
    zip.Save(ZipFileToCreate);
}

参考:http://dotnetzip.herobo.com/DNZHelp/Index.html