通用压缩单元

通用压缩单元

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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/// <author>cxg 2020-7-7</author>
/// 支持跨os,支持d7及以上版本
unit zip;
 
interface
 
{$if CompilerVersion>= 21}  //d2010
  {$define NEWZLib}
{$IFEND}
 
uses
  Classes, Zlib, SysUtils;
 
type
{$if CompilerVersion< 18.5}  //d2007
  TBytes = array of Byte;
{$IFEND}
 
  TZip = class(TObject)
  public
    /// <summary>
    ///   解压
    /// </summary>
    class procedure UnZipStream(const InStream, OutStream: TStream);
    class procedure UnZipBytes(const inbytes: TBytes; outbytes: TBytes);
    class procedure UnZipBuf(const inbuf: Pointer; insize: Integer; outbuf: Pointer; outsize: Integer);
    /// <summary>
    ///   压缩
    /// </summary>
    class procedure ZipStream(const InStream, OutStream: TStream);
    class procedure ZipBytes(const inbytes: TBytes; outbytes: TBytes);
    class procedure ZipBuf(const inbuf: Pointer; insize: Integer; outbuf: Pointer; outsize: Integer);
    class function verifyData(const buf; len: Cardinal): Cardinal;
    class function verifyStream(Stream: TStream; len: Cardinal): Cardinal;
  end;
 
implementation
 
class procedure TZip.UnZipBuf(const inbuf: Pointer; insize: Integer; outbuf: Pointer; outsize: Integer);
begin
  {$IFDEF POSIX}
  ZLib.ZDecompress(inbuf, insize, outbuf, outsize);
  {$ELSE}
  {$if defined(NEWZLib)}
  ZLib.ZDecompress(inbuf, insize, outbuf, outsize);
  {$ELSE}
  Zlib.DecompressBuf(inbuf, insize, 0, outbuf, outsize);
  {$ifend}
  {$ENDIF}
end;
 
class procedure TZip.UnZipBytes(const inbytes: TBytes; outbytes: TBytes);
{$if not defined(NEWZLib)}
var
  InSize, OutSize: Integer;
  OutBuf: Pointer;
{$ifend}
begin
  {$IFDEF POSIX}
  ZLib.ZDecompress(inbytes, outbytes);
  {$ELSE}
  {$if defined(NEWZLib)}
  ZLib.ZDecompress(inbytes, outbytes);
  {$ELSE}
  InSize := Length(inbytes);
  Zlib.DecompressBuf(@inbytes[0], InSize, 0, OutBuf, OutSize);
  Move(OutBuf^, outbytes[0], OutSize);
  FreeMem(OutBuf, OutSize);
  {$ifend}
  {$ENDIF}
end;
 
class procedure TZip.UnZipStream(const InStream, OutStream: TStream);
var
  l: Integer;
 
{$IFDEF POSIX}
var
  inBytes, OutBytes: TBytes;
{$ELSE}
var
  inBytes: TBytes;
  OutBuf: Pointer;
  Outsize: Integer;
{$ENDIF}
begin
  if InStream = nil then
    exit;
  l := InStream.Size;
  if l = 0 then
    Exit;
  {$IFDEF POSIX}
  SetLength(inBytes, l);
  InStream.Position := 0;
  InStream.Read(inBytes[0], InStream.Size);
  ZLib.ZDecompress(inBytes, OutBytes);
  OutStream.Size := Length(OutBytes);
  OutStream.Position := 0;
  OutStream.Write(OutBytes[0], Length(OutBytes));
  {$ELSE}
  setLength(inBytes, l);
  InStream.Position := 0;
  InStream.ReadBuffer(inBytes[0], l);
  {$if defined(NEWZLib)}
  ZLib.ZDecompress(@inBytes[0], l, OutBuf, Outsize);
  {$ELSE}
  Zlib.DecompressBuf(@inBytes[0], l, 0, OutBuf, Outsize);
  {$ifend}
  try
    OutStream.Size := Outsize;
    OutStream.Position := 0;
    OutStream.WriteBuffer(OutBuf^, Outsize);
  finally
    FreeMem(OutBuf, Outsize);
  end;
  {$ENDIF}
end;
 
class function TZip.verifyData(const buf; len: Cardinal): Cardinal;
var
  i: Cardinal;
  p: PByte;
begin
  i := 0;
  Result := 0;
  p := PByte(@buf);
  while i < len do
  begin
    Result := Result + p^;
    Inc(p);
    Inc(i);
  end;
end;
 
class function TZip.verifyStream(Stream: TStream; len: Cardinal): Cardinal;
var
  l, j: Cardinal;
  lvBytes: TBytes;
begin
  SetLength(lvBytes, 1024);
 
  if len = 0 then
  begin
    j := Stream.Size - Stream.Position;
  end
  else
  begin
    j := len;
  end;
 
  Result := 0;
 
  while j > 0 do
  begin
    if j < 1024 then
      l := j
    else
      l := 1024;
 
    Stream.ReadBuffer(lvBytes[0], l);
 
    Result := Result + verifyData(lvBytes[0], l);
    Dec(j, l);
  end;
end;
 
class procedure TZip.ZipBuf(const inbuf: Pointer; insize: Integer; outbuf: Pointer; outsize: Integer);
begin
  {$IFDEF POSIX}
  ZLib.ZCompress(inbuf, insize, outbuf, outsize);
  {$ELSE}
  {$if defined(NEWZLib)}
  ZLib.ZCompress(inbuf, insize, outbuf, outsize);
  {$ELSE}
  ZLib.CompressBuf(@inbytes[0], insize, outbuf, outsize);
  {$ifend}
  {$ENDIF}
end;
 
class procedure TZip.ZipBytes(const inbytes: TBytes; outbytes: TBytes);
{$if not defined(NEWZLib)}
var
  insize, outsize: integer;
  OutBuf: Pointer;
{$ifend}
begin
  {$IFDEF POSIX}
  ZLib.ZCompress(inbytes, outbytes);
  {$ELSE}
  {$if defined(NEWZLib)}
  ZLib.ZCompress(inbytes, outbytes);
  {$ELSE}
  insize := Length(inbytes);
  ZLib.CompressBuf(@inbytes[0], insize, OutBuf, outsize);
  Move(OutBuf^, outbytes[0], outsize);
  FreeMem(OutBuf, outsize);
  {$ifend}
  {$ENDIF}
end;
 
class procedure TZip.ZipStream(const InStream, OutStream: TStream);
{$IFDEF POSIX}
var
  inBytes, OutBytes: TBytes;
{$ELSE}
var
  inBytes: TBytes;
  OutBuf: Pointer;
  Outsize: Integer;
{$ENDIF}
var
  l: Integer;
begin
  if InStream = nil then
    exit;
  l := InStream.Size;
  if l = 0 then
    Exit;
  {$IFDEF POSIX}
  SetLength(inBytes, InStream.Size);
  InStream.Position := 0;
  InStream.Read(inBytes[0], InStream.Size);
  ZLib.ZCompress(inBytes, OutBytes);
  OutStream.Size := Length(OutBytes);
  OutStream.Position := 0;
  OutStream.Write(OutBytes[0], Length(OutBytes));
  {$ELSE}
  SetLength(inBytes, l);
  InStream.Position := 0;
  InStream.ReadBuffer(inBytes[0], l);
  {$if defined(NEWZLib)}
  ZLib.ZCompress(@inBytes[0], l, OutBuf, Outsize);
  {$ELSE}
  ZLib.CompressBuf(@inBytes[0], l, OutBuf, Outsize);
  {$ifend}
  try
    OutStream.Size := Outsize;
    OutStream.Position := 0;
    OutStream.WriteBuffer(OutBuf^, Outsize);
  finally
    FreeMem(OutBuf, Outsize);
  end;
  {$ENDIF}
end;
 
end.

  

posted @   delphi中间件  阅读(431)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
历史上的今天:
2016-07-08 探测断链
点击右上角即可分享
微信分享提示