程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 導出QQWry.Dat中IP地址到文件[C#]

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

編輯:關於C語言
/**//******************************************************************
** 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 fromFile=@"E:\個人資料\IMTools\QQwryUpdate\QQWry.Dat";
            exp.SaveToText(toFile,fromFile);
            #endregion

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved