Here are the steps,
Step 1:
Create a simple Windows Project. New Project, Visual C#, Windows 8, Windows, then click Blank App (Windows 8.1).
Step 2:
Let's add a button in MainPage.xaml :
- A button with the Click event.
Complete MainPage.xaml code snippet is:
- <Page
- x:Class="ZipFolderContents.MainPage"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="using:ZipFolderContents"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d">
-
- <Grid Background="#FF5D3FE4">
- <Button x:Name="btn_ZipContents" Content="Zip Folder" HorizontalAlignment="Left" Margin="451,144,0,0" VerticalAlignment="Top" Click="btn_ZipContents_Click" Height="60" Width="204"/>
- </Grid>
- </Page>
Step 3:
For testing purposes, we will read a folder from Documents Library, zip its content and save it in our application local folder.
So lets create a folder named ‘
TestFolder’ in Documents Library. Inside the ‘TestFolder’, add some folders, text files, images, videos, etc.
Step 4:
For accessing Documents Library, we have to declar it in app capabilities. Right click on Package.appmanifest > View Code
In Package.appmanifest XML, update the Capabilities node with:
- <Capabilities>
- <Capability Name="internetClient" />
- <Capability Name="documentsLibrary" />
- </Capabilities>
Step 5:
Now again in the Package.appmanifest designer view, we have to add File Type Association declarations as discussed
here:
Step 6:
In the code behind: MainPage.xaml.cs
Update your code with this:- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.IO.Compression;
- using System.Linq;
- using System.Runtime.InteropServices.WindowsRuntime;
- using System.Threading.Tasks;
- using Windows.Foundation;
- using Windows.Foundation.Collections;
- using Windows.Storage;
- using Windows.UI.Popups;
- using Windows.UI.Xaml;
- using Windows.UI.Xaml.Controls;
- using Windows.UI.Xaml.Controls.Primitives;
- using Windows.UI.Xaml.Data;
- using Windows.UI.Xaml.Input;
- using Windows.UI.Xaml.Media;
- using Windows.UI.Xaml.Navigation;
-
-
-
- namespace ZipFolderContents
- {
-
-
-
- public sealed partial class MainPage : Page
- {
- public MainPage()
- {
- this.InitializeComponent();
- }
-
- private async void btn_ZipContents_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- string appFolderPath = ApplicationData.Current.LocalFolder.Path;
- StorageFolder destinationFolder = await StorageFolder.GetFolderFromPathAsync(appFolderPath);
-
-
- StorageFolder sourceFolder = await KnownFolders.DocumentsLibrary.GetFolderAsync("TestFolder");
-
-
- StorageFile zipFile = await destinationFolder.CreateFileAsync("TestFolder.zip", CreationCollisionOption.ReplaceExisting);
- Stream zipToCreate = await zipFile.OpenStreamForWriteAsync();
- ZipArchive archive = new ZipArchive(zipToCreate, ZipArchiveMode.Update);
-
- await ZipFolderContentsHelper(sourceFolder, archive, sourceFolder.Path);
- archive.Dispose();
- MessageDialog msg = new MessageDialog("Success");
- await msg.ShowAsync();
- }
- catch (Exception ex)
- {
- Debug.WriteLine(ex.ToString());
- }
- }
-
- private async Task ZipFolderContentsHelper(StorageFolder sourceFolder, ZipArchive archive, string sourceFolderPath)
- {
- IReadOnlyList<StorageFile> files = await sourceFolder.GetFilesAsync();
-
- foreach (StorageFile file in files)
- {
- ZipArchiveEntry readmeEntry = archive.CreateEntry(file.Path.Remove(0, sourceFolderPath.Length));
- byte[] buffer = WindowsRuntimeBufferExtensions.ToArray(await FileIO.ReadBufferAsync(file));
- using (Stream entryStream = readmeEntry.Open())
- {
- await entryStream.WriteAsync(buffer, 0, buffer.Length);
- }
- }
-
- IReadOnlyList<StorageFolder> subFolders = await sourceFolder.GetFoldersAsync();
-
- if (subFolders.Count() == 0)
- {
- return;
- }
-
- foreach (StorageFolder subfolder in subFolders)
- {
- await ZipFolderContentsHelper(subfolder, archive, sourceFolderPath);
- }
- }
- }
- }
Step 7:
Run the application and click on Zip Folder button, you will get a success message if everything is OK. And ‘TestFolder.zip’ is created in: This PC, C , Users > [Your User Name] > AppData, Local, Packages, [App package name] > LocalState
That’s it.
Thanks, Happy Coding!
Read more articles on Windows Runtime Apps: