Asp.net 使用SharpZipLib在.net中通过http传输zip文件

Asp.net 使用SharpZipLib在.net中通过http传输zip文件,asp.net,http,zip,stream,Asp.net,Http,Zip,Stream,我正在做一个简单的下载服务,这样用户就可以从我们的网站下载所有的图片。 为此,我只需将所有内容压缩到http流 但是,似乎所有内容都存储在内存中,直到zip文件完成并关闭输出,数据才会发送。 我希望服务立即开始发送,并且不要使用太多内存 public void ProcessRequest(HttpContext context) { List<string> fileNames = GetFileNames(); context.Response.ContentTy

我正在做一个简单的下载服务,这样用户就可以从我们的网站下载所有的图片。 为此,我只需将所有内容压缩到http流

但是,似乎所有内容都存储在内存中,直到zip文件完成并关闭输出,数据才会发送。 我希望服务立即开始发送,并且不要使用太多内存

public void ProcessRequest(HttpContext context)
{
    List<string> fileNames = GetFileNames();
    context.Response.ContentType = "application/x-zip-compressed";
    context.Response.AppendHeader("content-disposition", "attachment; filename=files.zip");
    context.Response.ContentEncoding = Encoding.Default;
    context.Response.Charset = "";

    byte[] buffer = new byte[1024 * 8];

    using (ICSharpCode.SharpZipLib.Zip.ZipOutputStream zipOutput = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(context.Response.OutputStream))
    {
        foreach (string fileName in fileNames)
        {
            ICSharpCode.SharpZipLib.Zip.ZipEntry zipEntry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(fileName);
            zipOutput.PutNextEntry(zipEntry);
            using (var fread = System.IO.File.OpenRead(fileName))
            {
                ICSharpCode.SharpZipLib.Core.StreamUtils.Copy(fread, zipOutput, buffer);
            }
        }
        zipOutput.Finish();
    }

    context.Response.Flush();
    context.Response.End();
}
public void ProcessRequest(HttpContext上下文)
{
List fileNames=GetFileNames();
context.Response.ContentType=“应用程序/x-zip-compressed”;
AppendHeader(“内容处置”,“附件;文件名=files.zip”);
context.Response.ContentEncoding=Encoding.Default;
context.Response.Charset=“”;
字节[]缓冲区=新字节[1024*8];
使用(ICSharpCode.SharpZipLib.Zip.ZipOutputStream ZIPOUTPUTPUT=新ICSharpCode.SharpZipLib.Zip.ZipOutputStream(context.Response.OutputStream))
{
foreach(文件名中的字符串文件名)
{
ICSharpCode.SharpZipLib.Zip.ZipEntry-ZipEntry=新ICSharpCode.SharpZipLib.Zip.ZipEntry(文件名);
zipOutput.PutNextEntry(zipEntry);
使用(var fread=System.IO.File.OpenRead(文件名))
{
ICSharpCode.SharpZipLib.Core.StreamUtils.Copy(fread、zipOutput、buffer);
}
}
zipOutput.Finish();
}
context.Response.Flush();
context.Response.End();
}

我可以看到工作进程在生成文件时内存不断增长,然后在完成发送时释放内存。如何在不使用太多内存的情况下执行此操作?

with
context.Response.BufferOutput=false
并从代码末尾删除
Flush
调用。

使用Response.BufferOutput=false;在ProcessRequest开始时,在每个文件后刷新响应。

供参考。这是递归添加整个文件树的工作代码,可将流传输到浏览器:

string path = @"c:\files";

Response.Clear();
Response.ContentType = "application/zip";
Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", "hive.zip"));
Response.BufferOutput = false;

byte[] buffer = new byte[1024 * 1024];
using (ZipOutputStream zo = new ZipOutputStream(Response.OutputStream, 1024 * 1024)) {
    zo.SetLevel(0);
    DirectoryInfo di = new DirectoryInfo(path);
    foreach (string file in Directory.GetFiles(di.FullName, "*.*", SearchOption.AllDirectories)) {
        string folder = Path.GetDirectoryName(file);
        if (folder.Length > di.FullName.Length) {
            folder = folder.Substring(di.FullName.Length).Trim('\\') + @"\";
        } else {
            folder = string.Empty;
        }
        zo.PutNextEntry(new ZipEntry(folder + Path.GetFileName(file)));
        using (FileStream fs = File.OpenRead(file)) {
            ICSharpCode.SharpZipLib.Core.StreamUtils.Copy(fs, zo, buffer);
        }
        zo.Flush();
        Response.Flush();
    }
    zo.Finish();
}

Response.Flush();

当缓冲关闭时,刷新没有任何用处。我相信还有其他方法可以处理非常大的文件,允许单独的请求获得单独的响应块。我会将文件压缩到磁盘上,以便能够在多次请求中为其提供服务,而无需每次重新压缩。