3
\$\begingroup\$

I'm looking for ways to speed up the process of adding entries to a zip-archive, in a web app, using DotNetZip.

The problem currently is that the response times out before all files has been added to the archive. There can potentially be between 200 - 500 entries in each archive, each entry between 1 - 4 meg in size. I've tried a sample of 100 files which is too slow.

Compression is not necessary since all files are .mp3-files.

I currently have this code:

private readonly IDictionary<string, Stream> _files = new Dictionary<string, Stream>();

public bool CreateZip(ref MemoryStream result, bool compress = true)
{
    using (ZipFile zip = new ZipFile())
    {
        zip.UseZip64WhenSaving = Zip64Option.AsNecessary;

        if (!compress)
            zip.CompressionLevel = CompressionLevel.None;

        if (_files.Any())
        {
            foreach (var file in _files)
            {
                ZipEntry entry = zip.AddEntry(file.Key, file.Value);
                entry.Comment = file.Key;
            }
        }

        zip.Save(result);
        result.Seek(0, SeekOrigin.Begin);
        result.Flush();
    }
    return true;
}

The files are first added to a dictionary (as streams from a RavenFS/RavenFileStorage) and then added to the archive. The zip itself is also saved to a stream.

I thought about threading but it seems that is not supported by the lib when adding entries to the archive.

Am I using the lib correctly or is any method faster?

\$\endgroup\$
2
  • \$\begingroup\$ What does too slow mean? Does it run for 30 minutes? Two hours? What do you consider fast? Have you tried to create a zip with the same 100 files in windows to have some values to compare? \$\endgroup\$ Commented Aug 9, 2016 at 12:21
  • \$\begingroup\$ @t3chb0t When trying to zip 100 8 meg .mp3 files on my windows laptop takes aprox 30 seconds, the default timeout is 2 minutes on the IIS. I would consider anything below that 2 min very fast. Don't even know if it's possible though. \$\endgroup\$ Commented Aug 9, 2016 at 12:59

1 Answer 1

3
\$\begingroup\$
  • Don't use ref parameters. It's very rarely needed and it's of no use in your CreateZip method.
  • Save the ZipFile directly to the Response.OutputStream instead of a temporary MemoryStream. This will allow you to start streaming the file while you're creating it.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.