XmlSerialize 直接到 GZipStream 在解压缩时抛出幻数异常

发布于 2024-10-27 02:23:49 字数 1822 浏览 1 评论 0原文

我正在尝试将一个对象序列化为 XML,但是我的对象是一个包含许多记录的通用列表,并导致序列化程序消耗大量内存。因此,我尝试使用以下代码直接序列化为 GZipStream:

Dim formatter As XmlSerializer = XmlSerializerFactory.GetSerializerForType(_type)
Using _ms As New MemoryStream()
    Using gzStream As New GZipStream(_ms, CompressionMode.Compress, True)

        _ms.Position = 0
        formatter.Serialize(gzStream, obj)
        _ms.Position = 0
        gzStream.Flush()
        gzStream.Close()
    End Using

    _ms.Position = 0
    Dim decompressData() As Byte
    Using gzStream As New GZipStream(_ms, CompressionMode.Decompress)
        ReDim decompressData(9000 - 1) 'this number doesn't matter, the data in my test sample is small
        Dim Len As Integer = gzStream.Read(decompressData, 0, decompressData.Length)
    End Using
End Using

但是,我遇到了 InvalidDataException GZip 标头中的幻数不正确。当尝试将数据读入 decompressData 数组时,请确保传入 GZip 流。

当我首先序列化到单独的内存流,然后压缩该流时,例如:

Dim formatter As XmlSerializer = XmlSerializerFactory.GetSerializerForType(_type)
Using _ms As New MemoryStream()
    Dim uc_fileBytes() As Byte
    Dim uc_len As Integer
    Using _ms101 As New MemoryStream()
        formatter.Serialize(_ms101, obj)
        uc_fileBytes = _ms101.GetBuffer()
        uc_len = _ms101.Length
    End Using
    Using gzStream As New GZipStream(_ms, CompressionMode.Compress, True)
        _ms.Position = 0
        gzStream.Write(uc_fileBytes, 0, uc_len)
        gzStream.Flush()
        gzStream.Close()
    End Using

    Dim decompressData() As Byte
    Using gzStream As New GZipStream(_ms, CompressionMode.Decompress)
        ReDim decompressData(9000 - 1)
        Dim Len As Integer = gzStream.Read(decompressData, 0, decompressData.Length)
    End Using
End Using

它工作正常,没有错误。但是为什么当我直接序列化到 GZipStream 时会失败?

I am trying to Serialize an object to XML however my object is a generic list containing many records and causes the serializer to consume lots of memory. So I tried to serialize directly to a GZipStream with the following code:

Dim formatter As XmlSerializer = XmlSerializerFactory.GetSerializerForType(_type)
Using _ms As New MemoryStream()
    Using gzStream As New GZipStream(_ms, CompressionMode.Compress, True)

        _ms.Position = 0
        formatter.Serialize(gzStream, obj)
        _ms.Position = 0
        gzStream.Flush()
        gzStream.Close()
    End Using

    _ms.Position = 0
    Dim decompressData() As Byte
    Using gzStream As New GZipStream(_ms, CompressionMode.Decompress)
        ReDim decompressData(9000 - 1) 'this number doesn't matter, the data in my test sample is small
        Dim Len As Integer = gzStream.Read(decompressData, 0, decompressData.Length)
    End Using
End Using

However I run into an InvalidDataException The magic number in GZip header is not correct. Make sure you are passing in a GZip stream. when trying to read the data into the decompressData array.

When I Serialize to a separate memory stream first and then compress that stream such as:

Dim formatter As XmlSerializer = XmlSerializerFactory.GetSerializerForType(_type)
Using _ms As New MemoryStream()
    Dim uc_fileBytes() As Byte
    Dim uc_len As Integer
    Using _ms101 As New MemoryStream()
        formatter.Serialize(_ms101, obj)
        uc_fileBytes = _ms101.GetBuffer()
        uc_len = _ms101.Length
    End Using
    Using gzStream As New GZipStream(_ms, CompressionMode.Compress, True)
        _ms.Position = 0
        gzStream.Write(uc_fileBytes, 0, uc_len)
        gzStream.Flush()
        gzStream.Close()
    End Using

    Dim decompressData() As Byte
    Using gzStream As New GZipStream(_ms, CompressionMode.Decompress)
        ReDim decompressData(9000 - 1)
        Dim Len As Integer = gzStream.Read(decompressData, 0, decompressData.Length)
    End Using
End Using

It works fine without error. But why does it fail when I serialize directly to the GZipStream?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏末的微笑 2024-11-03 02:23:49

问题的原因是 GZipStream 在写入时的行为与 MemoryStream 不同(显然)。它不能很好地处理分页写入。

The cause of the problem is because the GZipStream behaves differently (obviously) to the MemoryStream when writing to it. It doesn't handle paged writes very well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文