Skip to main content
 首页 » 编程设计

c#之解码 Base64 和 Inflate Zlib 压缩的 XML

2024年02月13日14softidea

很抱歉这篇文章很长,会尽量缩短。

我正在使用一个返回如下内容的 json API(当然它的文档为零):

{ 
uncompressedlength: 743637, 
compressedlength: 234532, 
compresseddata: "lkhfdsbjhfgdsfgjhsgfjgsdkjhfgj" 
} 

数据(在本例中为 xml)被压缩,然后是我试图提取的 base64 编码数据。我所拥有的只是他们用 perl 编写的演示代码来对其进行解码:

use Compress::Zlib qw(uncompress); 
use MIME::Base64  qw(decode_base64); 
my $uncompresseddata = uncompress(decode_base64($compresseddata)); 

看起来很简单。

我尝试了多种方法来解码 base64:

 private string DecodeFromBase64(string encodedData) 
    { 
        byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData); 
        string returnValue = System.Text.Encoding.Unicode.GetString(encodedDataAsBytes); 
        return returnValue; 
    } 
 
    public string base64Decode(string data) 
    { 
        try 
        { 
            System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding(); 
            System.Text.Decoder utf8Decode = encoder.GetDecoder(); 
 
            byte[] todecode_byte = Convert.FromBase64String(data); 
            int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length); 
            char[] decoded_char = new char[charCount]; 
            utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0); 
            string result = new String(decoded_char); 
            return result; 
        } 
        catch (Exception e) 
        { 
            throw new Exception("Error in base64Decode" + e.Message); 
        } 
    } 

而且我已经尝试使用 Ionic.Zip.dll(DotNetZip?)和 zlib.net 来扩大 Zlib 压缩。但是一切都出错了。我正在尝试找出问题的根源。是base64解码还是Inflate?

我在使用 zlib 膨胀时总是遇到错误:我在使用 zlib.net 时遇到错误的魔数(Magic Number)错误,在使用 DotNetZip 时我得到“错误状态(无效的存储 block 长度)”:

string decoded = DecodeFromBase64(compresseddata); 
string decompressed = UnZipStr(GetBytes(decoded)); 
 
 
 public static string UnZipStr(byte[] input) 
    { 
 
        using (MemoryStream inputStream = new MemoryStream(input)) 
        { 
            using (Ionic.Zlib.DeflateStream zip = 
              new Ionic.Zlib.DeflateStream(inputStream, Ionic.Zlib.CompressionMode.Decompress)) 
            { 
                using (StreamReader reader = 
                  new StreamReader(zip, System.Text.Encoding.UTF8)) 
                { 
                    return reader.ReadToEnd(); 
                } 
            } 
        } 
    } 

读完后: http://george.chiramattel.com/blog/2007/09/deflatestream-block-length-does-not-match.html 并听取了其中一条评论。我将代码更改为:

MemoryStream memStream = new MemoryStream(Convert.FromBase64String(compresseddata)); 
memStream.ReadByte(); 
memStream.ReadByte(); 
DeflateStream deflate = new DeflateStream(memStream, CompressionMode.Decompress); 
string doc = new StreamReader(deflate, System.Text.Encoding.UTF8).ReadToEnd(); 

而且它工作正常。

请您参考如下方法:

这是罪魁祸首:

http://george.chiramattel.com/blog/2007/09/deflatestream-block-length-does-not-match.html

通过跳过前两个字节,我能够将其简化为:

MemoryStream memStream = new MemoryStream(Convert.FromBase64String(compresseddata)); 
memStream.ReadByte(); 
memStream.ReadByte(); 
DeflateStream deflate = new DeflateStream(memStream, CompressionMode.Decompress); 
string doc = new StreamReader(deflate, System.Text.Encoding.UTF8).ReadToEnd();