使用c#生成zip文件代码
答
使用外部库如this one或this one。 例如与DotNetZip你可以做一个zip文件是这样的:
using (ZipFile zip = new ZipFile())
{
// add this map file into the "images" directory in the zip archive
zip.AddFile("c:\\images\\personal\\7440-N49th.png", "images");
// add the report into a different directory in the archive
zip.AddFile("c:\\Reports\\2008-Regional-Sales-Report.pdf", "files");
zip.AddFile("ReadMe.txt");
zip.Save("MyZipFile.zip");
}
答
随着.NET 4.5你现在可以真正轻松地使用.NET Framework创建zip文件:
using System;
using System.IO;
using System.IO.Compression;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string startPath = @"c:\example\start";
string zipPath = @"c:\example\result.zip";
string extractPath = @"c:\example\extract";
ZipFile.CreateFromDirectory(startPath, zipPath);
ZipFile.ExtractToDirectory(zipPath, extractPath);
}
}
}
直接从微软的文档采取上面的代码:http://msdn.microsoft.com/en-us/library/ms404280(v=vs.110).aspx
还有其他的方法生成压缩文件,在上面链接的页面上进一步解释。
我会用下面的答案之一,但如果你在一个他们不喜欢第三方dll的地方工作,我会在今天早些时候链接到我对类似问题的回答:http:/ /*.com/questions/3437770/how-to-extract-zip-file-using-dotnet-framework-4-0-without-using-third-party-dlls/3438147#3438147 – 2010-08-09 14:33:52