0

实际上,我正在尝试将 .zip/.epub 文件从 webResponse 保存到我的 wp8 隔离存储中,其中包含 saveral .html、.png、.jpg 和 .woff(Web 开放字体格式)文件。

我的以下代码仅保存 .zip/.epub 中的 .html 和 .png 文件,当它尝试保存 .jpg 和 .woff 格式文件时,它显示以下异常。

例外:IsolatedStorageFileStream BinaryWritter 上不允许操作

请帮我解决如何流式写入 myFont.woff 文件?

我的代码如下。

    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);
                    }
                }

        }
    }
4

1 回答 1

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

就我而言,这是将不同扩展名的文件保存到隔离存储中的唯一方法,BinaryWriter 将不起作用。所以@KooKiz 是对的。

谢谢库克兹 :)

于 2013-09-30T07:38:07.983 回答