C# 从多个文件创建zip文件

C# 从多个文件创建zip文件,c#,zipfile,C#,Zipfile,下面的代码在创建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(

下面的代码在创建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类

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


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

如果我正确理解您的问题,我认为解决方案可能是创建zipfile并添加内容,而不使用完全限定的路径。在add方法上可能还有一个选项,可以只添加文件(不考虑其包含路径)。我尝试了这个选项,但发现文件错误。。应该指定路径,我想我认为@codebase的答案中提供的解决方案是一条很好的路径。我使用的是ICSharpCode.SharpZipLib.Zip