comentsys.wordpress.com today features a Universal Windows Platform tutorial showing how to create a ZipFile App which will allow you to create New and Open ZIP archives plus add, remove and extract files.
There’s a ZipItem Class to represent each item within the Archive then in the Library Class there are const values to set file extensions and descriptions plus variables for a “token” and StorageItemAccessList. There’s a GetByteFromFile method that converts a StorageFile into a byte with the use of a IRandomAccessStream plus a DataReader. The OpenFileAsync method uses a FileOpenPicker to select a file with the passed in extension and returns a StorageFile and SaveFileAsync uses a FileSavePicker to return a StorageFile that will be created and written to with the passed in filename, extension and description, and there’s a string property to store the name of the archive.
Also in the Library Class there’s a List method that uses a StorageFile from the StorageItemAccessList using the “token” – if this file is present then a Stream is used to then get the ZipArchive from and then each ZipArchiveEntry in the archive is looped through to create a list of ZipItem and is returned my that method. The NewAsync method uses the SaveFileAsync to create the StorageFile to create a new archive and returns true if successful or false if not. The OpenAsync method uses the OpenFileAsync to open an archive from the StorageFile and returns the list of ZipItem.
Again also in the Library Class is an AddAsync method uses a StorageFile from the StorageItemAccessList using the “token”. It then uses the OpenFileAsync to get the file to add to the archive – if the archive file is present then a Stream is used to then get the ZipArchive and then it checks for an existing entry – if there is one then it is deleted with Delete if not then a new entry is created with CreateEntry using the GetByteFromFile to insert the file into the archive. The Extract method is used to retrieve a file from an archive and uses a StorageFile from the StorageItemAccessList using the “token” and if this file is present then a Stream is used to then get the ZipArchive and then an entry is retrieved by name with the GetEntry method and then if present the SaveFileAsync method is used to get a StorageFile to write the file Stream to, then there’s a RemoveAsync method to remove a given item from the archive.
The MainPage has an AppBarButton for New, Open and Add. Then theres a ListBox that will have a list of ZipItem set to it’s DataSource and has a DataTemplate that displays a list of Name, Actual Size and Compressed Size for each entry in an archive plus an AppBarButton for Extract and Remove for each archive entry item displayed.
Behind the Scenes
This example shows how you can use the ZipArchive compression features in System.IO.Compression and use this within a Universal Windows Platform application – usually this is used directly with paths but due to the sandboxed nature of UWP apps the lesser seen methods demonstrated in the example are used instead. This example demonstrates all the key features of dealing with a zip archive and is pretty close to the functionality within Windows 10 itself for dealing with zip files.