Iris Classon
Iris Classon - In Love with Code

Example: Compressing and decompressing files in a Windows Store App with GZipStream class

I don’t know about you, but working with streams in WinRT has proven to be a challenge- at least for me. I keep expecting things to be there that aren’t and my reads and writes collide in my mind. Aside from WinRT, there has also been changes in (welcoming changes) in .Net 4.5, and I struggled to find some simple examples, of well- let’s say compression and decompression with GZip (talking about the GZipStream class in .Net here).

I’m pretty stressed at the moment with next week’s conferences, so I’ve just whipped up a simple working example here for compressing and decompressing files in Windows Store Apps with the GZipStream class.
I also wanted to compare the compression to Zip and GZip- they aren’t really 100% comparable as GZip doesn’t archive on its own. But nonetheless I was curious to see how much of a difference there was in regards to compression and compression only when compressing a small json file. You can see the results below.

Compressing and decompressing files in a Windows Store App with GZipStream class
Here is the code for compressing and decompressing files in a Windows Store App with GZipStream class where the user selects the files.

Please notice that there is no error handling etc. since this is just an example, and I’ve left in duplicated code to keep the code in each function so you can see how it works. This is by no means production code :)

<3 notice the CopyToAsync <3 #HowIDoLoveAsync

[sourcecode language=“csharp”]
private async void Button_Click_1(object sender, RoutedEventArgs e)
{
var filePickerGet = new FileOpenPicker {SuggestedStartLocation = PickerLocationId.DocumentsLibrary};
filePickerGet.FileTypeFilter.Add(".txt");

IReadOnlyList<StorageFile> storageFiles = await filePickerGet.PickMultipleFilesAsync();  

var filePickerSaveTo = new FileSavePicker();  
filePickerSaveTo.FileTypeChoices.Add("Zip Files (\*.zip)", new List<string> { ".gzip" });  
filePickerSaveTo.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;  

var zip = await filePickerSaveTo.PickSaveFileAsync();  

using (var zipStream = await zip.OpenAsync(FileAccessMode.ReadWrite))  
{  
    using (var compressStream = new GZipStream(zipStream.AsStreamForWrite(), CompressionMode.Compress))  
    {  
        foreach (var file in storageFiles)  
        {  
            using (var fileStream = await file.OpenReadAsync())  
            {  
                fileStream.GetInputStreamAt(0).AsStreamForRead().CopyTo(compressStream);  
            }  
        }  
    }  
}  

}

private async void Button_Click_2(object sender, RoutedEventArgs e)
{
var filePickerGet = new FileOpenPicker {SuggestedStartLocation = PickerLocationId.DocumentsLibrary};
filePickerGet.FileTypeFilter.Add(".gzip");
var zipFile = await filePickerGet.PickSingleFileAsync();

var picker = new FileSavePicker();  
picker.FileTypeChoices.Add("Txt Files (\*.txt)", new List<string> { ".txt" });  
picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;  
var txtFile = await picker.PickSaveFileAsync();  

using (var zipStream = await zipFile.OpenAsync(FileAccessMode.ReadWrite))  
{  
    using (var decompressedZip = new GZipStream(zipStream.AsStreamForRead(), CompressionMode.Decompress))  
    {  
        using (var fileStream = await txtFile.OpenStreamForWriteAsync())  
        {  
            await decompressedZip.CopyToAsync(fileStream);  
        }  
    }  
}  

}
[/sourcecode]

Comments

Leave a comment below, or by email.
Farhan
3/8/2013 2:53:57 AM
Why didn't you find my article in CodeProject ? Anyway, here is my article, though I have used ZipArchive class.
http://www.codeproject.com/Tips/515704/Archive-Multiple-Files-In-Zip-Extract-Zip-Archive 


Last modified on 2013-02-25

comments powered by Disqus