Created
June 28, 2014 03:45
-
-
Save hawksprite/17b1958e90fc53188f8f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Downloader { | |
public bool Finished = false; | |
public double TotalBytes = 0; | |
public double BytesRecieved = 0; | |
// Percent stuff | |
public float Progress { get { return ((float)Progress_Percent) / 100.0f; } } | |
public int Progress_Percent = 0; | |
private WebClient client; | |
private string _url; | |
private string _location; | |
public void Start(string url, string location) | |
{ | |
Finished = false; | |
_url = url; | |
_location = location; | |
// Setup the client | |
client = new WebClient(); | |
client.DownloadProgressChanged += client_DownloadProgressChanged; | |
client.DownloadFileCompleted += client_DownloadFileCompleted; | |
// Start the download | |
client.DownloadFileAsync(new Uri(url), location); | |
} | |
public void Unzip() | |
{ | |
// Unzips the file we downloaded | |
if (Finished) | |
{ | |
// If Finished | |
string fileName = Path.GetFileName(_location); | |
if (fileName.Contains(".zip")) | |
{ | |
// It's a zip file we downloaded. | |
// Begin unzipping it. | |
Progress_Percent = 0; | |
Finished = false; | |
int totalFiles = 0; | |
using (ZipFile zip = ZipFile.Read(_location)) | |
{ | |
try | |
{ | |
totalFiles = zip.Entries.Count; | |
if (totalFiles <= 0) { totalFiles = 1; } | |
for (int i = 0; i < zip.Count; i++) | |
{ | |
ZipEntry e = zip[i]; | |
e.Extract(Path.GetDirectoryName(_location), ExtractExistingFileAction.OverwriteSilently); | |
Progress_Percent = (int)((double)i / (double)zip.Count); | |
} | |
} | |
catch (Exception z) { Debug.Log(z.ToString()); } | |
} | |
try | |
{ | |
// Delete the zip | |
File.Delete(_location); | |
} | |
catch (Exception z) { } | |
Finished = true; | |
Progress_Percent = 100; | |
} | |
} | |
} | |
void client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) | |
{ | |
Finished = true; | |
} | |
void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) | |
{ | |
Progress_Percent = e.ProgressPercentage; | |
TotalBytes = e.TotalBytesToReceive; | |
BytesRecieved = e.BytesReceived; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment