Add excel memory stream to zip

Hi guys,

I created the excel workbook and saved it as MemoryStream, after I would like to archive the stream to zip stream and return byte array of zip file.

Could you tell me how to do it?

Thanks,
Alex

4 Replies

AK Alexander Kurnevich November 1, 2017 02:57 PM UTC

Hi guys,

I achieved this with following way: 


    private static MemoryStream CreateArchive(Stream ms)

    {

        using (var zipMemoryStream = new MemoryStream())

        {

            var zipArchive = new ZipArchive

            {

                DefaultCompressionLevel = CompressionLevel.Best,

            };

            ms.Position = 0;

            zipArchive.AddItem("Excel.xlsx", ms, false, FileAttributes.Normal);

            zipArchive.Save(zipMemoryStream, false);


                return zipMemoryStream;

            }

        }

Not sure if it's right.

Thanks,
Alex






MC Mohan Chandran Syncfusion Team November 2, 2017 09:25 AM UTC

Hi Alexander, 
  
Yes, your code snippets are correct. Also, you can get the byte array from stream using MemoryStream.ToArray() method. Please refer the following code sample for the same. 
 
Code Sample : 
 
byte[] zipBytes = CreateArchive(stream).ToArray(); 
 
 Please let us know if you have any concern. 
 
Regards, 
Mohan Chandran. 



AK Alexander Kurnevich November 2, 2017 10:39 AM UTC

Yes, sure. 

Thanks,

Alex



MC Mohan Chandran Syncfusion Team November 3, 2017 05:19 AM UTC

Hi Alexander, 
  
We are glad that the requirement is achieved at your end. Please let us know if you have any other queries. 
  
Regards, 
Mohan Chandran. 


Loader.
Up arrow icon