Export IP addresses to files in QQWry.Dat [C #]

Source: Internet
Author: User
Tags constructor tostring
IP Address

Download cs File: Http://www.cnblogs.com/Files/rexsp/IPExport.rar
/**//******************************************************************
* * File Name:IPExport.cs
* * Copyright (c) 2004-2005 Pptech Studio (pptech.net)
* * CREATER:REXSP (msn:yubo@x263.net)
* * 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>
Summary description of the ExportData.
</summary>
public class Ipexport
{
Private member #region Private member
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

constructor function #region Constructor
Public Ipexport ()
{
//
TODO: Add constructor logic here
//
}
#endregion

Export Data #region Export data
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 converts to ip#region int to 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&AMP;0X0000FF00) >>8;
if (seg3<0)
seg3+=0x100;
Long seg4= (IP_INT&AMP;0X000000FF);
if (seg4<0)
seg4+=0x100;
String IP=SEG1. ToString () + "." +SEG2. ToString () + "." +seg3. ToString () + "." +seg4. ToString ();

return IP;
}
#endregion

Get Country/region offset #region Get country/region offset
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

Get country/region string #region Get country/region string
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
}
}


Call Mode:


Test export IP Address Library #region test export IP Address library
Ipexport exp = new Ipexport ();
string tofile = @ "D:\Work\PPTechStudio\ip.txt";
String fromfile=@ "e:\ personal Data \imtools\qqwryupdate\qqwry.dat";
Exp. Savetotext (Tofile,fromfile);
#endregion



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.