服务器中的zip文件

时间:2010-09-20 13:51:07

标签: c# asp.net zip

如何将多个文件压缩(在服务器中)到一个存档?

3 个答案:

答案 0 :(得分:2)

以下代码使用我们的Rebex ZIP,并展示了如何在不使用任何临时文件的情况下将文件添加到ZIP存档中。然后将ZIP发送到Web浏览器。

// prepare MemoryStream to create ZIP archive within
using (MemoryStream ms = new MemoryStream())
{
    // create new ZIP archive within prepared MemoryStream
    using (ZipArchive zip = new ZipArchive(ms))
    {            
         // add some files to ZIP archive
         zip.Add(@"c:\temp\testfile.txt");
         zip.Add(@"c:\temp\innerfile.txt", @"\subfolder");

         // clear response stream and set the response header and content type
         Response.Clear();
         Response.ContentType = "application/zip";
         Response.AddHeader("content-disposition", "filename=sample.zip");

         // write content of the MemoryStream (created ZIP archive) 
         // to the response stream
         ms.WriteTo(Response.OutputStream);
    }
}

// close the current HTTP response and stop executing this page
HttpContext.Current.ApplicationInstance.CompleteRequest();

有关详细信息,请参阅ZIP tutorial

替代解决方案:

SharpZipLibDotNetZip广泛使用的免费替代品。

答案 1 :(得分:1)

答案 2 :(得分:0)

Codeplex具有Dot Net Zip http://dotnetzip.codeplex.com/

您还可以尝试System.IO.Compression