下载cs文件:http://www.cnblogs.com/files/rexsp/ipexport.rar
/**//******************************************************************
** file name:ipexport.cs
** copyright (c) 2004-2005 pptech studio(pptech.net)
** creater:rexsp(msn:[email protected])
** create date:2004-12-29 20:10:28
** modifier:
** modify date:
** description:to export the ip location from qqwry.dat
** version: ipexport 1.0.0
******************************************************************/
using system;
using system.collections;
using system.data;
using system.io;
namespace pptech.esp.component
{
/**//// <summary>
/// exportdata 的摘要说明。
/// </summary>
public class ipexport
{
私有成员#region 私有成员
private string country;
private string local;
private filestream objfs = null;
private long startip=0;
private long endip=0;
private int countryflag=0;
private long endipoff=0;
#endregion
构造函数#region 构造函数
public ipexport()
{
//
// todo: 在此处添加构造函数逻辑
//
}
#endregion
导出数据#region 导出数据
public void savetotext(string tofilepath,string fromfilepath)
{
objfs = new filestream(fromfilepath, filemode.open, fileaccess.read);
objfs.position=0;
byte[] buff1 = new byte[8] ;
objfs.read(buff1,0,8);
int firststartip=buff1[0]+buff1[1]*256+buff1[2]*256*256+buff1[3]*256*256*256;
int laststartip=buff1[4]*1+buff1[5]*256+buff1[6]*256*256+buff1[7]*256*256*256;
long recordcount=convert.toint64((laststartip-firststartip)/7.0);
if(recordcount<=1)
{
country="filedataerror";
objfs.close();
}
long range=recordcount;
streamwriter writer=file.appendtext(tofilepath);
for(int i=0;i<=recordcount;i++)
{
long offset = firststartip+i*7;
objfs.position=offset;
byte [] buff = new byte[7];
objfs.read(buff,0,7);
endipoff=convert.toint64(buff[4].tostring())+convert.toint64(buff[5].tostring())*256+convert.toint64(buff[6].tostring())*256*256;
startip=convert.toint64(buff[0].tostring())+convert.toint64(buff[1].tostring())*256+convert.toint64(buff[2].tostring())*256*256+convert.toint64(buff[3].tostring())*256*256*256;
objfs.position=endipoff;
byte [] buff3 = new byte[5];
objfs.read(buff3,0,5);
this.endip=convert.toint64(buff3[0].tostring())+convert.toint64(buff3[1].tostring())*256+convert.toint64(buff3[2].tostring())*256*256+convert.toint64(buff3[3].tostring())*256*256*256;
this.countryflag=buff3[4];
string showip=this.inttoip(startip);
this.getcountry();
writer.writeline(showip+" "+this.country+this.local);
}
writer.close();
}
#endregion
int转换成ip#region int转换成ip
private string inttoip(long ip_int)
{
long seg1=(ip_int&0xff000000)>>24;
if(seg1<0)
seg1+=0x100;
long seg2=(ip_int&0x00ff0000)>>16;
if(seg2<0)
seg2+=0x100;
long seg3=(ip_int&0x0000ff00)>>8;
if(seg3<0)
seg3+=0x100;
long seg4=(ip_int&0x000000ff);
if(seg4<0)
seg4+=0x100;
string ip=seg1.tostring()+"."+seg2.tostring()+"."+seg3.tostring()+"."+seg4.tostring();
return ip;
}
#endregion
获取国家/区域偏移量#region 获取国家/区域偏移量
private string getcountry()
{
switch(this.countryflag)
{
case 1:
case 2:
this.country=getflagstr(this.endipoff+4);
this.local=( 1 == this.countryflag )?" ":this.getflagstr(this.endipoff+8);
break;
default:
this.country=this.getflagstr(this.endipoff+4);
this.local=this.getflagstr(objfs.position);
break;
}
return " ";
}
#endregion
获取国家/区域字符串#region 获取国家/区域字符串
private string getflagstr(long offset)
{
int flag=0;
byte [] buff = new byte[3];
while(1==1)
{
//objfs.seek(offset,seekorigin.begin);
objfs.position=offset;
flag = objfs.readbyte();
if(flag==1||flag==2)
{
objfs.read(buff,0,3);
if(flag==2)
{
this.countryflag=2;
this.endipoff=offset-4;
}
offset=convert.toint64(buff[0].tostring())+convert.toint64(buff[1].tostring())*256+convert.toint64(buff[2].tostring())*256*256;
}
else
{
break;
}
}
if(offset<12)
return " ";
objfs.position=offset;
return getstr();
}
#endregion
getstr#region getstr
private string getstr()
{
byte lowc=0;
byte upc=0;
string str="";
byte[] buff = new byte[2];
while(1==1)
{
lowc= (byte)objfs.readbyte();
if(lowc==0)
break;
if(lowc>127)
{
upc=(byte)objfs.readbyte();
buff[0]=lowc;
buff[1]=upc;
system.text.encoding enc = system.text.encoding.getencoding("gb2312");
str+=enc.getstring(buff);
}
else
{
str+=(char)lowc;
}
}
return str;
}
#endregion
}
}
调用方式:
测试导出ip地址库#region 测试导出ip地址库
ipexport exp = new ipexport();
string tofile = @"d:/work/pptechstudio/ip.txt";
string [email protected]"e:/个人资料/imtools/qqwryupdate/qqwry.dat";
exp.savetotext(tofile,fromfile);
#endregion
新闻热点
疑难解答