c# – .NET GZipStream解压缩生成空流

前端之家收集整理的这篇文章主要介绍了c# – .NET GZipStream解压缩生成空流前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图序列化和压缩一个 WPF FlowDocument,然后反向解压缩字节数组并反序列化以重新创建FlowDocument – 使用.NET GZipStream类.我正在跟踪MSDN上描述的示例,我有以下测试程序:
var flowDocumentIn = new FlowDocument();
flowDocumentIn.Blocks.Add(new Paragraph(new Run("Hello")));
Debug.WriteLine("Compress");
byte[] compressedData;
using (var uncompressed = new MemoryStream())
{
    XamlWriter.Save(flowDocumentIn,uncompressed);
    uncompressed.Position = 0;
    using (var compressed = new MemoryStream())
    using (var compressor = new GZipStream(compressed,CompressionMode.Compress))
    {
        Debug.WriteLine(" uncompressed.Length: " + uncompressed.Length);
        uncompressed.CopyTo(compressor);
        Debug.WriteLine(" compressed.Length: " + compressed.Length);
        compressedData = compressed.ToArray();
    }
}

Debug.WriteLine("Decompress");
FlowDocument flowDocumentOut;
using (var compressed = new MemoryStream(compressedData))
using (var uncompressed = new MemoryStream())
using (var decompressor = new GZipStream(compressed,CompressionMode.Decompress))
{
    Debug.WriteLine(" compressed.Length: " + compressed.Length);
    decompressor.CopyTo(uncompressed);
    Debug.WriteLine(" uncompressed.Length: " + uncompressed.Length);
    flowDocumentOut = (FlowDocument) XamlReader.Load(uncompressed);
}

Assert.AreEqual(flowDocumentIn,flowDocumentOut);

然而,我在XamlReader.Load行中收到异常,这是正常的,因为调试输出告诉未压缩的流具有零长度.

Compress
 uncompressed.Length: 123
 compressed.Length: 202
Decompress
 compressed.Length: 202
 uncompressed.Length: 0

为什么最终的未压缩流不包含原始的123个字节?

(请忽略“压缩”字节数组大于“未压缩”字节数组的事实 – 我通常会使用更大的流程文档)

解决方法

在从内存流获取压缩字节之前,需要关闭GZipStream.在这种情况下,由于使用所调用的Dispose处理关闭.
using (var compressed = new MemoryStream())
{
    using (var compressor = new GZipStream(compressed,CompressionMode.Compress))
    {
        uncompressed.CopyTo(compressor);
    }
    // Get the compressed bytes only after closing the GZipStream
    compressedBytes = compressed.ToArray();
}

这可以工作,甚至可以删除MemoryStream的使用,因为它将由GZipStream处理,除非您使用构造函数重载,允许您指定底层流应该保持打开状态.这意味着与代码一样,您调用ToArray处理的流,但这是允许的,因为这些字节仍然可用,这使得处理内存流有点奇怪,但如果你不这样做,FXCop会让你烦恼.

猜你在找的C#相关文章