' Collection of code snippets by Arne Vajhøj 
' (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
Imports System
Imports System.IO
Imports System.Net
Imports System.Data
Imports System.Data.SqlClient
Imports ICSharpCode.SharpZipLib.Zip

Namespace E_Geo
    Public Class GeoLoad
        Private Const UrlWebHostingInfo As String = "http://ip-to-country.webhosting.info/downloads/ip-to-country.csv.zip"
        Private Const UrlMaxMind As String = "http://www.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip"
        Private Const WorkDir As String = "C:\e"
        Private Const ConnStr As String = "server=ARNEPC2;Integrated Security=SSPI;database=Test"

        Private Shared Sub Download(ByVal urlstr As String, ByVal zipfnm As String)
            Dim wc As WebClient = New WebClient
            wc.DownloadFile(urlstr, WorkDir + "\" + zipfnm)
        End Sub

        Private Shared Sub Unzip(ByVal zipfnm As String, ByVal csvfnm As String)
            Dim s As Stream = New FileStream(WorkDir + "\" + csvfnm, FileMode.Create, FileAccess.Write)
            Dim zis As ZipInputStream = New ZipInputStream(New FileStream(WorkDir + "\" + zipfnm, FileMode.Open, FileAccess.Read))
            Dim ze As ZipEntry = zis.GetNextEntry
            Dim b(100000) As Byte
            Dim n As Integer
            n = zis.Read(b, 0, b.Length)
            While n > 0
                s.Write(b, 0, n)
                n = zis.Read(b, 0, b.Length)
            End While
            zis.Close
            s.Close
        End Sub

        Private Shared Sub DbImport(ByVal csvfnm As String, ByVal fromcol As Integer, ByVal tocol As Integer, ByVal countrycol As Integer, ByVal tblnam As String)
            Dim con As SqlConnection = New SqlConnection(ConnStr)
            con.Open
            Dim drp As SqlCommand = New SqlCommand("DROP TABLE " + tblnam, con)
            Try
                drp.ExecuteNonQuery
            Catch e As Exception
                ' we will end here first time
            End Try
            Dim cre1 As SqlCommand = New SqlCommand("CREATE TABLE " + tblnam + "(id INTEGER NOT NULL, ip1 NUMERIC(10,0), ip2 NUMERIC(10,0), country CHAR(2), PRIMARY KEY(id))", con)
            cre1.ExecuteNonQuery
            Dim cre2 As SqlCommand = New SqlCommand("CREATE INDEX ixip1 ON " + tblnam + "(ip1)", con)
            cre2.ExecuteNonQuery
            Dim cre3 As SqlCommand = New SqlCommand("CREATE INDEX ixip2 ON " + tblnam + "(ip2)", con)
            cre3.ExecuteNonQuery
            Dim ins As SqlCommand = New SqlCommand("INSERT INTO " + tblnam + " VALUES(@id,@ip1,@ip2,@country)", con)
            ins.Parameters.Add("@id", SqlDbType.Int)
            ins.Parameters.Add("@ip1", SqlDbType.Decimal)
            ins.Parameters.Add("@ip2", SqlDbType.Decimal)
            ins.Parameters.Add("@country", SqlDbType.VarChar)
            Dim sr As StreamReader = New StreamReader(WorkDir + "\" + csvfnm)
            Dim n As Integer = 0
            Dim line As String
            line = sr.ReadLine
            While Not (line Is Nothing)
                n = n + 1
                Dim cols As String() = line.Split(",".ToCharArray)
                Dim fromval As Decimal = Decimal.Parse(cols(fromcol - 1).Replace("""", " ").Trim)
                Dim toval As Decimal = Decimal.Parse(cols(tocol - 1).Replace("""", " ").Trim)
                Dim code As String = cols(countrycol - 1).Replace("""", " ").Trim
                ins.Parameters("@id").Value = n
                ins.Parameters("@ip1").Value = fromval
                ins.Parameters("@ip2").Value = toval
                ins.Parameters("@country").Value = code
                ins.ExecuteNonQuery
                line = sr.ReadLine
            End While
            sr.Close
            con.Close
        End Sub

        Private Shared Sub LoadWebHostingInfo()
            Download(UrlWebHostingInfo, "whi.zip")
            Unzip("whi.zip", "whi.csv")
            DbImport("whi.csv", 1, 2, 3, "whi")
        End Sub

        Private Shared Sub LoadMaxMind()
            Download(UrlMaxMind, "mm.zip")
            Unzip("mm.zip", "mm.csv")
            DbImport("mm.csv", 3, 4, 5, "mm")
        End Sub

        Public Shared Sub Main(ByVal args As String())
            LoadWebHostingInfo
            LoadMaxMind
        End Sub
    End Class 
End Namespace
