1- using System . Net . Http . Handlers ;
1+ using Downloader ;
2+ using SharpCompress . Archives . SevenZip ;
3+ using SharpCompress . Common ;
4+ using SharpCompress . Readers ;
25
36namespace FlashpointInstaller
47{
58 public partial class Install : Form
69 {
7- private static ProgressMessageHandler handler = new ( new HttpClientHandler ( ) { AllowAutoRedirect = true } ) ;
8- private HttpClient client = new ( handler ) ;
10+ DownloadService downloader = new ( new DownloadConfiguration ( ) ) ;
11+
12+ Stream stream ;
13+ SevenZipArchive archive ;
14+ IReader reader ;
15+
16+ string lastEntry = "" ;
17+ long extractedSize = 0 ;
918
1019 public Install ( )
1120 {
@@ -14,34 +23,79 @@ public Install()
1423
1524 private async void Install_Load ( object sender , EventArgs e )
1625 {
17- handler . HttpReceiveProgress += Install_ReceiveProgress ;
26+ downloader . DownloadStarted += OnDownloadStarted ;
27+ downloader . DownloadProgressChanged += OnDownloadProgressChanged ;
1828
19- byte [ ] fpData = await client . GetByteArrayAsync ( "http://localhost/Flashpoint%2010.1%20Infinity.7z" ) ;
20- await File . WriteAllBytesAsync ( @"fp.tmp" , fpData ) ;
29+ stream = await downloader . DownloadFileTaskAsync ( "http://localhost/Flashpoint%2011%20Infinity.7z" ) ;
30+
31+ await Task . Run ( ( ) =>
32+ {
33+ using ( archive = SevenZipArchive . Open ( stream ) )
34+ {
35+ using ( reader = archive . ExtractAllEntries ( ) )
36+ {
37+ reader . EntryExtractionProgress += OnEntryExtractionProgressChanged ;
38+ reader . WriteAllToDirectory ( ( ( Main ) Application . OpenForms [ "Main" ] ) . FolderText . Text , new ExtractionOptions { ExtractFullPath = true , Overwrite = true } ) ;
39+ }
40+ }
41+ } ) ;
42+ }
2143
22- Close ( ) ;
44+ private void OnDownloadStarted ( object ? sender , DownloadStartedEventArgs e )
45+ {
46+ Info . Invoke ( ( MethodInvoker ) delegate
47+ {
48+ Info . Text = $ "Downloaded 0MB of { Math . Round ( ( double ) e . TotalBytesToReceive / 1000000 ) } MB";
49+ } ) ;
2350 }
2451
25- private void Install_ReceiveProgress ( object sender , HttpProgressEventArgs e )
52+ private void OnDownloadProgressChanged ( object ? sender , DownloadProgressChangedEventArgs e )
2653 {
27- if ( this . IsHandleCreated )
54+ Progress . Invoke ( ( MethodInvoker ) delegate
2855 {
29- Progress . Invoke ( ( MethodInvoker ) delegate
30- {
31- Progress . Value = ( int ) ( ( e . BytesTransferred / e . TotalBytes ) * Progress . Maximum ) ;
32- } ) ;
33-
34- Info . Invoke ( ( MethodInvoker ) delegate
35- {
36- Info . Text = $ "Downloading... { e . BytesTransferred / 1000000 } of { e . TotalBytes / 1000000 } ";
37- } ) ;
56+ Progress . Value = ( int ) ( ( double ) e . ReceivedBytesSize / e . TotalBytesToReceive * ( Progress . Maximum / 2 ) ) ;
57+ } ) ;
58+
59+ Info . Invoke ( ( MethodInvoker ) delegate
60+ {
61+ Info . Text = $ "Downloaded { e . ReceivedBytesSize / 1000000 } MB of { e . TotalBytesToReceive / 1000000 } MB";
62+ } ) ;
63+ }
64+
65+ private void OnEntryExtractionProgressChanged ( object ? sender , ReaderExtractionEventArgs < IEntry > e )
66+ {
67+ if ( e . Item . Key != lastEntry )
68+ {
69+ extractedSize += e . Item . Size ;
70+ lastEntry = e . Item . Key ;
3871 }
72+
73+ long extractedSizeExact = extractedSize - ( e . Item . Size - e . ReaderProgress . BytesTransferred ) ;
74+
75+ Progress . Invoke ( ( MethodInvoker ) delegate
76+ {
77+ Progress . Value = ( Progress . Maximum / 2 ) + ( ( int ) ( ( double ) extractedSizeExact / archive . TotalUncompressSize * ( Progress . Maximum / 2 ) ) ) ;
78+ } ) ;
79+
80+ Info . Invoke ( ( MethodInvoker ) delegate
81+ {
82+ Info . Text = $ "Extracted { extractedSize / 1000000 } MB of { archive . TotalUncompressSize / 1000000 } MB";
83+ } ) ;
3984 }
4085
4186 private void Cancel_Click ( object sender , EventArgs e )
4287 {
43- client . DeleteAsync ( "http://localhost/Flashpoint%2010.1%20Infinity.7z" ) ;
44- client . CancelPendingRequests ( ) ;
88+ if ( downloader . IsBusy )
89+ {
90+ downloader . CancelAsync ( ) ;
91+ }
92+ else if ( reader != null )
93+ {
94+ reader . Cancel ( ) ;
95+ archive . Dispose ( ) ;
96+ stream . Dispose ( ) ;
97+ }
98+
4599 Close ( ) ;
46100 }
47101 }
0 commit comments