This is probably a very localized question not useful for the rest of the community but hopefully someone can help me.
What I know
I have a base64 encoded ZIP, in a string, inside an XML element.
The file looks like this:
<Document>
<Content vesion="1"><!-- BASE64 STRING ---></Content>
</Document>
What I want to do
Decode the string, and then unzip it.
What I've tries so far (and failed)
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); } }
Got the error:
The magic number in GZip header is not correct. Make sure you are passing in a GZip stream...
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(); } } } }
Got the error:
Block length does not match with its complement...
I sent a mail to the guys that are sending this XML data to my customer, but the problem is that they are very slow to respond (3-4 weeks).
So I am hoping someone can point me in the right direction.
I can not append files to the question, so if someone wants have a look at it I can send a mail or something?
GZipStream
? – Sergey Kalinichenko