- Create New "Windows Phone Application".
- Download "Silverlight SharpZipLib" from http://slsharpziplib.codeplex.com/releases/view/50561.
- Unpack "Silverlight SharpZipLib" and copy Bin/Release/SharpZipLib.WindowsPhone7.dll
- Create new folder "SharpZip" and past into copied SharpZipLib.WindowsPhone7.dll.
- Right click on project and "Add Reference...".
- Choose from "Browse" tab SharpZipLib.WindowsPhone7.dll from your project location.
- 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;
- 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); }
- 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); } } } } }
Run your application and enjoy.
WP7 - Download zip file, unzip and save to isolated storage
Ярлыки:
download,
file,
IsolatedStorage,
SharpZipLib,
unzip,
Visual Studio,
windows phone 7,
wp7,
zip file
Subscribe to:
Post Comments (Atom)
I have to wait for the completion of the operation
ReplyDeletehow can I do?
Paste the code at the end of ReadWebRequestCallback function or create a new function and call it from end of the ReadWebRequestCallback function
Deletei am facing an error at line
ReplyDeleteusing (BinaryWriter streamWriter =
new BinaryWriter(new IsolatedStorageFileStream(theEntry.Name,
FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write, isoStore)))
Error:
"System.IO.IsolatedStorage Exception
Opeartion not permitted on IsolatedStorageFileStream
Please help
I am having the same problem
Delete(Error:
"System.IO.IsolatedStorage Exception
Opeartion not permitted on IsolatedStorageFileStream)
Please have you been able to fix yours? I need assistance. Any suggestions will be appreciated.
See http://stackoverflow.com/questions/8415979/operation-not-permitted-on-isolatedstoragefilestream-error
Delete