Jester
Jester

Reputation: 3317

Decode base64 encrypted string

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)

Got the error:

The magic number in GZip header is not correct. Make sure you are passing in a GZip stream...

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?

Upvotes: 3

Views: 7151

Answers (1)

Akku
Akku

Reputation: 4454

As Harold already pointed out in the comments, this is all wrong. In your last comment (Jester) you clarified that the zip file was first converted to a string, and then that string got converted to a base64 string. As this absolutely makes no sense (why should you do that), I imagine that you got something wrong there and really mean that the file got converted to a base64 string. This is best practice for email for example, and I've recently been doing that to transfer files via XML in XMPP. My guess would be that...

byte[] file = System.Convert.FromBase64String(encodedData);
File.WriteAllBytes(directoryToWriteTo+filename+".zip", file);

... creates the file you're looking for. byte[] here already IS a zip file. As zip files can be messy to deal with (as you didn't really say what's in there), I would recommend saving these bytes to a file and try to open it with a zip-software like WinRar. If this worked and you can get the file contents out of the zip file, you could ask another question how to the the contents. I would also recommend using SharpZipLib.dll, because it's really the only solution I got working in reasonable time so far.

Upvotes: 5

Related Questions