Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="downloadUrl"></param>
        /// <param name="destinationFile"></param>
        /// <param name="progressMonitor"></param>
        /// <returns></returns>
        /// <exception cref="DownloadFailedException"></exception>
        public static DownloadResult DownloadFileFromGithub(string downloadUrl, string destinationFile, IProgressMonitor progressMonitor)
        {
            var cancellationToken = progressMonitor.GetCancellationToken();

            if (cancellationToken.IsCancellationRequested)
            {
                return(DownloadResult.Canceled);
            }

            if (!Directory.Exists(Path.GetDirectoryName(destinationFile)))
            {
                throw new DirectoryNotFoundException(Path.GetDirectoryName(destinationFile));
            }

            Exception downloadException = null;

            using (var webClient = new WebClient())
            {
                var currentVersion = GobchatContext.ApplicationVersion;
                webClient.Headers.Add("User-Agent", $"Gobchat v{currentVersion}");

                progressMonitor.Progress   = 0d;
                progressMonitor.StatusText = Resources.Core_Util_DownloadHelper_Waiting;
                progressMonitor.Log(Resources.Core_Util_DownloadHelper_Prepare);

                void OnDownloadProgressChanged(object s, DownloadProgressChangedEventArgs e)
                {
                    progressMonitor.Progress   = e.ProgressPercentage / 100d;
                    progressMonitor.StatusText = StringFormat.Format(Resources.Core_Util_DownloadHelper_Download, e.BytesReceived, e.TotalBytesToReceive);
                    if (cancellationToken.IsCancellationRequested)
                    {
                        progressMonitor.Log(Resources.Core_Util_DownloadHelper_Canceled);
                        webClient.CancelAsync();
                    }
                }

                webClient.DownloadProgressChanged += OnDownloadProgressChanged;

                try
                {
                    progressMonitor.Log(StringFormat.Format(Resources.Core_Util_DownloadHelper_Connecting, downloadUrl));
                    var downloadTask = webClient.DownloadFileTaskAsync(downloadUrl, destinationFile);
                    downloadTask.Wait();

                    progressMonitor.Progress   = 1d;
                    progressMonitor.StatusText = Resources.Core_Util_DownloadHelper_Complete;
                    progressMonitor.Log(Resources.Core_Util_DownloadHelper_Complete);
                }
                catch (AggregateException ex)
                {
                    downloadException = ex.Flatten();
                }
                catch (Exception ex)
                {
                    downloadException = ex;
                }
            }

            if (downloadException != null && !cancellationToken.IsCancellationRequested)
            {
                throw new DownloadFailedException(downloadException.Message, downloadException);
            }

            return(cancellationToken.IsCancellationRequested ? DownloadResult.Canceled : DownloadResult.CompletedSuccessfully);
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="archivePath"></param>
        /// <param name="destinationFolder"></param>
        /// <param name="progressMonitor"></param>
        /// <returns></returns>
        /// <exception cref="FileNotFoundException"></exception>
        /// <exception cref="DirectoryNotFoundException"></exception>
        /// <exception cref="ExtractionFailedException"></exception>
        public static ExtractionResult ExtractArchive(string archivePath, string destinationFolder, IProgressMonitor progressMonitor)
        {
            var cancellationToken = progressMonitor.GetCancellationToken();

            if (cancellationToken.IsCancellationRequested)
            {
                return(ExtractionResult.Canceled);
            }

            if (!File.Exists(archivePath))
            {
                throw new FileNotFoundException(archivePath);
            }
            if (!Directory.Exists(destinationFolder))
            {
                throw new DirectoryNotFoundException(destinationFolder);
            }

            progressMonitor.StatusText = Resources.Core_Util_ArchiveUnpackerHelper_Unpack;
            progressMonitor.Progress   = 0d;
            progressMonitor.Log(StringFormat.Format(Resources.Core_Util_ArchiveUnpackerHelper_UnpackLog, archivePath));

            try
            {
                using (var archive = SharpCompress.Archives.ArchiveFactory.Open(archivePath))
                {
                    double totalArchiveSize = 0d;
                    double processedBytes   = 0d;

                    foreach (var entry in archive.Entries)
                    {
                        totalArchiveSize += entry.Size;
                    }
                    totalArchiveSize = Math.Max(1, totalArchiveSize);

                    using (var reader = archive.ExtractAllEntries())
                    {
                        reader.EntryExtractionProgress += (sender, e) =>
                        {
                            progressMonitor.Progress = (processedBytes + e.ReaderProgress.BytesTransferred) / totalArchiveSize;
                        };

                        while (reader.MoveToNextEntry())
                        {
                            if (cancellationToken.IsCancellationRequested)
                            {
                                progressMonitor.StatusText = Resources.Core_Util_ArchiveUnpackerHelper_Canceled;
                                progressMonitor.Log(Resources.Core_Util_ArchiveUnpackerHelper_Canceled);
                                return(ExtractionResult.Canceled);
                            }

                            var entry = reader.Entry;
                            if (!entry.IsDirectory)
                            {
                                progressMonitor.StatusText = StringFormat.Format(Resources.Core_Util_ArchiveUnpackerHelper_UnpackLog, entry.Key);
                                progressMonitor.Log(StringFormat.Format(Resources.Core_Util_ArchiveUnpackerHelper_UnpackLog, entry.Key));

                                reader.WriteEntryToDirectory(destinationFolder, new ExtractionOptions()
                                {
                                    ExtractFullPath = true, Overwrite = true
                                });
                                processedBytes          += entry.Size;
                                progressMonitor.Progress = processedBytes / totalArchiveSize;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ExtractionFailedException(ex.Message, ex);
            }

            progressMonitor.StatusText = Resources.Core_Util_ArchiveUnpackerHelper_Complete;
            progressMonitor.Progress   = 1d;

            return(ExtractionResult.Complete);
        }