ndows Phone 7 中解压zip包

15 篇文章 0 订阅
14 篇文章 0 订阅
目前项目中需要从服务器下载ZIP包然后解压获取里面的文件,先做下记录,给后来者一些帮助。
首先从 
http://slsharpziplib.codeplex.com/  获取 SharpZipLib.WindowsPhone7.dll
添加引用
  1. <phone:PhoneApplicationPage 
  2.     x:Class="PhoneApp4.MainPage"
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
  6.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
  7.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  8.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  9.     mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
  10.     FontFamily="{StaticResource PhoneFontFamilyNormal}"
  11.     xmlns:my="clr-namespace:PhoneApp4"
  12.     FontSize="{StaticResource PhoneFontSizeNormal}"
  13.     Foreground="{StaticResource PhoneForegroundBrush}"
  14.     SupportedOrientations="Portrait" Orientation="Portrait"
  15.     shell:SystemTray.IsVisible="True">

  16.     <Grid x:Name="LayoutRoot" Background="Transparent">
  17.         <Image Height="347" HorizontalAlignment="Left" Margin="38,35,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="270" />
  18.         <Button Content="download" Height="72" HorizontalAlignment="Left" Margin="38,432,0,0" x:Name="btnLoad" VerticalAlignment="Top" Width="203" Click="btnLoad_Click" />
  19.         <my:ProgressBarWithText x:Name="progressBarWithText" Text="正在努力加载中..." VerticalAlignment="Top" Foreground="{StaticResource PhoneAccentBrush}"/>
  20.     </Grid>
  21. </phone:PhoneApplicationPage>
后台代码:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Documents;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Animation;
  11. using System.Windows.Shapes;
  12. using Microsoft.Phone.Controls;
  13. using System.Net.NetworkInformation;
  14. using ICSharpCode.SharpZipLib.Zip;
  15. using System.IO.IsolatedStorage;
  16. using System.IO;

  17. namespace PhoneApp4
  18. {
  19.     public partial class MainPage : PhoneApplicationPage
  20.     {
  21.         // 构造函数
  22.         public MainPage()
  23.         {
  24.             InitializeComponent();
  25.         }

  26.         private void btnLoad_Click(object sender, RoutedEventArgs e)
  27.         {
  28.             Button btn = sender as Button;
  29.             if (NetworkInterface.GetIsNetworkAvailable())
  30.             {
  31.                 btn.IsEnabled = false;
  32.                 WebClient client = new WebClient();
  33.                 progressBarWithText.ShowProgress = true;
  34.                 client.OpenReadAsync(new Uri("http://files.cnblogs.com/youhui/2012-04/20120405.zip", UriKind.Absolute));
  35.                 client.OpenReadCompleted += (a, b) =>
  36.                 {
  37.                     //解压下载的ZIP包
  38.                     if (null == b.Error && !b.Cancelled)
  39.                     {
  40.                         using (ZipInputStream zipInputStream = new ZipInputStream(b.Result))
  41.                         {
  42.                             ZipEntry zipEntry;
  43.                             byte[] data = new byte[2048];
  44.                             int size = 2048;
  45.                             while ((zipEntry = zipInputStream.GetNextEntry()) != null)
  46.                             {
  47.                                 if (zipEntry != null)
  48.                                 {
  49.                                     string fName = zipEntry.Name;
  50.                                     if (fName != String.Empty && fName.Contains(".jpg"))
  51.                                     {
  52.                                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
  53.                                         string path = "download";
  54.                                         if (!Directory.Exists(path))
  55.                                         {
  56.                                             isf.CreateDirectory(path);
  57.                                         }
  58.                                         using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(System.IO.Path.Combine(path + "\\", fName.Substring(fName.LastIndexOf("/") + 1, fName.Length - fName.LastIndexOf("/") - 1)), FileMode.OpenOrCreate, isf))
  59.                                         {
  60.                                             while (true)
  61.                                             {
  62.                                                 size = zipInputStream.Read(data, 0, data.Length);
  63.                                                 if (size <= 0)
  64.                                                     break;
  65.                                                 fileStream.Write(data, 0, size);
  66.                                             }
  67.                                         }
  68.                                     }
  69.                                 }
  70.                             }
  71.                         }
  72.                         btn.Content = "已下载";
  73.                         MessageBox.Show("下载完成");
  74.                     }
  75.                     progressBarWithText.ShowProgress = false;
  76.                 };
  77.             }
  78.             else
  79.             {
  80.                 MessageBox.Show("当前设备没有网络连接!");
  81.             }
  82.         }
  83.     }
  84. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值