Example #1
0
        protected async Task MoveFileAsync(FileInfo file)
        {
            try
            {
                var tempPath      = UpdaterPaths.GetTempUnpackedFolder() + "\\";
                var newFilePath   = file.FullName.Remove(file.FullName.IndexOf(tempPath), tempPath.Length);
                var directoryPath = newFilePath.Remove(newFilePath.IndexOf(file.Name));

                if (!Directory.Exists(directoryPath) && !string.IsNullOrWhiteSpace(directoryPath))
                {
                    Directory.CreateDirectory(directoryPath);
                }

                using (var oldFileStream = file.OpenRead())
                {
                    using (var newFileStream = File.Create(newFilePath))
                    {
                        OnCopyingFileChanged(file.Name);

                        int    bytesRead   = 0;
                        int    bytesLoaded = 0;
                        byte[] buffer      = new byte[4096];

                        do
                        {
                            bytesRead = await oldFileStream.ReadAsync(buffer, 0, buffer.Length);

                            await newFileStream.WriteAsync(buffer, 0, bytesRead);

                            bytesLoaded += bytesRead;

                            double progress = (double)bytesLoaded / (double)file.Length * 100;
                            ProgressReport((int)progress);
                        }while (bytesRead > 0);

                        await newFileStream.FlushAsync();
                    }
                }
            }
            catch
            {
                throw new UpdaterException(UpdaterError.CopyingFailed);
            }
        }
Example #2
0
        private async Task <DirectoryInfo> Unzip(string zipFile)
        {
            using (var operationScope = new OperationScope(currentOperation, OperationStatus.Unzipping))
            {
                try
                {
                    DirectoryInfo dir = new DirectoryInfo(UpdaterPaths.GetTempUnpackedFolder());

                    if (!dir.Exists)
                    {
                        dir.Create();
                    }

                    using (var zipFileStream = File.OpenRead(zipFile))
                    {
                        using (var archive = new ZipArchive(zipFileStream))
                        {
                            foreach (ZipArchiveEntry entry in archive.Entries)
                            {
                                if (string.IsNullOrWhiteSpace(entry.Name))
                                {
                                    continue;
                                }

                                OnUnzippingFileChanged(entry.Name);

                                var directoryPath = entry.FullName.Remove(entry.FullName.IndexOf(entry.Name));

                                if (!string.IsNullOrWhiteSpace(directoryPath))
                                {
                                    Directory.CreateDirectory(Path.Combine(dir.FullName, directoryPath));
                                }

                                string fileName = Path.Combine(dir.FullName, entry.FullName.Replace("/", @"\"));

                                using (var newFileStream = File.Create(fileName))
                                {
                                    Stream fileData = entry.Open();

                                    int    bytesRead   = 0;
                                    int    bytesLoaded = 0;
                                    byte[] buffer      = new byte[4096];

                                    do
                                    {
                                        bytesRead = await fileData.ReadAsync(buffer, 0, buffer.Length);

                                        await newFileStream.WriteAsync(buffer, 0, bytesRead);

                                        bytesLoaded += bytesRead;

                                        double progress = (double)bytesLoaded / entry.Length * 100;
                                        ProgressReport((int)progress);
                                    }while (bytesRead > 0);

                                    await newFileStream.FlushAsync();
                                }
                            }
                        }
                    }

                    return(dir);
                }
                catch
                {
                    throw new UpdaterException(UpdaterError.UnzippingFailed);
                }
            }
        }