从流中创建多文件zip并即时下载

时间:2016-02-26 06:29:26

标签: c# .net dotnetzip

我使用DotNetZip库制作多文件zip存档并即时下载(无需等待下载开始)。但是我无法立即下载。从浏览器开发控制台,在网络选项卡中,我注意到第一个zip文件是"已转移"完全转移后,它开始下载。

以下是代码片段:

using (var vZipArchive = new ZipFile())
{
    if (vFilesTable.Rows.Count > 0)
    {
        string vPathFormat = null;
        string vKeyName = null;
        string vPrimKey = null;
        string vFileName = null;

        vZipArchive.CompressionLevel = CompressionLevel.BestSpeed;
        vZipArchive.CompressionMethod = CompressionMethod.Deflate;
        vZipArchive.Comment = "Document Archive";
        foreach (DataRow vFile in vFilesTable.Rows)
        {
            if (vKeyFieldName != null && !string.IsNullOrEmpty(vKeyFieldName))
            {
                vPathFormat = "{0}/";
            }
            else
            {
                vPathFormat = "/";
            }
            if (vFile.Table.Columns.Contains("FileName") && vFile.Table.Columns.Contains("PrimKey"))
            {
                vPrimKey = vFile["PrimKey"].ToString();
                vFileName = vFile["FileName"].ToString();
                if (vKeyFieldName != null)
                {
                    vKeyName = vFile[vKeyFieldName].ToString();
                }
                string vPath = null;
                if (vKeyFieldName != null)
                {
                    vPath = string.Format(vKeyFieldName + " " + vPathFormat + "{1}", vKeyName, vFileName);
                }
                else
                {
                    vPath = string.Format(vPathFormat + vFileName);
                }
                using (var vFileStream = vUserContext.GetFileStream(vRecordSource.ViewName, new Guid(vPrimKey)))
                {
                    vFileStream.Position = 0;
                    vZipArchive.AddEntry(vPath, vFileStream);  
                }  
            }
            else
            {
                throw new Exception("Select 'FileName' and 'PrimKey' fields in underlying DataSource");
            }  
        } //end loop
        pContext.Response.Clear();
        pContext.Response.BufferOutput = false;
        pContext.Response.ContentType = "application/zip";
        pContext.Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}.zip\"", vZipName));
        vZipArchive.Save(pContext.Response.OutputStream);
    }
    else
    {
        return;
    }
}

我还尝试使用ZipOutputStreamSharpCompress.dll库。

那么,我缺少什么让它起作用?或者它不可能?

0 个答案:

没有答案