Click here to Skip to main content
13,354,132 members (58,030 online)
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 10 Sep 2012

How to Compress Files in Metro Style Applications

, 10 Sep 2012
Rate this:
Please Sign up or sign in to vote.
How to use the ZipArchive class in a Metro style app to compress a set of files and save the compressed file to a particular location.

In this blog post, I am going to demonstrate how to use ZipArchive class in Metro style app to compress a set of files and save the compressed file to a particular location.

Steps to Compress a Set of Files

Step 1

Select the folder containing the files which you want to compress.

//Select Folder to Compress
FolderPicker saveFolder = new FolderPicker();
//Suggest start location
saveFolder.SuggestedStartLocation = PickerLocationId.Desktop;
//Add file type filter
//Opens folder picker to allow the user to select the folder to compress 
StorageFolder storageFolderForCompression  = await saveFolder.PickSingleFolderAsync();

Step 2

Retrieve the files present under the selected folder:

// Retrieve the files to compress
IReadOnlyList<StorageFile> filesToCompress = await 
GetStorageFiles(storageFolderForCompression as IStorageItem);

Helper Methods

async Task<List<StorageFile>> GetStorageFiles(IStorageItem storageItem)
List<StorageFile> storageFileList = new List<StorageFile>();
// Gets the items under the selected folder (Storage Item)
IReadOnlyList<IStorageItem> items = await (storageItem as StorageFolder).GetItemsAsync();
foreach(IStorageItem item in items)
case FileAttributes.Directory:
// If the item is a directory under the selected folder, 
// then retrieve the files under the directory by calling the same function recursively
List<StorageFile> temp = await GetStorageFiles(item);
// Copy the files under the directory to the storage file list
Copy(temp, storageFileList);
// If the item is a file, Add the item to the storage file list
storageFileList.Add(item as StorageFile);
// Return storage file list for compression
return storageFileList;

private void Copy(List<StorageFile> source, List<StorageFile> destination)
// For each file item present under the directory copy it to the   destination storage file list
foreach (StorageFile file in source)

Note: Alternatively, you can use FileOpenPicker control’s PickMultipleFilesAsync() method to allow the users to select multiple files instead of selecting the folder. In this way, you can skip both Step 1 and Step 2.

// File open picker
FileOpenPicker fileOpenPicker = new FileOpenPicker();
fileOpenPicker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
// Allows user to select multiple files which returns storage file   list
IReadOnlyList<StorageFile> filesToCompress = await fileOpenPicker.PickMultipleFilesAsync();

Step 3

Create ZipArchive object using a memory stream and then for each file to be compressed, add a ZipArchiveEntry with the file name and copy the file contents to the ZipArchiveEntrystream. Once the files are added to the zip archive, close the zip archive object and copy the contents of memory stream to the storage file which is saved to a particular location.

// Retrieve files to compress
IReadOnlyList<StorageFile> filesToCompress = 
await GetStorageFiles(storageFolderForCompression as IStorageItem);
// Created new file to store compressed files
//This will create a file under the selected folder in the name   ""
StorageFile zipFile = await storageFolderForCompression.CreateFileAsync("");
// Create stream to compress files in memory (ZipArchive  can't stream to an IRandomAccessStream, see
// for details on this issue)
using (MemoryStream zipMemoryStream = new MemoryStream())
// Create zip archive
using (ZipArchive zipArchive = new ZipArchive(zipMemoryStream, ZipArchiveMode.Create))
// For each file to compress...
foreach (StorageFile fileToCompress in filesToCompress)
//Read the contents of the file
byte[] buffer = WindowsRuntimeBufferExtensions.ToArray(await FileIO.ReadBufferAsync(fileToCompress));
// Create a zip archive entry
ZipArchiveEntry entry = zipArchive.CreateEntry(fileToCompress.Name);
// And write the contents to it
using (Stream entryStream = entry.Open())
await entryStream.WriteAsync(buffer, 0, buffer.Length);
using (IRandomAccessStream zipStream = await zipFile.OpenAsync(FileAccessMode.ReadWrite))
// Write compressed data from memory to file
using (Stream outstream = zipStream.AsStreamForWrite())
byte[] buffer = zipMemoryStream.ToArray();
outstream.Write(buffer, 0, buffer.Length);

Thus the files under the selected folder are compressed to a particular location.



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Technical Lead Syncfusion
India India
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionOutofMemoryException Pin
P1120-Sep-15 22:11
memberP1120-Sep-15 22:11 
QuestionIssue for zip a big file Pin
Goblin_Yang13-Jan-13 16:09
memberGoblin_Yang13-Jan-13 16:09 
BugFormatting and Blog URL Pin
Shemeer NS9-Sep-12 11:07
memberShemeer NS9-Sep-12 11:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 10 Sep 2012
Article Copyright 2012 by rajendran_sp
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid