Help with Compressed Data from Database

Sep 15, 2011 at 2:31 PM

Hi All,

I have an XML string that is compressed and stored in a database, can anyone point me in the right direction on how to decompress and get to the actual XML.
I have tried writing a few different solutions using the examples from the site but to no avail. It seems to be "zip" compressed due to the magic number header.
Here is the header from the string in the database below.


Any information would be greatly apperciated.

Ron Duquette


Sep 15, 2011 at 6:08 PM

you'll need to hex-decode that string.

Strip the first two characters.  Then, taking 2 characters at a time, convert that string to an array of bytes. Then, you can create a MemoryStream around that array, and use the ZipFile.Read() method to read in the zipfile from that MemoryStream.

// assume s is a string containing "0x504b03040390393039...."
var a = new byte[s.Length/2 -1];
for (int i=0; i+2 < s.Length; i+=2) 
    a[i/2] = Int32.Parse(s.Substring(i+2,2), NumberStyles.AllowHexSpecifier);

var ms = new MemoryStream(a);
using (var zip = ZipFile.Read(ms)) 
    ....// read zipfile here


Sep 15, 2011 at 7:11 PM

Worked like a charm. Thanks so much for your help.

Sep 15, 2011 at 11:18 PM

No charge.