Showing posts with label IsolatedStorage. Show all posts
Showing posts with label IsolatedStorage. Show all posts

WP7 - Download zip file, unzip and save to isolated storage


  1. Create New "Windows Phone Application".
  2. Download "Silverlight SharpZipLib" from http://slsharpziplib.codeplex.com/releases/view/50561.
  3. Unpack "Silverlight SharpZipLib" and copy Bin/Release/SharpZipLib.WindowsPhone7.dll
  4. Create new folder "SharpZip" and past into copied SharpZipLib.WindowsPhone7.dll.
  5. Right click on project and "Add Reference...".
  6. Choose from "Browse" tab SharpZipLib.WindowsPhone7.dll from your project location.
  7. Add to "MainPage.xaml.cs" (probably you need to remove "using System.Windows.Shapes" because conflict System.Windows.Shapes.Path vs System.IO.Path):
    using System.IO;
    using System.IO.IsolatedStorage;
    using ICSharpCode.SharpZipLib.Zip;
    using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
    
  8. Add code for download file from web:
    public MainPage()
    {
        InitializeComponent();
    
        Loaded += new RoutedEventHandler(MainPage_Loaded);
    }
    
    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        System.Uri targetUri = new System.Uri("http://MyDomain/Archive.zip");
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(targetUri);
        //create asynchronous tast and declare callback to get data stream
        request.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), request);
    }
    
  9. And add function "ReadWebRequestCallback" to unzip & save file we got:
    private void ReadWebRequestCallback(IAsyncResult callbackResult)
    {
        HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
        HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(callbackResult);
        using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
        {
            //open isolated storage to save files
            using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (ZipInputStream s = new ZipInputStream(httpwebStreamReader.BaseStream))
                {
                    //s.Password = "123456";//if archive is encrypted
                    ZipEntry theEntry;
                    try
                    {
                        while ((theEntry = s.GetNextEntry()) != null)
                        {
                            string directoryName = Path.GetDirectoryName(theEntry.Name);
                            string fileName = Path.GetFileName(theEntry.Name);
    
                            // create directory
                            if (directoryName.Length > 0)
                            {
                                Directory.CreateDirectory(directoryName);
                            }
    
                            if (fileName != String.Empty)
                            {
                                //save file to isolated storage
                                using (BinaryWriter streamWriter = 
                                        new BinaryWriter(new IsolatedStorageFileStream(theEntry.Name,
                                            FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write, isoStore)))
                                {
    
                                    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 (ZipException ze)
                    {
                        Debug.WriteLine(ze.Message);
                    }
                }
            }
    
        }
    }
    

  10. Run your application and enjoy.

WP7 - Load local HTML files and all related files (js, css) to WebBrowser

Agenda : we have several files html, css, js interconnected and we would start our web application in WebBrowser.
0. Create new project "SilverlightWebBrowser"
1. In the project navigator will create a new folder and name "HTML".
2. Copy all our files in that folder (if you want include image files don't forget change "Build Action" property of the images to "Content" from "Resource").


3. Add to MainPage.xaml code for show WebBrowser:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="0,0,0,0">
    <phone:WebBrowser IsScriptEnabled="True" Name="webBrowser1" Width="450" Height="548"/>
</Grid>

4. MainPage.xaml.cs

#define DEBUG

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Resources;
using Microsoft.Phone.Controls;

namespace SilverlightWebBrowser
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            SaveFilesInHTMLFolderToIsoStore();

            webBrowser1.Navigate(new Uri("HTML/Index.html", UriKind.Relative));

        }

        private static void SaveFilesInHTMLFolderToIsoStore()
        {
#if DEBUG
            // This deletes all existing files in IsolatedStorage - Useful in testing
            // In live should not do this, but only load files once - this speeds subsequent loading of the app
            using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                isoStore.Remove();
            }
#endif
            string[] files = AllFilesInHTMLFolder();

            using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                Debug.WriteLine("check for exist " + files[0]);
                if (!isoStore.FileExists(files[0]))
                {
                    foreach (string f in files)
                    {
                        Debug.WriteLine("copy to isolated storage " + f.ToString());
                        StreamResourceInfo sr = Application.GetResourceStream(new Uri(f, UriKind.Relative));

                        // T4 Template includes all files in source folder(s). This may include some which are not in the project
                        if (sr != null)
                        {
                            using (BinaryReader br = new BinaryReader(sr.Stream))
                            {
                                byte[] data = br.ReadBytes((int)sr.Stream.Length);
                                SaveFileToIsoStore(f, data);
                            }
                        }
                    }
                }
            }
        }

        private static void SaveFileToIsoStore(string fileName, byte[] data)
        {
            string strBaseDir = string.Empty;
            const string DelimStr = "/";
            char[] delimiter = DelimStr.ToCharArray();
            string[] dirsPath = fileName.Split(delimiter);

            // Get the IsoStore
            using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                // Recreate the directory structure
                for (int i = 0; i < dirsPath.Length - 1; i++)
                {
                    strBaseDir = Path.Combine(strBaseDir, dirsPath[i]);
                    isoStore.CreateDirectory(strBaseDir);
                }

                // Create the file if not exists
                // or override if exist
                using (BinaryWriter bw = new BinaryWriter(new IsolatedStorageFileStream(fileName,
                        FileMode.Create, FileAccess.Write, FileShare.Write, isoStore)))
                {
                    bw.Write(data);
                }
            }
        }



        //generated from MainPage.xaml.tt -> Run Custom Tool
        private static string[] AllFilesInHTMLFolder()
        {
            return new[] {
                           "HTML/index.css",
                           "HTML/Index.html",
                           "HTML/inner.js",
                        };
        }
    
    }
}

5. And then run emulator
6. Now We see our page with our css and js
7. But if we don't know in advance a list of files or it's large enough that would get lost, you can use preprocessed text template.

MainPage.xaml.tt

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".gen.cs" #>
<#@ import namespace="System.IO"#>
// <auto-generated />
using Microsoft.Phone.Controls;

namespace SilverlightWebBrowser
{
    public partial class MainPage : PhoneApplicationPage
    {
        private static string[] AllFilesInHTMLFolder()
        {
            return new[] {
<#
            DirectoryInfo directoryInfo = new DirectoryInfo(Path.Combine(Path.GetDirectoryName(Host.TemplateFile), "HTML"));

            foreach(FileInfo file in directoryInfo.GetFiles("*.*", SearchOption.AllDirectories))
            {
    if (!file.FullName.Contains(@"\."))
    {#>
                           "<#= file.FullName.Substring(file.FullName.IndexOf("HTML")).Replace(@"\", "/") #>",
<#           }
   }
#>
                        };
        }
    }
}

8. Copy file MainPage.xaml.tt into project
    -> select file in navigation project
    -> right click -> Run Custom Tools
    -> open generated file MainPage.xaml.gen.cs
    -> we see our function AllFilesInHTMLFolder() with all exist files
    -> if you want to see all code in one place as copy/paste to MainPage.xaml.cs and delete MainPage.xaml.gen.cs. If no as go to 10.
10. It's all.