Dav*_* S. 2 c# compression stream azure gzipstream
我正在将文本写入System.IO.StreamWriter.
底层流(在 中指定new StreamWriter(underlyingStream))写入远程文件。(我认为它的类型不相关,但为了完整起见,我会提到它是 microsoft azure CloudBlobStream underlyingStream)。
现在,我想通过编写一个具有相同内容的附加压缩文件来扩展此功能,但在GZipOutputStream compressedUnderlyingStream和StreamWriter第二个CloudBlobStream.
我正在寻找一种方法将这两个CloudBlobStreams 指定为StreamWriter. 但我找不到办法。是否存在某种可以组合两个底层流的流类型?或者我该如何处理这个问题?注意我希望所有内容都保留为流,以最大限度地减少内存中的数据量。
//disregard lack of using statements for this example's sake
CloudBlobStream blobStream = blob.OpenWrite();
CloudBlobStream compressedBlobStream = compressedBlob.OpenWrite();
GZipOutputStream compressorStream = new GZipOutputStream(compressedBlobStream);
//I make the streamwriter here for the regular blob,
///but how could I make the same streamwriter also write to the compressedBlob at the same time?
TextWriter streamWriter = new StreamWriter(blobStream, Encoding.UTF8);
Run Code Online (Sandbox Code Playgroud)
我一时想不出一个,但自己写一下是很简单的:
class MultiStream : Stream
{
private List<Stream> streams;
public Streams(IEnumerable<Stream> streams)
{
this.streams = new List<Stream>(streams);
}
...
public override void Write(byte[] buffer, int offset, int count)
{
foreach(Stream stream in streams)
stream.Write(buffer, offset, count);
}
...
}
Run Code Online (Sandbox Code Playgroud)
编辑:感谢tenbits完成了上述类的实现。因为这既不是我的工作,也不是我亲自测试过的东西,所以我只提供了一个链接,并将保持原始答案不变。