serialization - XmlSerialize directly to GZipStream throws magic number exception on Decompression -


I am trying to sequence an object sequence to XML though my object is a normal list containing many records and is very consumable to either the causes serializer memory I tried to order directly to a GZipStream with the following code:

  dim formatter XmlSerializer = XmlSerializerFactory.GetSerializerForType (_type) _ms New MemoryStream As using (as) gzstream U Use as GZipStream (_ms, CompressionMode.Compress, it is true) _ms.Position = 0 FormatterkSerialize (GzStream, Obj) _ms.Position = 0 GzStreamkFlush () GzStreamkClose () End _ms.Position = 0 dim DecompressData (using byte (as the new GZipStream use as gzStream) _ms, CompressionMode.Decompress) ReDim decompressData (9000 - 1) 'the number does not matter, the data in your test sample small slow lane integer = gzStream.Read (decompressData, 0, decompressData.Length) to use in the end as the end   

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

When I first and then a different memory stream sequence that section like sec: ..

Dim formatter as XmlSerializer = XmlSerializerFactory.GetSerializerForType (_type) _ms as New MemoryStream () dim uc_fileBytes () byte dim uc_len as new MemoryStream () ) using formatter.Serialize (_ms101 as _ms101, obj) uc_fileBytes = _ms101.GetBuffer () uc_len = _ms101.Length end New GZipStream (_ms, CompressionMode.Compress, it is true) _ms.Position = 0 gzStream.Write as gzStream using access (uc_fileBytes, 0, uc_len) gzStream.Flush () gzStream.Close () finished as the use of use byte New GZipStream as gzStream dim decompressData () ( _ms, CompressionMode.Decompress) Using End End Usage as ReDim DecompressData (9000 - 1) Dim Lane Integer = gzStream.Read (decompressData, 0, decompressData.Length)

This Works fine without error but why does it fail when I directly order for GZipStream?

After

This is the cause of the problem, because GZipStream behaves differently in MemoryStream (obviously) writing it The time it does not handle paginated writes very well.

Comments