I am trying to unzip a stream using GZipStream and BinaryStream, but I fail.
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; } } } }
This xml is compressed and encrypted, so I need to unzip and then decrypt. When I try to read, you throw one of the expectations with this message: The magic number in the GZip header is incorrect. I tried many times to fix it, but it sounds workable. The question is, how should I use the โhabitsโ, and if so, or is there another way to do what I'm trying to do? Should I unzip first to use BinaryReader?
Actually, I need to reverse 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); } } } }
source share