Saturday, 16 August 2014

gzipstream truncating my xml



I am attempting to serialize my powershell objects and send them over a tcp connection. i have a wonderful convertfrom-xml script obtained from here that seems to work: http://ift.tt/1m59ov6


In order to keept he size of my packets down, and perhaps provide just a teensy bit of security by obscurity, I am attempting to compress the objects while they travel, using gzipstream. i've also tried deflatestream. neither of them seem to be working for me.



$o = ""|select x,y,z,a
$o.x = 3
$o.y = "hello"
$o.z = [datetime]::now
$o.a = 1..9
$xml = $o | convertto-xml
$mem = new-object system.io.memorystream
$gz = new-object system.io.compression.gzipstream($mem, [system.io.compression.compressionmode]::compress)
$xml.save($gz)
$bytes = $mem.toarray()
$gz.close()
$mem.close()


This part successfully gives me an array of bytes that seem compressed.



$mem = new-object system.io.memorystream(,$bytes)
$gz = new-object system.io.compression.gzipstream($mem, [system.io.compression.compressionmode]::decompress)
$gz.flush()
$xml = new-object xml
$xml.load($gz)
$o = $xml | convertfrom-xml
$gz.close()
$mem.close()


this part fails on the line $xml.load($gz), saying the Root element is missing. I thus try and read from $gz directly, and get a -1 to begin with (which i thought meant the end of the stream?), and then immediately after the first -1, i get the following data:



<?xml version="1.0"?>
<Objects>
<Object Type="System.Management.Automation.PSCustomObject">
<Property Name="x" Type="System.Int32">3</Property>
<Property Name="y" Type="System.String">hello</Property>
<Property Name="z" Type="System.DateTime">16/08/2014 11:43:48 PM</Property>
<Property Name="a" Type="System.Object[]">
<Property Type="System.Int32">1</Property>
<Property Type="System.Int32">2</Property>
<Property Type="System.Int32">3</Property>
<Property Type="System.Int32">4</Property>
<Property Type="System.Int32">5</Property>
<Property Type="System.Int32">6</Property>
<Property Type="System.Int32">7</Property>
<Property Type="System.Int32">8</Property>
<Property Type="System.Int32">9</Property>
</Property>
</Object>


i then tried a few other things to get it to work, including replacing gzipstream with deflatestream, flushing $gz, and reading from $gz to a third stream. None of them have helped with either the initial -1 or the truncating of my xml. what is causing these two issues?


No comments:

Post a Comment