Use C # To export IP addresses in qqwry. dat to other files

Source: Internet
Author: User

/**//*********************************** *******************************
** 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 of 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 # region Constructor
Public ipexport ()
{
//
// Todo: add the 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; [Page]
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; [Page]
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 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 & 0x0000ff00)> 8;
If (seg3 <0)
Seg3 + = 0x100;
Long seg4 = (ip_int & 0x000000ff );
If (seg4 <0)
Seg4 + = 0x100; [Page]
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]; [Page]
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 ();
} [Page]
# 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; [Page]
}
# Endregion
}
}

Call method:

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

Download CS file: http://files.cnblogs.com/rexsp/IPExport.rar

Related Article

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.