用以前放在牛腩公用類庫里的IPSearch就行了忙厌,編譯沒有錯誤筋岛,不過直接用發(fā)現(xiàn)中文亂碼茫孔,一通亂改才發(fā)現(xiàn)問題淹冰,看最后的ReadString方法 库车,
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace Niunan.WebAssign.Util
{
///<summary>
/// 提供從純真IP數(shù)據(jù)庫搜索IP信息的方法;
/// 感謝LumaQQ提供純真IP數(shù)據(jù)庫格式文檔樱拴;
/// ----HeDaode 2007-12-28 四川教育學院
///</summary>
internal class IPSearch
{
FileStream ipFile;
long ip;
string ipfilePath;
///<summary>
/// 構(gòu)造函數(shù)
///</summary>
///<param name="ipfilePath">純真IP數(shù)據(jù)庫路徑</param>
public IPSearch(string ipfilePath)
{
this.ipfilePath = ipfilePath;
}
///<summary>
/// 地理位置,包括國家和地區(qū)
///</summary>
public struct IPLocation
{
public string country, area;
}
///<summary>
/// 獲取指定IP所在地理位置
///</summary>
///<param name="strIP">要查詢的IP地址</param>
///<returns></returns>
public IPLocation GetIPLocation(string strIP)
{
ip = IPToLong(strIP);
ipFile = new FileStream(ipfilePath, FileMode.Open, FileAccess.Read);
long[] ipArray = BlockToArray(ReadIPBlock());
long offset = SearchIP(ipArray, 0, ipArray.Length - 1) * 7 + 4;
ipFile.Position += offset;//跳過起始IP
ipFile.Position = ReadLongX(3) + 4;//跳過結(jié)束IP
IPLocation loc = new IPLocation();
int flag = ipFile.ReadByte();//讀取標志
if (flag == 1)//表示國家和地區(qū)被轉(zhuǎn)向
{
ipFile.Position = ReadLongX(3);
flag = ipFile.ReadByte();//再讀標志
}
long countryOffset = ipFile.Position;
loc.country = ReadString(flag);
if (flag == 2)
{
ipFile.Position = countryOffset + 3;
}
flag = ipFile.ReadByte();
loc.area = ReadString(flag);
ipFile.Close();
ipFile = null;
return loc;
}
///<summary>
/// 將字符串形式的IP轉(zhuǎn)換位long
///</summary>
///<param name="strIP"></param>
///<returns></returns>
public long IPToLong(string strIP)
{
byte[] ip_bytes = new byte[8];
string[] strArr = strIP.Split(new char[] { '.' });
for (int i = 0; i < 4; i++)
{
ip_bytes[i] = byte.Parse(strArr[3 - i]);
}
return BitConverter.ToInt64(ip_bytes, 0);
}
///<summary>
/// 將索引區(qū)字節(jié)塊中的起始IP轉(zhuǎn)換成Long數(shù)組
///</summary>
///<param name="ipBlock"></param>
long[] BlockToArray(byte[] ipBlock)
{
long[] ipArray = new long[ipBlock.Length / 7];
int ipIndex = 0;
byte[] temp = new byte[8];
for (int i = 0; i < ipBlock.Length; i += 7)
{
Array.Copy(ipBlock, i, temp, 0, 4);
ipArray[ipIndex] = BitConverter.ToInt64(temp, 0);
ipIndex++;
}
return ipArray;
}
///<summary>
/// 從IP數(shù)組中搜索指定IP并返回其索引
///</summary>
///<param name="ipArray">IP數(shù)組</param>
///<param name="start">指定搜索的起始位置</param>
///<param name="end">指定搜索的結(jié)束位置</param>
///<returns></returns>
int SearchIP(long[] ipArray, int start, int end)
{
int middle = (start + end) / 2;
if (middle == start)
return middle;
else if (ip < ipArray[middle])
return SearchIP(ipArray, start, middle);
else
return SearchIP(ipArray, middle, end);
}
///<summary>
/// 讀取IP文件中索引區(qū)塊
///</summary>
///<returns></returns>
byte[] ReadIPBlock()
{
long startPosition = ReadLongX(4);
long endPosition = ReadLongX(4);
long count = (endPosition - startPosition) / 7 + 1;//總記錄數(shù)
ipFile.Position = startPosition;
byte[] ipBlock = new byte[count * 7];
ipFile.Read(ipBlock, 0, ipBlock.Length);
ipFile.Position = startPosition;
return ipBlock;
}
///<summary>
/// 從IP文件中讀取指定字節(jié)并轉(zhuǎn)換位long
///</summary>
///<param name="bytesCount">需要轉(zhuǎn)換的字節(jié)數(shù)柠衍,主意不要超過8字節(jié)</param>
///<returns></returns>
long ReadLongX(int bytesCount)
{
byte[] _bytes = new byte[8];
ipFile.Read(_bytes, 0, bytesCount);
return BitConverter.ToInt64(_bytes, 0);
}
///<summary>
/// 從IP文件中讀取字符串
///</summary>
///<param name="flag">轉(zhuǎn)向標志</param>
///<returns></returns>
string ReadString(int flag)
{
if (flag == 1 || flag == 2)//轉(zhuǎn)向標志
ipFile.Position = ReadLongX(3);
else
ipFile.Position -= 1;
List<byte> list = new List<byte>();
byte b = (byte)ipFile.ReadByte();
while (b > 0)
{
list.Add(b);
b = (byte)ipFile.ReadByte();
}
return Encoding.GetEncoding("GB2312").GetString(list.ToArray());//.net core下用這個,要不然會出中文亂碼
//return Encoding.Default.GetString(list.ToArray()); //.netframework下用這個
}
}
}