从多个文件创建 zip 文件

Creating zip file from multiple files

下面的代码在创建 zip 文件时工作正常,但创建的文件有一个文件夹 IIS Deploy >>>WebService... 然后是文本文件,而不仅仅是文本文件。

我怎样才能将文本文件添加到 zip 文件中?

ZipFile z = ZipFile.Create("C:\IIS Deploy\WebServiceTest\WebServiceTest\Accident.zip");

//initialize the file so that it can accept updates
z.BeginUpdate();

//add the file to the zip file        
z.Add("C:\IIS Deploy\WebServiceTest\WebServiceTest\test1.txt");
z.Add("C:\IIS Deploy\WebServiceTest\WebServiceTest\test2.txt");
z.Add("C:\IIS Deploy\WebServiceTest\WebServiceTest\test3.txt");

//commit the update once we are done
z.CommitUpdate();
//close the file
z.Close();

如果所有内容都在同一个文件夹中,那么最简单的选择是使用 CreateFromDirectory class。

static void Main()
    {
    // Create a ZIP file from the directory "source".
    // ... The "source" folder is in the same directory as this program.
    // ... Use optimal compression.
    ZipFile.CreateFromDirectory("source", "destination.zip",
        CompressionLevel.Optimal, false);

    // Extract the directory we just created.
    // ... Store the results in a new folder called "destination".
    // ... The new folder must not exist.
    ZipFile.ExtractToDirectory("destination.zip", "destination");
    }

http://www.dotnetperls.com/zipfile

请注意,它适用于 .NET Framework 4.6 和 4.5