When developing a Win8 modern UI app, If you need zip compression and decompression files (not gzip), you may first think of sharpziplib, this class library is used during WP7. However, with the use of. Net 4.5 and C #5.0 in Win8, a new function is introduced. In fact, zip compression and decompression can be implemented without a third-party class library.
The system. Io. Compression namespace is mainly used. The following example showsProgramAll namespaces to be used are as follows:
UsingSystem;UsingSystem. Collections. Generic;UsingSystem. IO;UsingSystem. Io. compression;UsingSystem. runtime. interopservices. windowsruntime;UsingSystem. Threading. tasks;UsingWindows. storage;UsingWindows. Storage. streams;
Decompress the ZIP file:
Public Async Task <storagefolder> unzipfile (String Foldername, Byte [] Data) {storagefolder unzipfolder = Await Applicationdata. Current. localfolder. createfolderasync (Foldername, creationcollisionoption. openifexists ); VaR Stream = New Memorystream (data ); VaR Ziparchive = New Ziparchive (stream, ziparchivemode. Read ); Foreach ( VaR Ziparchiveentry In Ziparchive. Entries ){ If (! String. isnullorempty (ziparchiveentry. fullname )){ If (! Ziparchiveentry. fullname. endswith ( " / " )){ String Filename = ziparchiveentry. fullname. Replace ( " / " , " \\ " ); Using (Stream filedata = Ziparchiveentry. open () {storagefile newfile = Await Unzipfolder. createfileasync (filename, creationcollisionoption. replaceexisting ); Using (Irandomaccessstream newfilestream = Await Newfile. openasync (fileaccessmode. readwrite )){ Using (Stream S = Newfilestream. asstreamforwrite ()){ Await Filedata. copytoasync (s ); Await S. flushasync (); S. Dispose () ;}newfilestream. Dispose ();}}}}} Return Unzipfolder ;}
Unzipfile () method. The first parameter is the location of the file to be decompressed (the decompression location is localfolder. You can create a new folder and modify it as needed ), the second parameter is the byte array of the compressed file. This array may be from files downloaded from the network or in other independent buckets.
Folder compression (if you need to compress a single file,CodeCan be extracted from the following code ):
Public Async Task < Byte []> Zipfolder (storagefolder folder ){ Using ( VaR Zipmemorystream = New Memorystream ()){ Using ( VaR Ziparchive = New Ziparchive (zipmemorystream, ziparchivemode. Create )){ Await Addzipfoldertoentry (ziparchive, folder, "" );} Return Zipmemorystream. toarray ();}} Private Async Task < Bool > Addzipfoldertoentry (ziparchive, storagefolder folder,String Entryfirst) {ireadonlylist <Storagefile> filestocompress = Await Folder. getfilesasync (); Foreach (Storagefile filetocompress In Filestocompress ){ Byte [] Buffer = ( Await Fileio. readbufferasync (filetocompress). toarray (); ziparchiveentry entry = Ziparchive. createentry (entryfirst +Filetocompress. Name ); Using (Stream entrystream = Entry. open ()){ Await Entrystream. writeasync (buffer, 0 , Buffer. Length );}} VaR Childrenfolder = Await Folder. getfoldersasync (); Foreach ( VaR Storagefolder In Childrenfolder ){ Await Addzipfoldertoentry (ziparchive, storagefolder, entryfirst + storagefolder. Name + " / " );} Return True ;}
The zipfolder () method is to compress an extremely sub-folder and a file into a zip file. The specific process is to recursively call addzipfoldertoentry.