c# - 服务器中的 zip 文件

标签 c# asp.net zip

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

最佳答案

以下代码使用我们的 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广泛使用的免费替代品。

关于c# - 服务器中的 zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3752094/

相关文章:

c# - 接口(interface) IDataErrorInfo 不起作用

c# - 有没有办法在 IIS6 中访问 asp.net Response.Headers?

c# - 在 LINQ-to-Entities 查询中使用 .Include() 时返回最大/限制?

c# - SQL 报告服务 - 在报告中查找项目 - 加载

java,使用字典进行zip?

c# - NCommon 的任何替代品?

javascript - 避免 LinkedIn 收件人在连接消息中看到彼此的姓名和电子邮件地址

android - 验证备份 ZIP 文件

java - 如何修复 org.apache.commons.compress.archivers.zip.UnsupportedZipFeatureException(epub mimetype)?

c# - 如何通知Windows服务(C#)数据库表更改(sql 2005)?