I'm trying to decompress a stream, using a GZipStream and BinaryStream, but I'm failing.
Can you help me?
public static LicenseOwnerRoot GetLicenseFromStream(Stream stream)
{
using (BinaryReader br = new BinaryReader(stream))
{
string keyCrypto = br.ReadString();
string xmlCrypto = br.ReadString();
string key = Cryptography.Decrypt(keyCrypto);
string xml = Cryptography.Decrypt(key, xmlCrypto);
byte[] data = Encoding.UTF8.GetBytes(xml.ToCharArray());
using (MemoryStream ms = new MemoryStream(data))
{
using (GZipStream decompress = new GZipStream(ms, CompressionMode.Decompress))
{
xml = Encoding.UTF8.GetString(data);
LicenseOwnerRoot root = (LicenseOwnerRoot)Utility.XmlDeserialization(typeof(LicenseOwnerRoot), xml);
foreach (LicenseOwnerItem loi in root.Licenses)
loi.Root = root;
return root;
}
}
}
}
That xml is compressed and encrypted, so I have to decompress and then decrypt. When I try to read, throws one expections with this message: The magic number in GZip header is not correct. I tried so many times to fix that, but It's sounds workable. The question is: how I should use the 'usings' and if that way is right, or exists another way to do what I'm trying to do? I have to decompress before to use BinaryReader?
Actually, I have to do the inverse of this method:
public static void GenerateLicenseStream(string key, LicenseOwnerRoot root, Stream stream)
{
using (BinaryWriter sw = new BinaryWriter(stream))
{
string xml = Utility.XmlSerialization(root);
using (MemoryStream ms = new MemoryStream())
{
using (GZipStream compress = new GZipStream(ms, CompressionMode.Compress))
{
byte[] data = Encoding.UTF8.GetBytes(xml.ToCharArray());
compress.Write(data, 0, data.Length);
string keyCrypto = Cryptography.Encrypt(key);
string xmlCrypto = Cryptography.Encrypt(key, Encoding.UTF8.GetString(ms.ToArray()));
sw.Write(keyCrypto);
sw.Write(xmlCrypto);
}
}
}
}