PCL Create Zip File from Base64String with PCLStorage & ICSharpCode.SharpZipLib.Zip

FranoisGrzybowskiFranoisGrzybowski FRMember

Hi all,
I'm trying to use two nugets (PCLStorage and SharpZipLib) to achieve this :
I got a webservice that gives me the base64string of a .zip file. In Xamarin I have a PCL and i retrieve this string from the webservice and now I wan't to decode it, turn it back to a .zip file and save it to my device (then i'll unzip it afterwards). I'm mostly based on an example that does kind of the same thing but not exactly. In the code of the topic i follow to achieve my thing the guy is creating a text file then zip it and save it to the device. I have some difficulties to understand what parts of his code I can/can't get rid of.
http://stackoverflow.com/questions/35474724/corrupted-zip-when-passing-sharpziplib-memorystream-to-pclstorage
I'll give my code and his code so people can understand what goes wrong in mine and might help me.

Here's my code :

private async void DecodeBase64ToZipFile(string base64str)
{
    IFile file;
    try
    {
        string content = base64str.Substring(1, base64str.Length - 1);
        byte[] contentAsBytes = System.Convert.FromBase64String(content);

        using (MemoryStream contentStream = new MemoryStream())
        {
            await contentStream.WriteAsync(contentAsBytes, 0, contentAsBytes.Length);
            contentStream.Position = 0;

            file = await worldSubFolder.CreateFileAsync("world.zip", CreationCollisionOption.ReplaceExisting);
            using (Stream fileHandler = await file.OpenAsync(FileAccess.ReadAndWrite))
            {
                contentStream.Position = 0;
                await contentStream.CopyToAsync(fileHandler);
            }
        }
        Debug.WriteLine(Messages.FM_ZIPFILE_CREATION_SUCCESS_MESSAGE);
        Debug.WriteLine(">>> FILE PATH: " + file.Path);
    } catch (Exception e) {
        Debug.WriteLine(Messages.FM_ZIPFILE_CREATION_ERROR_MESSAGE + e.Message);
    }
}

The error I get when I run the code is this :
">> Zip file creation failure: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters."

Thanks to any of those who can help me

Sign In or Register to comment.