Exemple #1
0
        // Extract a single zip archive entry to the given folder
        private async Task ExtractZipEntryAsync(ZipArchiveEntry entry, StorageFolder folder)
        {
            try
            {
                using (Stream entryStream = entry.Open())
                {
                    byte[] buffer = new byte[entry.Length];
                    entryStream.Read(buffer, 0, buffer.Length);

                    string      filePath         = entry.FullName.Replace('/', '\\');
                    StorageFile uncompressedFile = await folder.CreateFileAsync(filePath, CreationCollisionOption.ReplaceExisting);

                    using (var uncompressedFileStream = await uncompressedFile.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        using (Stream outstream = uncompressedFileStream.AsStreamForWrite())
                        {
                            outstream.Write(buffer, 0, buffer.Length);
                            outstream.Flush();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                DownloadErrors.Add(ex.ToString());
                RaisePropertyChanged("HasDownloadErrors");
            }
        }
Exemple #2
0
        /// <summary>Downloads a .zip file from online URL and extracts it to the App Local Foilder</summary>
        public async Task DownloadLocalDataAsync()
        {
            try
            {
                IsDownloading   = true;
                CurrentFileName = string.Empty;
                DownloadErrors.Clear();

                CurrentAction = "Downloading...";
                var client   = new HttpClient();
                var response = await client.GetAsync(SampleDataUrl);

                CurrentAction = "Reading Archive...";
                var zipStream = await response.Content.ReadAsStreamAsync();

                CurrentAction = "Extracting Files...";
                await ExtractZipArchive(zipStream, ApplicationData.Current.LocalFolder);

                LastDownloadDate = DateTime.Now.ToString();
                ApplicationData.Current.LocalSettings.Values["LastDataDownloadDate"] = LastDownloadDate;
            }
            catch (Exception ex)
            {
                DownloadErrors.Add(ex.Message);
                RaisePropertyChanged("HasDownloadErrors");
            }
            finally
            {
                IsDownloading = false;
            }
        }