I have an XDocument which is large. I'm trying to stream this out somewhere:
public StreamResponse(Func<System.IO.Stream> source)
So to do this, I've done:
Stream stream = new MemoryStream(); // Create a stream
xmlDocument.Save(stream); // Save XDocument into the stream
stream.Position = 0;
return new StreamResponse(() => stream);
and that works fine.
Now, is it possible to change this so that I stream out a ZIP of the memory stream.
like this => XDocument => MemoryStream => ZIP Stream => stream-end-point ?
Stream stream = new MemoryStream(); // Create a stream
Stream compressed = new System.IO.Compression.GZipStream(stream, System.IO.Compression.CompressionLevel.Optimal);
xmlDocument.Save(compressed); // Save XDocument into the stream
compressed.Position = 0;
return new StreamResponse(() => stream);
Documentation: GZipStream
Yes, Use DotNetZip Pass your Memory Stream to the save method. Something like this:
using (ZipFile zip = new ZipFile())
{
// add this map file into the "images" directory in the zip archive
zip.AddFile("c:\\images\\personal\\7440-N49th.png", "images");
// add the report into a different directory in the archive
zip.AddFile("c:\\Reports\\2008-Regional-Sales-Report.pdf", "files");
zip.AddFile("ReadMe.txt");
zip.Save(myStream);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With