如果.zip / hpub文件包含.jpg,.woff(web开放字体格式),如何从响应streamStorage响应.zip / epub文件

时间:2013-09-02 06:51:41

标签: windows-phone-8 isolatedstorage epub sharpziplib streamwriter.write

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

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

异常:IsolatedStorageFileStream BinaryWritter上不允许操作

请帮我解决一下如何streamWrite 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);
                    }
                }

        }
    }

1 个答案:

答案 0 :(得分:0)

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

在我的情况下,这是将不同扩展名的文件保存到隔离存储中的唯一方法,BinaryWriter将无法正常工作。所以@KooKiz是对的。

谢谢Kookiz:)