Search code examples
c#asp.net-mvcasp.net-web-apidotnetzipionic-zip

PushStreamContent and ionic.zip


My webapi method for zipping on the fly use this code

var result = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new PushStreamContent((stream, content, arg3) =>
                {
                    using (var zipEntry = new Ionic.Zip.ZipFile())
                    {
                        using (var ms = new MemoryStream())
                        {
                            _xmlRepository.GetInitialDataInXml(employee, ms);
                            zipEntry.AddEntry("content.xml", ms);
                            zipEntry.Save(stream); //process sleep on this line
                        }

                    }
                })
            };

            result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = "FromPC.zip"
            };
            result.Content.Headers.ContentType =
                new MediaTypeHeaderValue("application/octet-stream");


            return result;

I want to

1) take data from _xmlRepository.GetInitialDataInXml

2) zip data on the fly via Ionic.Zip

3) return zipped stream as output of my WebApi action

But on this line zipEntry.Save(stream); execution process stops and don't go to next line. And method don't return anything

So why it doesnt' return me file?


Solution

  • When using PushStreamContent, you would need to close the stream to signal that you are done writing to the stream.

    Remarks section in the documentation:
    http://msdn.microsoft.com/en-us/library/jj127066(v=vs.118).aspx