在创建时发送zip文件

时间:2016-11-29 11:22:51

标签: c# asp.net-mvc sharpziplib

在我的网站上,当用户点击某个按钮时,一堆文件必须以压缩文件存档并发送出去。文件本身由第三部分生成,我只有URL。 我部分成功了,但我有一些问题。

首先,如果有很多要压缩的文件,服务器响应很慢,因为它首先构建zip文件,然后发送它。它甚至可以在一段时间后崩溃(特别是,我得到错误“算术操作中的溢出或下溢。”)。

其次,现在只有在zip存档完成后才会发送文件。我希望立即开始下载。也就是说,一旦用户从对话框中点击“保存”,数据就开始发送,并且当“正在”创建zip文件时它继续发送。我在某些网站上看到了这个功能,例如:http://download.muuto.com/

问题是,我无法弄清楚如何做到这一点。

我使用过这个问题的部分代码:Creating a dynamic zip of a bunch of URLs on the fly 从这篇博客文章:http://dejanstojanovic.net/aspnet/2015/march/generate-zip-file-on-the-fly-in-aspnet-mvc-application/

zip文件本身是在ASP.NET MVC Controller方法中创建和返回的。这是我的代码:

using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace MyProject.Controllers
{
    public class MyController : Controller
    {        
        public ActionResult DownloadFiles()
        {
            var files = SomeFunction();

            byte[] buffer = new byte[4096];

            var baseOutputStream = new MemoryStream();
            ZipOutputStream zipOutputStream = new ZipOutputStream(baseOutputStream);
            zipOutputStream.SetLevel(0); //0-9, 9 being the highest level of compression
            zipOutputStream.UseZip64 = UseZip64.Off;
            zipOutputStream.IsStreamOwner = false;

            foreach (var file in files)
            {
                using (WebClient wc = new WebClient())
                {
                    // We open the download stream of the file
                    using (Stream wcStream = wc.OpenRead(file.Url))
                    {
                        ZipEntry entry = new ZipEntry(ZipEntry.CleanName(file.FileName));
                        zipOutputStream.PutNextEntry(entry);

                        // As we read the stream, we add its content to the new zip entry
                        int count = wcStream.Read(buffer, 0, buffer.Length);
                        while (count > 0)
                        {
                            zipOutputStream.Write(buffer, 0, count);
                            count = wcStream.Read(buffer, 0, buffer.Length);
                            if (!Response.IsClientConnected)
                            {
                                break;
                            }
                        }
                    }
                }
            }
            zipOutputStream.Finish();
            zipOutputStream.Close();

            // Set position to 0 so that cient start reading of the stream from the begining
            baseOutputStream.Position = 0;

            // Set custom headers to force browser to download the file instad of trying to open it
            return new FileStreamResult(baseOutputStream, "application/x-zip-compressed")
            {
                FileDownloadName = "Archive.zip"
            };
        }
    }
}

1 个答案:

答案 0 :(得分:1)

通过对响应输出流和缓冲进行一些调整,我已经找到了解决方案:

using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace MyProject.Controllers
{
    public class MyController : Controller
    {        
        public ActionResult DownloadFiles()
        {
            var files = SomeFunction();

            // Disable Buffer Output to start the download immediately
            Response.BufferOutput = false;

            // Set custom headers to force browser to download the file instad of trying to open it
            Response.ContentType = "application/x-zip-compressed";
            Response.AppendHeader("content-disposition", "attachment; filename=Archive.zip");

            byte[] buffer = new byte[4096];

            ZipOutputStream zipOutputStream = new ZipOutputStream(Response.OutputStream);
            zipOutputStream.SetLevel(0); // No compression
            zipOutputStream.UseZip64 = UseZip64.Off;
            zipOutputStream.IsStreamOwner = false;

            try
            {
                foreach (var file in files)
                {
                    using (WebClient wc = new WebClient())
                    {
                        // We open the download stream of the image
                        using (Stream wcStream = wc.OpenRead(file.Url))
                        {
                            ZipEntry entry = new ZipEntry(ZipEntry.CleanName(file.FileName));
                            zipOutputStream.PutNextEntry(entry);

                            // As we read the stream, we add its content to the new zip entry
                            int count = wcStream.Read(buffer, 0, buffer.Length);
                            while (count > 0)
                            {
                                zipOutputStream.Write(buffer, 0, count);
                                count = wcStream.Read(buffer, 0, buffer.Length);
                                if (!Response.IsClientConnected)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                zipOutputStream.Finish();
                zipOutputStream.Close();
            }

            return new HttpStatusCodeResult(HttpStatusCode.OK);
        }
    }
}