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/
    欢迎任何形式的转载,未经作者同意,请保留此段声明!

  • 相关阅读:
    UNIX高级环境编程(2)FIle I/O - 原子操作、共享文件描述符和I/O控制函数
    UNIX高级环境编程(1)File I/O
    排序算法(2) 堆排序 C++实现
    加深一下BlockingQueue的认识
    测试一下StringBuffer和StringBuilder及字面常量拼接三种字符串的效率
    java中的锁
    Http状态码之:301、302重定向
    学点HTTP知识
    java中的字符串相关知识整理
    在Openfire上弄一个简单的推送系统
  • 原文地址:https://www.cnblogs.com/linlf03/p/2781036.html
Copyright © 2011-2022 走看看