forked from NewLifeX/X
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZip.cs
204 lines (177 loc) · 5.91 KB
/
Zip.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.IO;
using System.Text;
namespace NewLife.IP
{
class Zip : IDisposable
{
uint Index_Set;
uint Index_End;
uint Index_Count;
uint Search_Index_Set;
uint Search_Index_End;
IndexInfo Search_Set;
IndexInfo Search_Mid;
IndexInfo Search_End;
#region 属性
private Stream _Stream;
/// <summary>数据流</summary>
public Stream Stream { get { return _Stream; } set { _Stream = value; } }
#endregion
#region 构造
/// <summary>析构</summary>
~Zip() { OnDispose(false); }
/// <summary>销毁</summary>
public void Dispose() { OnDispose(true); }
void OnDispose(Boolean disposing)
{
if (_Stream != null) _Stream.Dispose();
if (disposing) GC.SuppressFinalize(this);
}
#endregion
#region 数据源
public Zip SetStream(Stream stream)
{
var ms = new MemoryStream();
var buf = new Byte[3];
stream.Read(buf, 0, buf.Length);
stream.Position -= 3;
// 仅支持Gzip压缩,可用7z软件先压缩为gz格式
if (buf[0] == 0x1F & buf[1] == 0x8B && buf[2] == 0x08)
IOHelper.DecompressGZip(stream, ms);
else
IOHelper.CopyTo(stream, ms);
ms.Position = 0;
_Stream = ms;
Index_Set = GetUInt32();
Index_End = GetUInt32();
Index_Count = (Index_End - Index_Set) / 7u + 1u;
return this;
}
#endregion
#region 方法
public String GetAddress(UInt32 ip)
{
if (_Stream == null) return "";
Search_Index_Set = 0u;
Search_Index_End = Index_Count - 1u;
while (true)
{
Search_Set = IndexInfoAtPos(Search_Index_Set);
Search_End = IndexInfoAtPos(Search_Index_End);
if (ip >= Search_Set.IpSet && ip <= Search_Set.IpEnd) break;
if (ip >= Search_End.IpSet && ip <= Search_End.IpEnd) return ReadAddressInfoAtOffset(Search_End.Offset);
Search_Mid = IndexInfoAtPos((Search_Index_End + Search_Index_Set) / 2u);
if (ip >= Search_Mid.IpSet && ip <= Search_Mid.IpEnd) return ReadAddressInfoAtOffset(Search_Mid.Offset);
if (ip < Search_Mid.IpSet)
Search_Index_End = (Search_Index_End + Search_Index_Set) / 2u;
else
Search_Index_Set = (Search_Index_End + Search_Index_Set) / 2u;
}
return ReadAddressInfoAtOffset(Search_Set.Offset);
}
String ReadAddressInfoAtOffset(uint Offset)
{
_Stream.Position = Offset + 4;
Byte tag = GetTag();
String addr;
String area;
if (tag == 1)
{
_Stream.Position = GetOffset();
tag = GetTag();
if (tag == 2)
{
uint offset = GetOffset();
area = ReadArea();
_Stream.Position = offset;
addr = ReadString();
}
else
{
_Stream.Position -= 1;
addr = ReadString();
area = ReadArea();
}
}
else
{
if (tag == 2)
{
uint offset = GetOffset();
area = ReadArea();
_Stream.Position = offset;
addr = ReadString();
}
else
{
_Stream.Position -= 1;
addr = ReadString();
area = ReadArea();
}
}
return (addr + " " + area).Trim();
}
uint GetOffset()
{
return BitConverter.ToUInt32(new Byte[]
{
(Byte)_Stream.ReadByte(),
(Byte)_Stream.ReadByte(),
(Byte)_Stream.ReadByte(),
0
}, 0);
}
String ReadArea()
{
Byte tag = GetTag();
if (tag == 1 || tag == 2)
_Stream.Position = GetOffset();
else
_Stream.Position -= 1;
return ReadString();
}
String ReadString()
{
var k = 0;
var buf = new Byte[256];
buf[k] = (Byte)_Stream.ReadByte();
while (buf[k] != 0)
{
k += 1;
buf[k] = (Byte)_Stream.ReadByte();
}
var str = Encoding.GetEncoding("GB2312").GetString(buf).Trim().Trim('\0').Trim();
if (str == "CZ88.NET") return null;
return str;
}
Byte GetTag()
{
return (Byte)_Stream.ReadByte();
}
IndexInfo IndexInfoAtPos(uint Index_Pos)
{
var inf = new IndexInfo();
_Stream.Position = Index_Set + 7u * Index_Pos;
inf.IpSet = GetUInt32();
inf.Offset = GetOffset();
_Stream.Position = inf.Offset;
inf.IpEnd = GetUInt32();
return inf;
}
uint GetUInt32()
{
Byte[] array = new Byte[4];
_Stream.Read(array, 0, 4);
return BitConverter.ToUInt32(array, 0);
}
#endregion
/// <summary>索引结构</summary>
class IndexInfo
{
public uint IpSet;
public uint IpEnd;
public uint Offset;
}
}
}