2010-08-09 98 views
2

我想通过使用.net框架类从我的mvc.net c#应用程序创建zip文件。 请尽快回复我。使用c#生成zip文件代码

+0

我会用下面的答案之一,但如果你在一个他们不喜欢第三方dll的地方工作,我会在今天早些时候链接到我对类似问题的回答:http:/ /stackoverflow.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

回答

1

使用外部库如this onethis 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"); 
} 
3

一个第三方库是http://dotnetzip.codeplex.com/

我喜欢它比SharpZipLib多了不少 - SharpZipLib是不是真的很直观地奠定了在所有。

2

随着.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

还有其他的方法生成压缩文件,在上面链接的页面上进一步解释。