如何将(在服务器中)多个文件压缩到一个存档中?
最佳答案
以下代码使用我们的 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 .
替代解决方案:
SharpZipLib和 DotNetZip广泛使用的免费替代品。
关于c# - 服务器中的 zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3752094/