0
votes

I have downloaded a zip file from blob storage and save it to isolated storage of windows phone like this :- FileStream fs is downloaded from blob.

public static void SaveToIsolatedStorage(FileStream fs, string fileName)
        {
            var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
            using (var streamWriter =
                            new StreamWriter(new IsolatedStorageFileStream(fileName,
                                FileMode.Create,
                                FileAccess.ReadWrite,
                                isolatedStorage)))
            {
                streamWriter.Write(fs);
            }
        }

But when checked this zip file using IsoStoreSpy it is showing corrupted. I have checked it by reading from isolated storage and tried to unzip it but not working. I am sure that it is corrupted because when i replace this file using IsoStoreSpy with some other zip and then tried to unzip it then it is working.

Edit:-

Code for downloading from Blob

private async Task DownloadFileFromBlobStorage()
    {
        var filename = "AppId_2.zip";
        var blobContainer = GetBlobClient.GetContainerReference("testwpclientiapcontainer");
        var blob = blobContainer.GetBlockBlobReference(filename);

        using (var filestream = new FileStream(filename, FileMode.Create))
        {
            await blob.DownloadToStreamAsync(filestream);

            SaveToIsolatedStorage(filestream, filename);
        }
    }

So anybody know how can i save the zip file to isolated storage without getting it corrupted ?

1

1 Answers

1
votes

You're using a StreamWriter. That's for text. You shouldn't be using it to copy a zip file at all. Never use any TextWriter for binary data.

Next you're using StreamWriter.Write(object), which is basically going to call ToString on the FileStream. That's not going to work either.

You should just create an IsolatedStorageStream, and then call fs.CopyTo(output).

public static void SaveToIsolatedStorage(Stream input, string fileName)
{
    using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
    {
        // Simpler than calling the IsolatedStorageFileStream constructor...
        using (var output = storage.CreateFile(fileName))
        {
            input.CopyTo(output);
        }
    }
}

In your edit you've shown code which saves to a FileStream first, and then copies the stream from the current position. As you've noted in comments, you needed to rewind it first.

Personally I wouldn't use a FileStream at all here - why do you want to save it as a normal file and an isolated file? Just use a MemoryStream:

using (var stream = new MemoryStream())
{
    await blob.DownloadToStreamAsync(filestream);
    stream.Position = 0;
    SaveToIsolatedStorage(stream, filename);
}

(Note that your SaveToIsolatedStorage method is still synchronous... you may wish to consider an asynchronous version.)