Unzip Folder Contents In Windows Runtime Apps

Here are the steps, 

Step 1 - Create a simple Windows Project. Click New Project, Visual C#, Windows 8, Windows, then select Blank App (Windows 8.1).

Here you can go through my previous article: Zip Folder and Its Content in Windows Runtime Apps

app

Step 2 - Let us add a button in MainPage.xaml :
  • A button with the Click event.

    Complete MainPage.xaml:
    1. <Page  
    2.     x:Class="UnZip.MainPage"  
    3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
    4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    5.     xmlns:local="using:UnZip"  
    6.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
    7.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
    8.     mc:Ignorable="d">  
    9.   
    10.     <Grid Background="#FF2077D4">  
    11.         <Button x:Name="btn_Unzip" Content="Unzip" HorizontalAlignment="Left" Margin="351,270,0,0" VerticalAlignment="Top" Height="83" Width="313" Click="btn_Unzip_Click"/>  
    12.     </Grid>  
    13. </Page>  
Step 3 - Let  us add a zip file in Assets folder of Solution Explorer. Unzip the contents of the zip folder and save all the files in application local folder.

Note: Make sure the Build Action is set to Content in the Properties of your zip file.

Properties

Step 4 - In the code behind: MainPage.xaml.cs,

Update your code with this:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.IO.Compression;  
  5. using System.Linq;  
  6. using System.Runtime.InteropServices.WindowsRuntime;  
  7. using Windows.ApplicationModel;  
  8. using Windows.Foundation;  
  9. using Windows.Foundation.Collections;  
  10. using Windows.Storage;  
  11. using Windows.UI.Xaml;  
  12. using Windows.UI.Xaml.Controls;  
  13. using Windows.UI.Xaml.Controls.Primitives;  
  14. using Windows.UI.Xaml.Data;  
  15. using Windows.UI.Xaml.Input;  
  16. using Windows.UI.Xaml.Media;  
  17. using Windows.UI.Xaml.Navigation;  
  18.   
  19. namespace UnZip  
  20. {  
  21.     public sealed partial class MainPage : Page  
  22.     {  
  23.         public MainPage()  
  24.         {  
  25.             this.InitializeComponent();  
  26.         }  
  27.   
  28.         private async void btn_Unzip_Click(object sender, RoutedEventArgs e)  
  29.         {  
  30.             try  
  31.             {  
  32.                 StorageFolder appFolder = ApplicationData.Current.LocalFolder;  
  33.                 var zipFile = await Package.Current.InstalledLocation.GetFileAsync("Assets\\TestZip.zip");  
  34.                 using (var zipFileStream = await zipFile.OpenStreamForReadAsync())  
  35.                 {  
  36.                     using (MemoryStream memoryStream = new MemoryStream((int)zipFileStream.Length))  
  37.                     {  
  38.                         await zipFileStream.CopyToAsync(memoryStream);  
  39.   
  40.                         using (var zipArchive = new ZipArchive(memoryStream, ZipArchiveMode.Read))  
  41.                         {  
  42.                             foreach (ZipArchiveEntry zipArchiveEntry in zipArchive.Entries)  
  43.                             {  
  44.                                 string[] arrayFolder = zipArchiveEntry.FullName.Split(new string[] { "/" }, StringSplitOptions.None);  
  45.                                 string folderName = string.Empty;  
  46.   
  47.                                 for (int i = 0; i < arrayFolder.Length - 1; i++)  
  48.                                 {  
  49.                                     folderName += "\\" + arrayFolder[i];  
  50.                                 }  
  51.   
  52.                                 if (zipArchiveEntry.Name != "")  
  53.                                 {  
  54.                                     using (Stream fileData = zipArchiveEntry.Open())  
  55.                                     {  
  56.                                         StorageFile outputFile = await appFolder.CreateFileAsync(folderName + "\\" + zipArchiveEntry.Name, CreationCollisionOption.ReplaceExisting);  
  57.                                         using (Stream outputFileStream = await outputFile.OpenStreamForWriteAsync())  
  58.                                         {  
  59.                                             await fileData.CopyToAsync(outputFileStream);  
  60.                                             await outputFileStream.FlushAsync();  
  61.                                         }  
  62.                                     }  
  63.                                 }  
  64.                                 else  
  65.                                 {  
  66.                                     await appFolder.CreateFolderAsync(folderName, CreationCollisionOption.ReplaceExisting);  
  67.                                 }  
  68.                             }  
  69.                         }  
  70.                     }  
  71.                 }  
  72.             }  
  73.             catch (Exception ex)  
  74.             {  
  75.             }  
  76.         }  
  77.     }  
  78. }  
Step 5 - Run the application and click the Unzip button. You see the extracted content of zip file inside: This PC, C, Users, [Your User Name] > AppData, Local, Packages, [App package name], then go to LocalState,

local state

That’s it.

You can also get the complete project from GitHub.


Similar Articles