这可能是一个仅适用于特定区域的问题,对于社区中的其他人没有用处,但希望有人能帮助我。
我的了解
我有一个base64编码的ZIP文件,在一个XML元素内以字符串的形式存在。
文件如下所示:
<Document>
<Content vesion="1"><!-- BASE64 STRING ---></Content>
</Document>
我想要做什么
解码字符串,然后解压它。
我尝试过但失败了的方法
Decoded the base64 string and put it in a file with a zip extension
public string DecodeFrom64(string encodedData) { byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData); string returnValue = System.Text.Encoding.Unicode.GetString(encodedDataAsBytes); return returnValue; }
Tried to unzip the string with the function:
public static string DecompressString(string compressedText) { byte[] gZipBuffer = Convert.FromBase64String(compressedText); using (var memoryStream = new MemoryStream()) { int dataLength = BitConverter.ToInt32(gZipBuffer, 0); memoryStream.Write(gZipBuffer, 4, gZipBuffer.Length - 4); var buffer = new byte[dataLength]; memoryStream.Position = 0; using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress)) { gZipStream.Read(buffer, 0, buffer.Length); } return Encoding.UTF8.GetString(buffer); } }
收到错误信息:
GZip头中的Magic Number不正确。请确保您正在传递一个GZip流...
Tried to decompress the string with the function:
public static string UnZipStr(string compressedText) { byte[] input = Convert.FromBase64String(compressedText); using (MemoryStream inputStream = new MemoryStream(input)) { using (DeflateStream gzip = new DeflateStream(inputStream, CompressionMode.Decompress)) { using (StreamReader reader = new StreamReader(gzip, System.Text.Encoding.UTF8)) { return reader.ReadToEnd(); } } } }
出现错误:
块长度与其补码不匹配...
我已向发送此XML数据给我的客户的人发送了一封邮件,但问题在于他们很慢回复(需要3-4周)。
因此,我希望有人能够指引我正确的方向。
我无法将文件附加到问题中,所以如果有人想查看它,我可以发送电子邮件或其他方式?