Tzipirig
Tzipirig

Reputation: 33

convert compress xmldocument as zip and get as byte array

I am building a XmlDocument in memory (I am not writing it to disk). I need to be able to create a zip archive that would contain the Xml file and then get the zip archive as byte array (all of this without actually writing/creating anything on the hard-disk). Is this possible?

I should mention that I am trying to do this in C#.

Upvotes: 0

Views: 874

Answers (1)

Henk Holterman
Henk Holterman

Reputation: 273179

var buffer = new MemoryStream();
using (buffer)
using (var zip = new ZipArchive(buffer, ZipArchiveMode.Create) )
{
    var entry = zip.CreateEntry("content.xml", CompressionLevel.Optimal);
    using (var stream = entry.Open())
    {
        xmlDoc.Save(stream);
    }                
}
var bytes = buffer.ToArray();

Upvotes: 2

Related Questions