work hard work smart

专注于Java后端开发。 不断总结,举一反三。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Metro style app 解压缩

Posted on 2012-11-21 17:11  work hard work smart  阅读(488)  评论(0编辑  收藏  举报

 这个例子,是一个完整的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;
                }
            }
        }

Source Address