导出QQWry.Dat中IP地址到文件[C#]

类别:.NET开发 点击:0 评论:0 推荐:

灵感之源语:“如果写个格式转换代码就更好了^_^”
既然如此,那……
下载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
    }
}


本文地址:http://com.8s8s.com/it/it42177.htm