Windows 8 Development winrt decompress the ZIP file and zip the folder

Source: Internet
Author: User
Tags ziparchive

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.