diff --git a/NovetusTest_FileDownloader/.vs/NovetusTest_FileDownloader/v14/.suo b/NovetusTest_FileDownloader/.vs/NovetusTest_FileDownloader/v14/.suo index 40a57c2..ae37a1a 100644 Binary files a/NovetusTest_FileDownloader/.vs/NovetusTest_FileDownloader/v14/.suo and b/NovetusTest_FileDownloader/.vs/NovetusTest_FileDownloader/v14/.suo differ diff --git a/NovetusTest_FileDownloader/NovetusTest_FileDownloader/Downloader.cs b/NovetusTest_FileDownloader/NovetusTest_FileDownloader/Downloader.cs index 08c397e..2b6984f 100644 --- a/NovetusTest_FileDownloader/NovetusTest_FileDownloader/Downloader.cs +++ b/NovetusTest_FileDownloader/NovetusTest_FileDownloader/Downloader.cs @@ -1,32 +1,23 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Net; using System.Windows.Forms; using System.IO; -using System.ComponentModel; class Downloader { private string fileURL; private string fileName; - private string fullFileName; - private string fileEXT; private string fileFilter; private string downloadOutcome; private string downloadOutcomeAddText; private ProgressBar downloadProgress; private SaveFileDialog saveFileDialog1; - public Downloader(string url, string name, string ext, string filter, ProgressBar progress) + public Downloader(string url, string name, string filter, ProgressBar progress) { fileName = name; - fileEXT = ext; fileURL = url; fileFilter = filter; - fullFileName = fileName + fileEXT; downloadProgress = progress; } @@ -46,25 +37,18 @@ class Downloader saveFileDialog1 = new SaveFileDialog() { - FileName = fullFileName, + FileName = fileName, //"Compressed zip files (*.zip)|*.zip|All files (*.*)|*.*" Filter = fileFilter, - Title = "Save " + fullFileName + Title = "Save " + fileName }; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { try { - MessageBox.Show(saveFileDialog1.FileName); - - using (WebClient wc = new WebClient()) - { - wc.DownloadProgressChanged += wc_DownloadProgressChanged; - wc.DownloadFileAsync(new Uri(fileURL), saveFileDialog1.FileName); - } - - downloadOutcome = "File " + fullFileName + " downloaded!" + downloadOutcomeAddText; + int read = DownloadGZipFile(fileURL, saveFileDialog1.FileName); + downloadOutcome = "File " + Path.GetFileName(saveFileDialog1.FileName) + " downloaded! " + read + " bytes written!" + downloadOutcomeAddText; } catch (Exception ex) { @@ -73,6 +57,81 @@ class Downloader } } + 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 + 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/NovetusTest_FileDownloader/NovetusTest_FileDownloader/Form1.Designer.cs b/NovetusTest_FileDownloader/NovetusTest_FileDownloader/Form1.Designer.cs index 30ca180..bf627ea 100644 --- a/NovetusTest_FileDownloader/NovetusTest_FileDownloader/Form1.Designer.cs +++ b/NovetusTest_FileDownloader/NovetusTest_FileDownloader/Form1.Designer.cs @@ -34,15 +34,13 @@ this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.label2 = new System.Windows.Forms.Label(); - this.textBox3 = new System.Windows.Forms.TextBox(); - this.label3 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // button1 // - this.button1.Location = new System.Drawing.Point(36, 101); + this.button1.Location = new System.Drawing.Point(12, 80); this.button1.Name = "button1"; - this.button1.Size = new System.Drawing.Size(365, 23); + this.button1.Size = new System.Drawing.Size(206, 23); this.button1.TabIndex = 0; this.button1.Text = "download"; this.button1.UseVisualStyleBackColor = true; @@ -50,15 +48,15 @@ // // progressBar1 // - this.progressBar1.Location = new System.Drawing.Point(36, 72); + this.progressBar1.Location = new System.Drawing.Point(12, 51); this.progressBar1.Name = "progressBar1"; - this.progressBar1.Size = new System.Drawing.Size(365, 23); + this.progressBar1.Size = new System.Drawing.Size(206, 23); this.progressBar1.TabIndex = 1; // // label1 // this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(59, 13); + this.label1.Location = new System.Drawing.Point(33, 9); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(46, 13); this.label1.TabIndex = 2; @@ -66,14 +64,14 @@ // // textBox1 // - this.textBox1.Location = new System.Drawing.Point(36, 36); + this.textBox1.Location = new System.Drawing.Point(12, 25); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(100, 20); this.textBox1.TabIndex = 3; // // textBox2 // - this.textBox2.Location = new System.Drawing.Point(162, 36); + this.textBox2.Location = new System.Drawing.Point(118, 25); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(100, 20); this.textBox2.TabIndex = 4; @@ -81,35 +79,17 @@ // label2 // this.label2.AutoSize = true; - this.label2.Location = new System.Drawing.Point(201, 13); + this.label2.Location = new System.Drawing.Point(159, 9); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(18, 13); this.label2.TabIndex = 5; this.label2.Text = "url"; // - // textBox3 - // - this.textBox3.Location = new System.Drawing.Point(301, 36); - this.textBox3.Name = "textBox3"; - this.textBox3.Size = new System.Drawing.Size(100, 20); - this.textBox3.TabIndex = 6; - // - // label3 - // - this.label3.AutoSize = true; - this.label3.Location = new System.Drawing.Point(328, 13); - this.label3.Name = "label3"; - this.label3.Size = new System.Drawing.Size(40, 13); - this.label3.TabIndex = 7; - this.label3.Text = "file ext."; - // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(439, 139); - this.Controls.Add(this.label3); - this.Controls.Add(this.textBox3); + this.ClientSize = new System.Drawing.Size(233, 113); this.Controls.Add(this.label2); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); @@ -133,8 +113,6 @@ private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Label label2; - private System.Windows.Forms.TextBox textBox3; - private System.Windows.Forms.Label label3; } } diff --git a/NovetusTest_FileDownloader/NovetusTest_FileDownloader/Form1.cs b/NovetusTest_FileDownloader/NovetusTest_FileDownloader/Form1.cs index 0bdae76..4a3278d 100644 --- a/NovetusTest_FileDownloader/NovetusTest_FileDownloader/Form1.cs +++ b/NovetusTest_FileDownloader/NovetusTest_FileDownloader/Form1.cs @@ -19,7 +19,7 @@ namespace NovetusTest_FileDownloader private void button1_Click(object sender, EventArgs e) { - Downloader download = new Downloader(textBox2.Text, textBox1.Text, textBox3.Text, "All files (*.*)|*.*", progressBar1); + Downloader download = new Downloader(textBox2.Text, textBox1.Text, "Roblox Model (*.rbxm)|*.rbxm|Roblox Mesh (*.mesh)|*.mesh|PNG Image (*.png)|*.png|WAV Sound (*.wav)|*.wav", progressBar1); try {