Script 14: Zip in Studio for WinRT XAML 


image


 


Zip 


 


ComponentOne Zip™ for WinRT XAML provides a complete implementation of the Zip compression standard. Improve the performance of your Windows Store apps by compressing the data sent over the wire and securing it with data encryption. The C1Zip library is the only complete, cross-platform Zip implementation.




  • Compress Files and Folders


Compressed files take up less storage space and can be transferred over the web more quickly than uncompressed files. Store resources as compressed files, download them efficiently, and then decompress them on the client side. You can also combine several files into a single compressed folder, making it easier to share a group of files. Compress and expand folders while preserving folder structure.




  • Get and Set File Information


Easily obtain zip file information including a detailed list of the zip file's contents. You can also control file information by adding and retrieving comments and control path information for individual entries in the zip file, and getting and setting the global zip file comment. Access files within a zip archive by index or file name with ease using the C1ZipFile and C1ZipEntry classes.




  • Data Encryption


C1Zip supports data encryption, so you can create secure archives that require a password to unzip. With C1Zip you can also decompress archives that are encrypted so long as you have the original password used when the zip was created.




  • Perfect for XML Data


XML is one of the most common formats used by web applications. XML data typically compresses to about 10% of the original size.




  • Cross-platform Compatibility


The C1Zip library is available for every .NET platform including Windows Forms, ASP.NET, WPF, Silverlight, Windows Phone, and WinRT. Re-use much of the same code and support in more than one platform. The Microsoft ZipArchive and Compression classes are only available in .NET 4.5 on Windows 8.


Here is some sample code to get you started with Zip



    Open a Zip File from Users Machine


You can allow the user to select and open a Zip file from their documents library using the FileOpenPicker class. Use the following code to open a file with C1Zip. This sample also assumes you have a C1FlexGrid control on your page named “flex” to display the results.


 


To set up your project, complete the following steps:


1.  In Visual Studio, select File | New | Project.


2.  In the New Project dialog box, expand a language in the left pane, under the language select Windows Metro style, and in the templates list select Blank App (XAML). Enter a Name for your project and click OK to create your project.


3.  Open MainPage.xaml if it isn't already open, place the cursor between the <Grid> and </Grid> tags, and click once.


4. Navigate to the Toolbox and double-click the Button icon to add the Button to MainPage.xaml. Modify properties to look as follows:


 



        <Button Content="Load Zip File" HorizontalAlignment="Left" VerticalAlignment="Top" Click="Button_Click_1"/>

 


 


5. Navigate to the Toolbox and double-click the FlexGrid icon from C1 XAML Controls icon to add the Button to MainPage.xaml. Thjis will add the appropriate references. Modify properties to look as follows:


 



        <FlexGrid:C1FlexGrid x:Name="flex" HorizontalAlignment="Left" Margin="38,68,0,0" VerticalAlignment="Top" Width="Auto" Height="Auto"/>

 


 


6. Open MainPage.Xaml.cs  and add the following using statement:


 



using C1.C1Zip;

 


 


7. Add this Button_Click_1 event handler with the following code.


 



        // open an existing zip file
async private void Button_Click_1(object sender, RoutedEventArgs e)
{
C1ZipFile zip = new C1ZipFile(new System.IO.MemoryStream(), true);
try
{
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.SuggestedStartLocation = Windows.Storage.Pickers.
PickerLocationId.DocumentsLibrary;
picker.FileTypeFilter.Add(
".zip");
var file = await picker.PickSingleFileAsync();
if (file != null)
{
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
zip.Open(stream.AsStream());
// Load entries into FlexGrid
flex.ItemsSource = zip.Entries;
}
}
catch (Exception x)
{
System.Diagnostics.
Debug.WriteLine(x.Message);
}
}

 


    


Run the app and open a Zip file… presto magic, it extracts the contents and displays in the legendary FlexGrid… wow! That was just too easy, huh?


 


 


image


 


This zip file, by the way, is a great one to download. I am opening up a Windows 8 app samples zip file I downloaded from Microsoft.


 


Microsoft’s Windows 8 Developer Site:


 


http://dev.windows.com  


 


Specific zip file download location:


 


http://code.msdn.microsoft.com/windowsapps/Windows-8-Modern-Style-App-Samples


 


 


Zip for WinRT XAML Frequently Asked Questions


 


Here are some frequently asked questions (FAQs) about ComponentOne Zip for WinRT XAML:


 


1)   How much data can be stored in a ZIP file comment? I would like to use it to store information in XML.


 


Answer: They are limited to 32,000 entries. However, if you are going anywhere near that value, it would be better to add that information as a separate file instead.


 


2)   What is the maximum number of files that can be stored in a ZIP file?


 


Answer: Same limit, 32,000 entries. The total length of the zip file is limited to 4 gigs. All these limits are related to the types of variables used in the zip file specification. Interestingly, there's a proposed spec for 64-bit extensions to the zip format, but that's not widely used yet, and there is a lot of debate still going on.


 


See the above demo and more in the documentation on WinRT XAML Zip.


Next: Blog Series (Part 15) TreeView: Windows 8 Studio for WinRT XAML