0

I'm using DotNetZip library to make multi-file zip archive and download it on the fly (no need to wait for download to start). However I can't make it to download instantly. From browser dev console, in network tab I noticed that first zip file is "transferred" and after being fully transferred it starts downloading.

Here is code fragment:

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;
    }
}

I also tried using ZipOutputStream and SharpCompress.dll library.

So, what I am missing to make it work? Or its impossible?

Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
  • `no need to wait for download to start` - what do you mean? – Mat J Feb 26 '16 at 06:59
  • I mean after pressing button "download" in 3-5 seconds zip starts to download. – Marius Balandis Feb 26 '16 at 07:07
  • So, What's your problem? it is not downloading at all? – Mat J Feb 26 '16 at 07:37
  • Its downloading, but after a while. Zip starts downloading after he's completely created - all files are fully streamed to it. I need that zip starts downloading as soon as possible and files will be streamed while downloading is still active. It should work like download stream. Now it depends on total size of zip: the bigger zip and the slower net, the time before zip starts downloading is longer. e.g 6.9mb zip starts downloading after 10secs but 119mb zip starts downloading after 1min. What I need is that the zip always starts downloading in less then ~5 secs – Marius Balandis Feb 26 '16 at 07:45

0 Answers0