【问题标题】:Generating zip file using c# code使用 c# 代码生成 zip 文件
【发布时间】:2016-03-26 00:39:32
【问题描述】:

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

【问题讨论】:

标签: asp.net-mvc-2


【解决方案1】:

你看过SharpZipLib吗?

【讨论】:

    【解决方案2】:

    借助 .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

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

    【讨论】:

      【解决方案3】:

      我使用的一个第三方库是http://dotnetzip.codeplex.com/

      我比 SharpZipLib 更喜欢它——SharpZipLib 的布局并不是很直观。

      【讨论】:

        【解决方案4】:

        使用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");
         }
        

        【讨论】:

          猜你喜欢
          • 2017-06-09
          • 1970-01-01
          • 1970-01-01
          • 2011-06-16
          • 2012-09-03
          • 2010-10-14
          • 1970-01-01
          • 2020-08-05
          相关资源
          最近更新 更多