zoukankan      html  css  js  c++  java
  • Metro style app 解压缩

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

    作者:Work Hard Work Smart
    出处:http://www.cnblogs.com/linlf03/
    欢迎任何形式的转载,未经作者同意,请保留此段声明!

  • 相关阅读:
    令我印象最深刻的三个老师
    硬盘大于2T安装CentOS7.X时要注意分区
    Linux网卡配置
    Python13:文件操作
    Python12:集合
    Python11:字典
    Python10:String字符串
    Python09:元组
    Python08:列表
    Python07:模块初识
  • 原文地址:https://www.cnblogs.com/linlf03/p/2781036.html
Copyright © 2011-2022 走看看