【问题标题】:how to streamWrite a .zip/epub file from response to isolatedStorage if that .zip/hpub file contains .jpg, .woff (web open font format)如果 .zip/hpub 文件包含 .jpg、.woff (网络开放字体格式),如何从对 isolatedStorage 的响应中流式写入 .zip/epub 文件
【发布时间】:2013-09-02 06:51:41
【问题描述】:

实际上,我正在尝试将 .zip/.epub 文件从 webResponse 保存到我的 wp8 隔离存储中,其中包含 saveral .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);
                    }
                }

        }
    }

【问题讨论】:

  • 它不会解决你的问题,但请不要直接使用CopyTo 方法将一个流复制到另一个流中。所以只要创建IsolatedStorageFileStream,那么你应该可以直接使用s.CopyTo(yourIsolatedStorageFileStream),完全跳过二进制写入器
  • 嗨@KooKiz,感谢您的关注,我无法正确获取您的信息,请探索您的建议。谢谢。
  • @KooKiz 我已经成功完成了这项任务,我在底部写了三行答案。

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


【解决方案1】:
using (IsolatedStorageFileStream fileStream = isf.OpenFile(fullFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
{
     s.CopyTo(fileStream);
}

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

感谢 Kookiz :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-19
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多