从多个文件创建zip文件

本文关键字:zip 文件 文件创建 | 更新日期: 2023-09-27 18:10:18

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

从多个文件创建zip文件

如果所有内容都在同一个文件夹中,那么最简单的选择是使用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");
    }
http://www.dotnetperls.com/zipfile

请注意,它适用于。net Framework 4.6和4.5