示例#1
0
        private async Task <StorageFolder> extractFiles(StorageFile zipfilename)
        {
            StorageFolder storfolder = null;

            try
            {
                // Create stream for compressed files in memory
                using (MemoryStream zipMemoryStream = new MemoryStream())
                {
                    using (Windows.Storage.Streams.IRandomAccessStream zipStream = await zipfilename.OpenAsync(FileAccessMode.Read))
                    {
                        // Read compressed data from file to memory stream
                        using (Stream instream = zipStream.AsStreamForRead())
                        {
                            byte[] buffer = new byte[1024];
                            while (instream.Read(buffer, 0, buffer.Length) > 0)
                            {
                                zipMemoryStream.Write(buffer, 0, buffer.Length);
                            }
                        }
                    }
                    storfolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(zipfilename.DisplayName, CreationCollisionOption.GenerateUniqueName);

                    // Create zip archive to access compressed files in memory stream
                    using (ZipArchive zipArchive = new ZipArchive(zipMemoryStream, ZipArchiveMode.Read))
                    {
                        // For each compressed file...
                        foreach (ZipArchiveEntry entry in zipArchive.Entries)
                        {
                            // ... read its uncompressed contents
                            using (Stream entryStream = entry.Open())
                            {
                                if (entry.Name != "")
                                {
                                    string fileName = entry.FullName.Replace("/", @"\");
                                    byte[] buffer   = new byte[entry.Length];
                                    entryStream.Read(buffer, 0, buffer.Length);

                                    // Create a file to store the contents
                                    StorageFile uncompressedFile = await storfolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);

                                    // Store the contents
                                    using (Windows.Storage.Streams.IRandomAccessStream uncompressedFileStream = await uncompressedFile.OpenAsync(FileAccessMode.ReadWrite))
                                    {
                                        using (Stream outstream = uncompressedFileStream.AsStreamForWrite())
                                        {
                                            outstream.Write(buffer, 0, buffer.Length);
                                            outstream.Flush();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch
            {
                //this.pbar.IsIndeterminate = false;
                //this.Add.IsEnabled = true;
                //this.imageGrid.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                if (storfolder != null)
                {
                    await storfolder.DeleteAsync();
                }
                return(null);
            }
            finally
            {
                zipfilename.DeleteAsync();
            }
            return(storfolder);
        }