在不上网的情况下。知道某一ip地址获得相应的地理位置。调用一位大侠的写的cs文件。
自己用这个类编写相应的分析.log日志的信息内容。
首先获取相应的ip对应的数据文件。下载这个版本。纯真版QQ IP数据库。里边有一个文件为:QQWry.Dat。这个文件就是存放ip与地理位置相对应的文件。把文件保存在f盘的根目录下
http://www.cz88.net/fox/
第二步:调用相应的.cs文件
其相应的调用方式为:
BDQQ.Data.QQWry qq = new BDQQ.Data.QQWry("f:\\QQWry.Dat");
BDQQ.Data.IPLocation ip = qq.SearchIPLocation(textBox1.Text);//这里添写IP地址
string country = ip.country;
string area = ip.area;
下面为一位大侠写的cs文件。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.IO;
3
using System.Text;
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
namespace BDQQ.Data
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
9
/// QQWry 的摘要说明。
10
/// </summary>
11
public class QQWry
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
//第一种模式
14![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
第一种模式#region 第一种模式
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
16![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
17
/// 第一种模式
18
/// </summary>
19
#endregion
20
private const byte REDIRECT_MODE_1 = 0x01;
21![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
//第二种模式
23![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
第二种模式#region 第二种模式
24![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
26
/// 第二种模式
27
/// </summary>
28
#endregion
29
private const byte REDIRECT_MODE_2 = 0x02;
30![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
//每条记录长度
32![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
每条记录长度#region 每条记录长度
33![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
34![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
35
/// 每条记录长度
36
/// </summary>
37
#endregion
38
private const int IP_RECORD_LENGTH = 7;
39![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
//数据库文件
41![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
数据库文件#region 数据库文件
42![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
43![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
44
/// 文件对象
45
/// </summary>
46
#endregion
47
private FileStream ipFile;
48![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
private const string unCountry = "未知国家";
50
private const string unArea = "未知地区";
51![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
//索引开始位置
53![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
索引开始位置#region 索引开始位置
54![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
55![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
56
/// 索引开始位置
57
/// </summary>
58
#endregion
59
private long ipBegin;
60![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
//索引结束位置
62![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
索引结束位置#region 索引结束位置
63![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
64![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
65
/// 索引结束位置
66
/// </summary>
67
#endregion
68
private long ipEnd;
69![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
//IP地址对象
71![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
IP地址对象#region IP地址对象
72![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
73![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
74
/// IP对象
75
/// </summary>
76
#endregion
77
private IPLocation loc;
78![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
79
//存储文本内容
80![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
存储文本内容#region 存储文本内容
81![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
82![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
83
/// 存储文本内容
84
/// </summary>
85
#endregion
86
private byte[] buf;
87![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
88
//存储3字节
89![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
存储3字节#region 存储3字节
90![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
91![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
92
/// 存储3字节
93
/// </summary>
94
#endregion
95
private byte[] b3;
96![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
97
//存储4字节
98![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
存储4字节#region 存储4字节
99![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
100![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
101
/// 存储4字节IP地址
102
/// </summary>
103
#endregion
104
private byte[] b4;
105![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
106
//构造函数
107![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
构造函数#region 构造函数
108![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
109![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
110
/// 构造函数
111
/// </summary>
112
/// <param name="ipfile">IP数据库文件绝对路径</param>
113
#endregion
114
public QQWry(string ipfile)
115![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
116![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
117
buf = new byte[100];
118
b3 = new byte[3];
119
b4 = new byte[4];
120
try
121![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
122
ipFile = new FileStream(ipfile, FileMode.Open);
123
}
124
catch (Exception ex)
125![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
126
throw new Exception(ex.ToString());
127
}
128
ipBegin = readLong4(0);
129
ipEnd = readLong4(4);
130
loc = new IPLocation();
131
}
132![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
133![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
134
//根据IP地址搜索
135![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
根据IP地址搜索#region 根据IP地址搜索
136![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
137![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
138
/// 搜索IP地址搜索
139
/// </summary>
140
/// <param name="ip"></param>
141
/// <returns></returns>
142
#endregion
143
public IPLocation SearchIPLocation(string ip)
144![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
145
//将字符IP转换为字节
146
string[] ipSp = ip.Split('.');
147
if (ipSp.Length != 4)
148![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
149
throw new ArgumentOutOfRangeException("不是合法的IP地址!");
150
}
151
byte[] IP = new byte[4];
152
for (int i = 0; i < IP.Length; i++)
153![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
154
IP[i] = (byte)(Int32.Parse(ipSp[i]) & 0xFF);
155
}
156![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
157
IPLocation local = null;
158
long offset = locateIP(IP);
159![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
160
if (offset != -1)
161![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
162
local = getIPLocation(offset);
163
}
164![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
165
if (local == null)
166![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
167
local = new IPLocation();
168
local.area = unArea;
169
local.country = unCountry;
170
}
171
return local;
172
}
173![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
174![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
175
//取得具体信息
176![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
取得具体信息#region 取得具体信息
177![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
178![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
179
/// 取得具体信息
180
/// </summary>
181
/// <param name="offset"></param>
182
/// <returns></returns>
183
#endregion
184
private IPLocation getIPLocation(long offset)
185![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
186
ipFile.Position = offset + 4;
187
//读取第一个字节判断是否是标志字节
188
byte one = (byte)ipFile.ReadByte();
189
if (one == REDIRECT_MODE_1)
190![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
191
//第一种模式
192
//读取国家偏移
193
long countryOffset = readLong3();
194
//转至偏移处
195
ipFile.Position = countryOffset;
196
//再次检查标志字节
197
byte b = (byte)ipFile.ReadByte();
198
if (b == REDIRECT_MODE_2)
199![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
200
loc.country = readString(readLong3());
201
ipFile.Position = countryOffset + 4;
202
}
203
else
204
loc.country = readString(countryOffset);
205![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
206
//读取地区标志
207
loc.area = readArea(ipFile.Position);
208![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
209
}
210
else if (one == REDIRECT_MODE_2)
211![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
212
//第二种模式
213
loc.country = readString(readLong3());
214
loc.area = readArea(offset + 8);
215
}
216
else
217![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
218
//普通模式
219
loc.country = readString(--ipFile.Position);
220
loc.area = readString(ipFile.Position);
221
}
222
return loc;
223
}
224![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
225![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
226
//取得地区信息
227![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
取得地区信息#region 取得地区信息
228![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
229![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
230
/// 读取地区名称
231
/// </summary>
232
/// <param name="offset"></param>
233
/// <returns></returns>
234
#endregion
235
private string readArea(long offset)
236![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
237
ipFile.Position = offset;
238
byte one = (byte)ipFile.ReadByte();
239
if (one == REDIRECT_MODE_1 || one == REDIRECT_MODE_2)
240![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
241
long areaOffset = readLong3(offset + 1);
242
if (areaOffset == 0)
243
return unArea;
244
else
245![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
246
return readString(areaOffset);
247
}
248
}
249
else
250![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
251
return readString(offset);
252
}
253
}
254![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
255![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
256
//读取字符串
257![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
读取字符串#region 读取字符串
258![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
259![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
260
/// 读取字符串
261
/// </summary>
262
/// <param name="offset"></param>
263
/// <returns></returns>
264
#endregion
265
private string readString(long offset)
266![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
267
ipFile.Position = offset;
268
int i = 0;
269
for (i = 0, buf[i] = (byte)ipFile.ReadByte(); buf[i] != (byte)(0); buf[++i] = (byte)ipFile.ReadByte()) ;
270![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
271
if (i > 0)
272
return Encoding.Default.GetString(buf, 0, i);
273
else
274
return "";
275
}
276![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
277![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
278
//查找IP地址所在的绝对偏移量
279![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
查找IP地址所在的绝对偏移量#region 查找IP地址所在的绝对偏移量
280![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
281![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
282
/// 查找IP地址所在的绝对偏移量
283
/// </summary>
284
/// <param name="ip"></param>
285
/// <returns></returns>
286
#endregion
287
private long locateIP(byte[] ip)
288![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
289
long m = 0;
290
int r;
291![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
292
//比较第一个IP项
293
readIP(ipBegin, b4);
294
r = compareIP(ip, b4);
295
if (r == 0)
296
return ipBegin;
297
else if (r < 0)
298
return -1;
299
//开始二分搜索
300
for (long i = ipBegin, j = ipEnd; i < j; )
301![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
302
m = this.getMiddleOffset(i, j);
303
readIP(m, b4);
304
r = compareIP(ip, b4);
305
if (r > 0)
306
i = m;
307
else if (r < 0)
308![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
309
if (m == j)
310![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
311
j -= IP_RECORD_LENGTH;
312
m = j;
313
}
314
else
315![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
316
j = m;
317
}
318
}
319
else
320
return readLong3(m + 4);
321
}
322
m = readLong3(m + 4);
323
readIP(m, b4);
324
r = compareIP(ip, b4);
325
if (r <= 0)
326
return m;
327
else
328
return -1;
329
}
330![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
331![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
332
//读出4字节的IP地址
333![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
读出4字节的IP地址#region 读出4字节的IP地址
334![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
335![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
336
/// 从当前位置读取四字节,此四字节是IP地址
337
/// </summary>
338
/// <param name="offset"></param>
339
/// <param name="ip"></param>
340
#endregion
341
private void readIP(long offset, byte[] ip)
342![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
343
ipFile.Position = offset;
344
ipFile.Read(ip, 0, ip.Length);
345
byte tmp = ip[0];
346
ip[0] = ip[3];
347
ip[3] = tmp;
348
tmp = ip[1];
349
ip[1] = ip[2];
350
ip[2] = tmp;
351
}
352![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
353![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
354
//比较IP地址是否相同
355![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
比较IP地址是否相同#region 比较IP地址是否相同
356![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
357![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
358
/// 比较IP地址是否相同
359
/// </summary>
360
/// <param name="ip"></param>
361
/// <param name="beginIP"></param>
362
/// <returns>0:相等,1:ip大于beginIP,-1:小于</returns>
363
#endregion
364
private int compareIP(byte[] ip, byte[] beginIP)
365![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
366
for (int i = 0; i < 4; i++)
367![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
368
int r = compareByte(ip[i], beginIP[i]);
369
if (r != 0)
370
return r;
371
}
372
return 0;
373
}
374![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
375![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
376
//比较两个字节是否相等
377![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
比较两个字节是否相等#region 比较两个字节是否相等
378![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
379![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
380
/// 比较两个字节是否相等
381
/// </summary>
382
/// <param name="bsrc"></param>
383
/// <param name="bdst"></param>
384
/// <returns></returns>
385
#endregion
386
private int compareByte(byte bsrc, byte bdst)
387![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
388
if ((bsrc & 0xFF) > (bdst & 0xFF))
389
return 1;
390
else if ((bsrc ^ bdst) == 0)
391
return 0;
392
else
393
return -1;
394
}
395![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
396![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
397
//根据当前位置读取4字节
398![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
根据当前位置读取4字节#region 根据当前位置读取4字节
399![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
400![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
401
/// 从当前位置读取4字节,转换为长整型
402
/// </summary>
403
/// <param name="offset"></param>
404
/// <returns></returns>
405
#endregion
406
private long readLong4(long offset)
407![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
408
long ret = 0;
409
ipFile.Position = offset;
410
ret |= (ipFile.ReadByte() & 0xFF);
411
ret |= ((ipFile.ReadByte() << 8) & 0xFF00);
412
ret |= ((ipFile.ReadByte() << 16) & 0xFF0000);
413
ret |= ((ipFile.ReadByte() << 24) & 0xFF000000);
414
return ret;
415
}
416![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
417![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
418
//根据当前位置,读取3字节
419![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
根据当前位置,读取3字节#region 根据当前位置,读取3字节
420![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
421![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
422
/// 根据当前位置,读取3字节
423
/// </summary>
424
/// <param name="offset"></param>
425
/// <returns></returns>
426
#endregion
427
private long readLong3(long offset)
428![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
429
long ret = 0;
430
ipFile.Position = offset;
431
ret |= (ipFile.ReadByte() & 0xFF);
432
ret |= ((ipFile.ReadByte() << 8) & 0xFF00);
433
ret |= ((ipFile.ReadByte() << 16) & 0xFF0000);
434
return ret;
435
}
436![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
437![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
438
//从当前位置读取3字节
439![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
从当前位置读取3字节#region 从当前位置读取3字节
440![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
441![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
442
/// 从当前位置读取3字节
443
/// </summary>
444
/// <returns></returns>
445
#endregion
446
private long readLong3()
447![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
448
long ret = 0;
449
ret |= (ipFile.ReadByte() & 0xFF);
450
ret |= ((ipFile.ReadByte() << 8) & 0xFF00);
451
ret |= ((ipFile.ReadByte() << 16) & 0xFF0000);
452
return ret;
453
}
454![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
455![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
456
//取得begin和end之间的偏移量
457![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
取得begin和end之间的偏移量#region 取得begin和end之间的偏移量
458![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
459![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
460
/// 取得begin和end中间的偏移
461
/// </summary>
462
/// <param name="begin"></param>
463
/// <param name="end"></param>
464
/// <returns></returns>
465
#endregion
466
private long getMiddleOffset(long begin, long end)
467![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
468
long records = (end - begin) / IP_RECORD_LENGTH;
469
records >>= 1;
470
if (records == 0)
471
records = 1;
472
return begin + records * IP_RECORD_LENGTH;
473
}
474
} //class QQWry
475![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
476
public class IPLocation
477![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
478
public String country;
479
public String area;
480![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
481
public IPLocation()
482![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
483
country = area = "";
484
}
485![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
486
public IPLocation getCopy()
487![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
488
IPLocation ret = new IPLocation();
489
ret.country = country;
490
ret.area = area;
491
return ret;
492
}
493
}
494
}
495![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
第三步:这是自己的文件用来分析一个相应的.log日志文件。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Text;
7
using System.Windows.Forms;
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
using System.IO;
10![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
namespace IpAddress
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
13
public partial class Form1 : Form
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15
//保存路径
16
string path = "null";
17
StrText st;
18![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
public Form1()
20![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
21
InitializeComponent();
22
path = "";
23![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
//皮肤
25
skinEngine1.SkinFile = "Page.ssk";
26
}
27![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
29![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
30
//路径信息
31
string path;
32![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
//文本内容
34
string strText;
35![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
openFileDialog1.FileName = "*.log";
37
openFileDialog1.Filter = "文本文件(*.log)|*.log";
38
openFileDialog1.RestoreDirectory = true;
39
openFileDialog1.FilterIndex = 1;
40
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
42
FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
43
FileInfo info = new FileInfo(openFileDialog1.FileName);
44
StreamReader sr = new StreamReader(openFileDialog1.FileName, Encoding.Default);
45
path = openFileDialog1.FileName;
46
//richTextBox1.Text = sr.ReadToEnd();
47
strText = sr.ReadToEnd();
48
R(strText);
49
sr.Close();
50
}
51
}
52![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
//private void Read(string strText)
54
//{
55
// richTextBox1.Text = "";
56
// int i = strText.Length;
57![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
// //计算共有多少行
59
// int num=0;
60![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
// //把字符串转化为字符数组
62
// char[] myChar = new char[i];
63![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
// //寻找enzhuji的位置的数组
65
// char[] enChar=new char[7];
66![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
// //访问时间
68
// string time;
69
70
// //访问地点
71
// string local;
72
73
// //记录访问页面
74
// string tail;
75![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
76
// //保存结果
77
// StringBuilder sb = new StringBuilder();
78![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
79![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
80
// enChar[0]='E';
81
// enChar[1]='N';
82
// enChar[2]='Z';
83
// enChar[3]='H';
84
// enChar[4]='U';
85
// enChar[5]='J';
86
// enChar[6]='I';
87![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
88
// BDQQ.Data.QQWry qq = new BDQQ.Data.QQWry("d:\\QQWry.Dat");
89![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
90
// myChar = strText.ToCharArray();
91
92
// foreach (char ch in myChar)
93
// {
94
// if (ch == '\n')
95
// num++;
96
// }
97![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
98
// string [] myStr=new string [num];
99
100
// myStr=strText .Split('\n');
101![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
102
// for (int j = 0; j < num; j++)
103
// {
104
// //计算每行的长度
105
// int n = myStr[j].Length;
106![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
107
// //把每行转换成字符数组
108
// char [] chaAr=new char [n];
109
// chaAr = myStr[j].ToCharArray();
110![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
111
// //如果倒数第六个字符为m则认为访问了htm页面
112
// if (chaAr[n - 6] == 'm')
113
// {
114
// //计算enzhuji的位置
115
// int address = myStr[j].IndexOfAny(enChar);
116
117
// //求出时间
118
// time = myStr[j].Substring(address + 15, 20);
119
120
// //求出地址
121
// local = myStr[j].Substring(address + 35, 16);
122![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
123
// int loc=local.LastIndexOf(' ');
124![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
125
// //求出ip信息
126
// local = local.Substring(0, loc);
127
// tail = myStr[j].Substring(myStr[j].Length-16,11);
128
129
// BDQQ.Data.IPLocation ip = qq.SearchIPLocation(local);//这里添写IP地址
130![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
131
// string country = ip.country;
132
// string area = ip.area;
133![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
134
// sb.Append(time + " " +local+" "+ tail+" "+country+" "+area + "\n");
135
136
// }
137
// }
138![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
139
// richTextBox1.Text = sb.ToString();
140
//}
141![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
142
private void R(string strText)
143![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
144
string str;
145
richTextBox1.Text = "";
146
st = new StrText(strText);
147
str = st.Read();
148
richTextBox1.Text = str;
149
}
150![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
151
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
152![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
153
if (this.path.Equals("") == true)
154![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
155
SaveFileDialog sf = new SaveFileDialog();
156
sf.Title = "保存";
157
sf.InitialDirectory = "c:\\";
158
sf.Filter = "All files (*.*)|*.*|文本文档 (*.txt)|*.txt";
159
sf.FilterIndex = 1;
160
if (sf.ShowDialog() == DialogResult.OK)
161![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
162
StreamWriter asw = new StreamWriter(sf.FileName, false, Encoding.Default);
163
asw.Write(richTextBox1.Text);
164
asw.Close();
165
}
166![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
167
}
168
else
169![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
170
StreamWriter sw = new StreamWriter(path, false, Encoding.Default);
171
sw.Write(richTextBox1.Text);
172
sw.Close();
173
}
174
}
175![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
176
private void 查询IpToolStripMenuItem_Click(object sender, EventArgs e)
177![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
178
Form2 fr = new Form2();
179
fr.ShowDialog();
180
}
181
}
182
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
namespace IpAddress
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
7
class StrText
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
private string strText;
10
11
public StrText(string strText)
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
this.strText = strText;
14
}
15![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
public string Read()
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
18
int i = strText.Length;
19![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
//计算共有多少行
21
int num = 0;
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
//把字符串转化为字符数组
24
char[] myChar = new char[i];
25![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
//寻找enzhuji的位置的数组
27
char[] enChar = new char[7];
28![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
//访问时间
30
string time;
31![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
//访问地点
33
string local;
34![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
//记录访问页面
36
string tail;
37![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
//保存结果
39
StringBuilder sb = new StringBuilder();
40![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
enChar[0] = 'E';
43
enChar[1] = 'N';
44
enChar[2] = 'Z';
45
enChar[3] = 'H';
46
enChar[4] = 'U';
47
enChar[5] = 'J';
48
enChar[6] = 'I';
49![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
BDQQ.Data.QQWry qq = new BDQQ.Data.QQWry("d:\\QQWry.Dat");
51![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
myChar = strText.ToCharArray();
53![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
foreach (char ch in myChar)
55![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
56
if (ch == '\n')
57
num++;
58
}
59![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
string[] myStr = new string[num];
61![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
myStr = strText.Split('\n');
63![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
for (int j = 0; j < num; j++)
65![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
66
//计算每行的长度
67
int n = myStr[j].Length;
68![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
//把每行转换成字符数组
70
char[] chaAr = new char[n];
71
chaAr = myStr[j].ToCharArray();
72![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
//如果倒数第六个字符为m则认为访问了htm页面
74
if (chaAr[n - 6] == 'm')
75![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
76
//计算enzhuji的位置
77
int address = myStr[j].IndexOfAny(enChar);
78![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
79
//求出时间
80
time = myStr[j].Substring(address + 15, 20);
81![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
82
//求出地址
83
local = myStr[j].Substring(address + 35, 16);
84![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
85
int loc = local.LastIndexOf(' ');
86![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
87
//求出ip信息
88
local = local.Substring(0, loc);
89
tail = myStr[j].Substring(myStr[j].Length - 16, 11);
90![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
91
BDQQ.Data.IPLocation ip = qq.SearchIPLocation(local);//这里添写IP地址
92![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
93
string country = ip.country;
94
string area = ip.area;
95![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
96
sb.Append(time + " " + local + " " + tail + " " + country + " " + area + "\n");
97![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
98
}
99
}
100![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
101
return sb.ToString();
102
}
103
}
104
}
105![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)