这个例子,是一个完整的Metro解压缩ZIP文件的例子,首先选择要解压缩的Zip文件,然后自选保存目录。
private async void UnZipFile(object sender, RoutedEventArgs e) { FileOpenPicker fop = new FileOpenPicker(); fop.FileTypeFilter.Add(".zip"); fop.SuggestedStartLocation = PickerLocationId.Desktop; fop.ViewMode = PickerViewMode.List; var zipfile = await fop.PickSingleFileAsync(); byte[] buffer = WindowsRuntimeBufferExtensions.ToArray(await Windows.Storage.FileIO.ReadBufferAsync(zipfile)); using (MemoryStream stream = new MemoryStream(buffer)) { ZipArchive zipArchive = new ZipArchive(stream, ZipArchiveMode.Read); FolderPicker fp = new FolderPicker(); //Add file types to the FolderPicker foreach (var zipArchiveEntry in zipArchive.Entries) { if (!zipArchiveEntry.FullName.EndsWith("/")) { string fileExtension = "." + Regex.Match(zipArchiveEntry.FullName, @"[^\.]\w+$").Value; if (!fp.FileTypeFilter.Contains(fileExtension)) fp.FileTypeFilter.Add(fileExtension); } } fp.SuggestedStartLocation = PickerLocationId.Desktop; fp.ViewMode = PickerViewMode.List; StorageFolder unzipfolder = await fp.PickSingleFolderAsync(); int progress = 0; 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(); } } } } } progress++; progressBar.Value = progress * 100 / zipArchive.Entries.Count; } } }