Example #1
0
        /// <summary>
        /// Unzips the .zip file into the specified folder to allow for the reading necessary
        /// to open the book in this app
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="folderName"></param>
        /// <returns></returns>
        private async Task UnZipTheFile(string fileName, string folderName)
        {
            var folder = await sFolder.GetFolderAsync(folderName);

            using (var stream = await sFolder.OpenStreamForReadAsync(fileName))
            {
                using (var archive = new ZipArchive(stream))
                {
                    foreach (var entry in archive.Entries)
                    {
                        if (entry.FullName.Contains('/'))
                        {
                            if (entry.FullName.ElementAt(entry.FullName.Length - 1) == '/')
                            {
                                var multiFolder = entry.FullName.Substring(0, entry.FullName.Length - 1);
                                try
                                {
                                    await sFolder.CreateFolderAsync(folderName + "\\"
                                                                    + multiFolder);
                                }
                                // ReSharper disable once EmptyGeneralCatchClause
                                catch (Exception)
                                {
                                }
                            }
                            else
                            {
                                var directoryName = entry.FullName.Split('/');
                                var lastFolder    = await Io.CreateOrGetFolders(folder, directoryName);

                                using (var file = entry.Open())
                                {
                                    StorageFile newFile = null;
                                    try
                                    {
                                        newFile = await lastFolder.CreateFileAsync(entry.Name, CreationCollisionOption.ReplaceExisting);
                                    }
                                    // ReSharper disable once EmptyGeneralCatchClause
                                    catch (Exception)
                                    {
                                    }
                                    if (newFile == null)
                                    {
                                        newFile = await lastFolder.GetFileAsync(entry.Name);
                                    }
                                    using (var trans = await newFile.OpenStreamForWriteAsync())
                                    {
                                        file.CopyTo(trans);
                                    }
                                }
                            }
                        }
                        else
                        {
                            //add files to the base folder
                            using (var file = entry.Open())
                            {
                                StorageFile newFile = null;
                                try
                                {
                                    newFile = await folder.CreateFileAsync(entry.FullName, CreationCollisionOption.ReplaceExisting);
                                }
                                // ReSharper disable once EmptyGeneralCatchClause
                                catch (Exception)
                                {
                                }
                                if (newFile == null)
                                {
                                    newFile = await folder.GetFileAsync(entry.FullName);
                                }
                                using (var trans = await newFile.OpenStreamForWriteAsync())
                                {
                                    file.CopyTo(trans);
                                }
                            }
                        }
                    }
                }
            }
        }