From 16a4f28a27a61d1f11e49f833f41e64bfcfdc527 Mon Sep 17 00:00:00 2001 From: Bitl Date: Fri, 8 Nov 2019 19:14:48 -0700 Subject: [PATCH] added download class --- NovetusLauncher/.vs/NovetusLauncher/v14/.suo | Bin 133120 -> 160256 bytes NovetusLauncher/NovetusFuncs/Downloader.cs | 140 ++++++++++++++++++ .../NovetusFuncs/NovetusFuncs.projitems | 1 + 3 files changed, 141 insertions(+) create mode 100644 NovetusLauncher/NovetusFuncs/Downloader.cs diff --git a/NovetusLauncher/.vs/NovetusLauncher/v14/.suo b/NovetusLauncher/.vs/NovetusLauncher/v14/.suo index 70447da30bb3f7bc82586c5eb70300a45f020b04..bac4b8dfc3ebdaf61cfb316bf7c4d2e7110f7d8b 100644 GIT binary patch delta 3210 zcmd6pe^8Xi8OPt}y&u4Nhle1Xig@x%R8)c>*b;&Vs6z}vV>8t%1Lm|QUf@Jh5=8+` zYC^3dOW2rbz*o5@)eyax;hiRJs#Rt#(~OLh)=ugqfsXAoiCXQ@G-~SgyWD$7Z8OuE z{?k1^xBEQ%?6bSiv%6e#TV(TW$VD(1i2))e z)h=18w0_N$4;RFaLA_1bFrq?1t;beug zAr>hTY#q%;23jH;Li_)l4Zmfx95W3kG!aY(V!3xuSMMeNq|?s8+6a= z_Gv$ql~h@A8vX7PvWn5-p4{`YvBH`Kg-CE%X;L8s)LRLI6e)5vroRqhNWC>HLRQww zN|M~Z>HMh2@U0cARYx{isXYt(5cgCJ6=k%iKC72laPy@>aB$l=JuB=)yfqLu_9*~E806%br&3_$-8yqJ*b}tFM$2x?oDpj zdxR@d806POw=#0NaNDCBD5$%}8J`$EZYQC6dz|(Z+J0T`AEy_tHON2Hb?=}a3{8_o zpN1NVO+(O5)h)ybQT_<|v-@3*#Vxx0Vr(ua=L+n(QWAZ{MazHLv z1o8l`ZVHWyT9qTbe^LMM3z+d=EkJo(7JqAv$|j?-!P35J#C#P4`_`+sSD8sEk^P=H zW7KXuv}LZxiYwXh`6g~BIZOS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=P)O(tH;Z!e@I8?$4 z^}+!vsUw)`WZs##Em!co&J*-|GcBYQ5L3r89J@MR!A+4MUz=c42>+fy#yr983{MCK zMef9$?t#Do+XM6j^5sm0wFg$*8-5?q7xV*sw>yDa_Dt?iUR$^xKIONF>e4txk1M-j z4wJL|`?tyltZXBY|NQ^oDiERdRv8eyRc@n4{x$#oTSadvVT$^;qAp`aa<3%TYTs?*Kb&bj11#LN^ ztdM2s8OQ60`;HaqMYnvYAL!zFtM}>!_x45b(v7e3_S@R?S(_7$=VIv)<1wdI-rQg_ zb}2N9_^9<;jr$R#DAha0n0c03tk?LR$|GggO)Zz1K4#ZCMKlj4#qjl{BHHI5A2WPi z;EJ>9lwzdxp-ZvaVN$%q3ycC6?TZkN#9W5!935mv%Lrl-S_UcFM36&@=-{DbBY7HK zAuW%XxKd89h~~94+<5jX)sk>ur6r@AckHfm%OM^jmvG5&SZNUlhmOKF-5lbls4PoO@{C{oGSt!f0d zJfCmM#TRpqN!nqiY#eGfKH{?Ar=xeM=^2csn_L(j(%P4c@u%Sr$H#~ZejX|_O)ha} z1obv%UBE}Xwey6w|5t}elrN%_0t;ConpaVz*l`haW7FG6jErOZwc}XS>_X})8tSQ- zO2y@R8Z36!Q>GZ%jnx_BuTUP-PLri=Yc(k>Qn;%qP5g9?V*g5_s+o_fVVt-_{mr6u z6TK%IswvmlAI_pJ;^q!Y5G73%WAtgF^I_UJ#?u zQMjQ9w6+<}wp}h*L=7UB({sW;6Sek96eV&v_VX~j8_V*Ab2YW6c+owTy{X-!sNVs) C?6;Qy diff --git a/NovetusLauncher/NovetusFuncs/Downloader.cs b/NovetusLauncher/NovetusFuncs/Downloader.cs new file mode 100644 index 0000000..49e0eb9 --- /dev/null +++ b/NovetusLauncher/NovetusFuncs/Downloader.cs @@ -0,0 +1,140 @@ +using System; +using System.Net; +using System.Windows.Forms; +using System.IO; + +class Downloader +{ + private string fileURL; + private string fileName; + private string fileFilter; + private string downloadOutcome; + private string downloadOutcomeAddText; + private ProgressBar downloadProgress; + private SaveFileDialog saveFileDialog1; + + public Downloader(string url, string name, string filter, ProgressBar progress) + { + fileName = name; + fileURL = url; + fileFilter = filter; + downloadProgress = progress; + } + + public void setDownloadOutcome(string text) + { + downloadOutcome = text; + } + + public string getDownloadOutcome() + { + return downloadOutcome; + } + + public void InitDownload(string additionalText = "") + { + downloadOutcomeAddText = additionalText; + + saveFileDialog1 = new SaveFileDialog() + { + FileName = fileName, + //"Compressed zip files (*.zip)|*.zip|All files (*.*)|*.*" + Filter = fileFilter, + Title = "Save " + fileName + }; + + if (saveFileDialog1.ShowDialog() == DialogResult.OK) + { + try + { + int read = DownloadGZipFile(fileURL, saveFileDialog1.FileName); + downloadOutcome = "File " + Path.GetFileName(saveFileDialog1.FileName) + " downloaded! " + read + " bytes written!" + downloadOutcomeAddText; + } + catch (Exception ex) + { + downloadOutcome = "Error when downloading file: " + ex.Message; + } + } + } + // currently this only DLs GZipped files. Will mod it to download both types later on. - Bitl + public static int DownloadGZipFile(string remoteFilename, string localFilename) + { + //credit to Tom Archer (https://www.codeguru.com/columns/dotnettips/article.php/c7005/Downloading-Files-with-the-WebRequest-and-WebResponse-Classes.htm) + //and Brokenglass (https://stackoverflow.com/questions/4567313/uncompressing-gzip-response-from-webclient/4567408#4567408) + + // Function will return the number of bytes processed + // to the caller. Initialize to 0 here. + int bytesProcessed = 0; + + // Assign values to these objects here so that they can + // be referenced in the finally block + Stream remoteStream = null; + Stream localStream = null; + WebResponse response = null; + + // Use a try/catch/finally block as both the WebRequest and Stream + // classes throw exceptions upon error + try + { + // Create a request for the specified remote file name + //WebRequest request = WebRequest.Create(remoteFilename); + var request = (HttpWebRequest)WebRequest.Create(remoteFilename); + request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate"); + request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; + if (request != null) + { + // Send the request to the server and retrieve the + // WebResponse object + response = request.GetResponse(); + if (response != null) + { + // Once the WebResponse object has been retrieved, + // get the stream object associated with the response's data + remoteStream = response.GetResponseStream(); + + // Create the local file + localStream = File.Create(localFilename); + + // Allocate a 1k buffer + byte[] buffer = new byte[1024]; + int bytesRead; + + // Simple do/while loop to read from stream until + // no bytes are returned + do + { + // Read data (up to 1k) from the stream + bytesRead = remoteStream.Read(buffer, 0, buffer.Length); + + // Write the data to the local file + localStream.Write(buffer, 0, bytesRead); + + // Increment total bytes processed + bytesProcessed += bytesRead; + } while (bytesRead > 0); + } + } + } + catch (Exception e) + { + Console.WriteLine(e.Message); + } + finally + { + // Close the response and streams objects here + // to make sure they're closed even if an exception + // is thrown at some point + if (response != null) response.Close(); + if (remoteStream != null) remoteStream.Close(); + if (localStream != null) localStream.Close(); + } + + // Return total bytes processed to caller. + return bytesProcessed; + } + + void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) + { + downloadProgress.Value = e.ProgressPercentage; + } +} diff --git a/NovetusLauncher/NovetusFuncs/NovetusFuncs.projitems b/NovetusLauncher/NovetusFuncs/NovetusFuncs.projitems index 3dc8447..ce6771c 100644 --- a/NovetusLauncher/NovetusFuncs/NovetusFuncs.projitems +++ b/NovetusLauncher/NovetusFuncs/NovetusFuncs.projitems @@ -14,6 +14,7 @@ +