how to streamWrite a .zip/epub file from response to isolatedStorage if that .zip/hpub file contains .jpg, .woff (web open font format)

StackOverflow https://stackoverflow.com/questions/18567176

Question

Actually i am trying to saving a .zip/.epub file from webResponse into my wp8 isolated storage which contains saveral .html, .png, .jpg and .woff ( Web Open Font Format) files.

my following code is saving only .html and .png files from the .zip/.epub, when it try to save .jpg and .woff format files it shows following exception.

Exception: Operation not permitted on IsolatedStorageFileStream BinaryWritter

Please help me to solve that how to streamWrite myFont.woff file ?

my code is following.

    private async void ReadWebRequestCallback(IAsyncResult ar)
    {
        IsolatedStorageFile myIsoStorage = IsolatedStorageFile.GetUserStoreForApplication();
        string dir = "/mainDir/subDir/subtosubDir";
        if (!myIsoStorage.DirectoryExists(dir))
        {
            myIsoStorage.CreateDirectory(dir);
        }

        HttpWebRequest myRequest = (HttpWebRequest)ar.AsyncState;
        HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(ar);
        using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
        {
                using (ZipInputStream s = new ZipInputStream(httpwebStreamReader.BaseStream))
                {
                    ZipEntry theEntry;
                    try
                    {
                        while ((theEntry = s.GetNextEntry()) != null)
                        {
                            if (theEntry.IsDirectory) 
                            { 
                                string strNewDirectory = dir+"/"+theEntry.Name; 
                                if (!myIsoStorage.DirectoryExists(strNewDirectory)) 
                                {
                                    myIsoStorage.CreateDirectory(strNewDirectory); 
                                } 
                            }
                           else if (theEntry.IsFile)
                           {
                               string fileName = dir + "/" + theEntry.Name;
                               //save file to isolated storage
                               using (BinaryWriter streamWriter = new BinaryWriter(new IsolatedStorageFileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite, myIsoStorage)))
                                {
                                    int size = 2048;
                                    byte[] data = new byte[2048];
                                    while (true)
                                    {
                                        size = s.Read(data, 0, data.Length);
                                        if (size > 0)
                                        {
                                            streamWriter.Write(data, 0, size);
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                }

                            }
                        }
                    }
                    catch (Exception ze)
                    {
                        Debug.WriteLine(ze.Message);
                    }
                }

        }
    }
Was it helpful?

Solution

using (IsolatedStorageFileStream fileStream = isf.OpenFile(fullFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
{
     s.CopyTo(fileStream);
}

In my case it is the only way to save different extension's files into the isolated Storage, BinaryWriter will not work. so @KooKiz was right.

Thanks Kookiz :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top