我正在尝试使用GZipStream和BinaryStream解压缩流,但是我失败了。你能帮助我吗?
那个XML文件是经过压缩和加密的,所以我需要先解压再解密。在我尝试读取时,会抛出一个带有以下消息的异常:GZip头中的魔数不正确。我已经尝试了很多次修复它,但看起来行不通。问题是:我应该如何使用“usings”,如果这种方式正确,还是存在其他方法可以实现我正在尝试做的事情?我必须在使用BinaryReader之前先解压缩吗?实际上,我必须执行与此方法相反的操作。
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;
}
}
}
}
那个XML文件是经过压缩和加密的,所以我需要先解压再解密。在我尝试读取时,会抛出一个带有以下消息的异常:GZip头中的魔数不正确。我已经尝试了很多次修复它,但看起来行不通。问题是:我应该如何使用“usings”,如果这种方式正确,还是存在其他方法可以实现我正在尝试做的事情?我必须在使用BinaryReader之前先解压缩吗?实际上,我必须执行与此方法相反的操作。
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);
}
}
}
}