11

我正在尝试序列化和压缩 WPF FlowDocument,然后执行反向操作 - 解压缩字节数组并反序列化以重新创建 FlowDocument - 使用 .NETGZipStream类。我正在遵循 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

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

(请忽略“压缩”字节数组大于“未压缩”字节数组的事实——我通常会处理更大的流文档)

4

3 回答 3

15

您需要在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();
}

这可行,您甚至可以删除usingfor ,因为除非您使用允许您指定底层流应保持打开状态的构造函数重载,否则MemoryStream它将由 处置。GZipStream这意味着您使用该代码调用ToArray已处置的流,但这是允许的,因为字节仍然可用,这使得处置内存流有点奇怪,但如果您不这样做,FXCop 会惹恼您。

于 2012-08-11T18:47:49.973 回答
4

Joao 的回答成功了。我已经复制了下面的完整工作示例。我在 output 中添加了一行compressedData.Length。有趣的是,它输出了 218 个字节,而compressedStream.Length只输出了 202 个字节。如果您在读取字节数组之前不关闭 GZipStream,compressedData.Length则为 202。我不确定为什么关闭 GZipStream 会给您额外的 16 个字节。

var flowDocumentIn = new FlowDocument();
flowDocumentIn.Blocks.Add(new Paragraph(new Run("Hello")));

Debug.WriteLine("Compress");

byte[] compressedData;

using (var uncompressedStream = new MemoryStream())
{
    XamlWriter.Save(flowDocumentIn, uncompressedStream);
    uncompressedStream.Position = 0;
    using (var compressedStream = new MemoryStream())
    {
        using (var gZipCompressor = new GZipStream(compressedStream, CompressionMode.Compress))
        {
            Debug.WriteLine(" uncompressedStream.Length: " + uncompressedStream.Length);
            uncompressedStream.CopyTo(gZipCompressor);
            Debug.WriteLine(" compressedStream.Length: " + compressedStream.Length);
        }
        compressedData = compressedStream.ToArray();
    }
}

Debug.WriteLine(" compressedData.Length: " + compressedData.Length);

Debug.WriteLine("Decompress");

FlowDocument flowDocumentOut;

using (var compressedStream = new MemoryStream(compressedData))
using (var uncompressedStream = new MemoryStream())
{
    using (var gZipDecompressor = new GZipStream(compressedStream, CompressionMode.Decompress))
    {
        Debug.WriteLine(" compressedStream.Length: " + compressedStream.Length);
        gZipDecompressor.CopyTo(uncompressedStream);
        Debug.WriteLine(" uncompressedStream.Length: " + uncompressedStream.Length);
    }
    uncompressedStream.Position = 0;
    flowDocumentOut = (FlowDocument)XamlReader.Load(uncompressedStream);
}

调试输出:

Compress
 uncompressedStream.Length: 123
 compressedStream.Length: 202
 compressedData.Length: 218
Decompress
 compressedStream.Length: 218
 uncompressedStream.Length: 123

uncompressedStream.Position = 0;还要注意调用之前的附加信息XamlReader.Load

于 2012-08-11T20:59:59.113 回答
0

将解压缩的字节复制到流后,需要将其位置设置为零,以便正确读取

于 2015-12-23T12:59:21.257 回答