以下代码在创建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();
答案 0 :(得分:2)
如果您拥有同一文件夹中的所有内容,那么最简单的选择就是使用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