我有一个很大的XDocument
。我正试着把这个输出到某个地方:
public StreamResponse(Func<System.IO.Stream> source)
要做到这一点,我做了:
Stream stream = new MemoryStream(); // Create a stream
xmlDocument.Save(stream); // Save XDocument into the stream
stream.Position = 0;
return new StreamResponse(() => stream);
,这很好。
现在,是否有可能改变这一点,以便我输出内存流的ZIP。
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);
文档:GZipStream
是的,使用DotNetZip将内存流传递给save方法。像这样:
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);
}
GZipStream在这里拯救你!
从MSDN:GZipStream类https://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream%28v=vs.110%29.aspx提供用于压缩和解压缩流的方法和属性。