解压缩流

本文关键字:解压缩 | 更新日期: 2023-09-27 18:00:12

我试图使用GZipStream和BinaryStream来解压缩流,但我失败了。

你能帮我吗?

    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);
                }
            }
        }
   } 

解压缩流

我为您编写了一个快速示例,它不进行加密,但突出显示了加密/解密应该发生的位置。这是.NET控制台应用程序的内容,您可以"按原样"运行:

    static void Main(string[] args)
    {
        var content = @"someTextOrXMLContentGoesHereCanBeAnything#$&%&*(@#$^";
        var data = Encoding.UTF8.GetBytes(content.ToCharArray());
        var fs = new StreamWriter(@"c:'users'stackoverflow'desktop'sample.bin");
        using (var bw = new BinaryWriter(fs.BaseStream))
        {
            using (var ms = new MemoryStream())
            {
                using (var compress = new GZipStream(ms, CompressionMode.Compress, true))
                {
                    compress.Write(data, 0, data.Length);
                }
                // encrypt goes here
                var compressedData = ms.ToArray();
                Console.WriteLine(compressedData.Length); // 179
                bw.Write(compressedData);
            }
        }
        // and the reverse...
        using (var fs2 = new StreamReader(@"c:'users'stackoverflow'desktop'sample.bin"))
        {
            using (var br = new BinaryReader(fs2.BaseStream))
            {
                var len = (int)br.BaseStream.Length;
                var encrypted = br.ReadBytes(len);
                // decrypt here
                var decrypted = encrypted; // <== new result after decryption
                using (var ms = new MemoryStream(decrypted))
                {
                    List<byte> bytesList = new List<byte>();
                    using (var decompress = new GZipStream(ms, CompressionMode.Decompress, true))
                    {
                        int val = decompress.ReadByte();
                        while (val > -1)
                        {
                            bytesList.Add((byte)val);
                            val = decompress.ReadByte();
                        }  
                    }
                    var final_result = new String(Encoding.UTF8.GetChars(bytesList.ToArray()));
                    Console.WriteLine(final_result);
                }
            }
        }
        Console.ReadLine();
    }

您没有从"解压"中读取任何内容。您需要从"解压"中读取所有数据(因为存储的数据没有长度,所以您必须读取,直到流为空),然后像现在这样将其转换为字符串。

您将压缩数据视为utf-8字节数组。Utf-8实际上有非常严格的规则,所以在这一步中,一半的压缩数据可能会被问号(无效字符的占位符)取代。

您需要加密/解密原始二进制数据,并丢失字符串转换。压缩数据不是字符串,不应被视为字符串。

如果您的加密方法只能对字符串进行操作(我没有Cryptography类的定义),那么您别无选择,只能先加密XML数据,然后压缩它(尽管它可能不会以这种方式压缩)。

你实际上也没有进行任何解压缩;您为压缩的数据构造了一个MemoryStreamGZipStream,但不使用它们,而是尝试直接使用data