【发布时间】:2016-03-26 00:39:32
【问题描述】:
我想使用 .net 框架类从我的 mvc.net c# 应用程序创建 zip 文件。 请尽快回复我。
【问题讨论】:
-
我会选择以下答案之一,但如果您在他们不喜欢第三方 dll 的地方工作,我会链接到我之前对类似问题的回答今天:stackoverflow.com/questions/3437770/…
标签: asp.net-mvc-2
我想使用 .net 框架类从我的 mvc.net c# 应用程序创建 zip 文件。 请尽快回复我。
【问题讨论】:
标签: asp.net-mvc-2
你看过SharpZipLib吗?
【讨论】:
借助 .NET 4.5,您现在可以使用 .NET 框架非常轻松地创建 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
还有其他生成压缩文件的方法,在上面链接的页面上有进一步的说明。
【讨论】:
我使用的一个第三方库是http://dotnetzip.codeplex.com/
我比 SharpZipLib 更喜欢它——SharpZipLib 的布局并不是很直观。
【讨论】:
使用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");
}
【讨论】: