|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Compression;
namespace QIP_Country_Match
{
class UpdateDatabase
{
string gzFileName;
System.Net.WebClient w;
System.Uri url1;
public UpdateDatabase()
{
gzFileName = Environment.GetEnvironmentVariable("TEMP") + "\\IpToCountry.gz";
w = new System.Net.WebClient();
url1 = new Uri(@"http://www.software77.net/cgi-bin/ip-country/geo-ip.pl?action=download");
}
public void Start()
{
ConnectAndDownload();
UncompressAndExit();
}
private void ConnectAndDownload()
{
//start downloading
ConnectAndDownload:
{
try
{
w.DownloadFile(url1, gzFileName);
}
catch (System.Net.WebException)
{
DialogResult dr = MessageBox.Show("Cannot connect to server!\nYou may not be connected to the internet or the server might be busy at this moment\nYou may need to try later\n\nNote: Our online database is locked everyday for new updates at 12 UTC, so please\nDO NOT attempt to update during this time!", "Error", MessageBoxButtons.RetryCancel);
if (dr == DialogResult.Retry)
goto ConnectAndDownload;
else
Application.Exit();
}
}
}
private void UncompressAndExit()
{
UncompressFile();
System.IO.File.Delete(gzFileName);
}
private void UncompressFile()
{
try
{
FileStream sourceFile = File.OpenRead(gzFileName);
FileStream destinationFile = File.Create(gzFileName.TrimEnd(".gz".ToCharArray()) + ".csv");
byte[] buffer = new byte[sourceFile.Length * 100];
int n;
using (GZipStream input = new GZipStream(sourceFile,
CompressionMode.Decompress, false))
{
n = input.Read(buffer, 0, buffer.Length);
destinationFile.Write(buffer, 0, n);
}
// Close the files.
sourceFile.Close();
destinationFile.Close();
}
catch (Exception) { }
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.