-
Notifications
You must be signed in to change notification settings - Fork 2
/
D64WB.pas
112 lines (101 loc) · 2.96 KB
/
D64WB.pas
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
unit D64WB;
// 28 Febr 2019 Roberto Della Pasqua www.dellapasqua.com
// for better performances put RDPMM64 as first unit clause in project source
// define SEAZLIB for compression realtime acceleration
// 26 Febr 2020 buffer len check
{$DEFINE D64ZLIB}
interface
uses Windows, Sysutils, System.Classes, Web.HTTPApp, Web.WebReq;
type
TWBHelper = class helper for TWebResponse
public
procedure ZlibDeflate; overload;
procedure ZlibDeflate(const Src:TMemoryStream); overload;
end;
implementation
{$IFDEF D64ZLIB}
uses D64ZLIB;
{$ELSE}
uses System.Zlib;
{$ENDIF}
procedure TWBHelper.ZlibDeflate;
var
ZBuff: TMemoryStream;
Src, Dst: TBytes;
begin
if ContentStream = nil then // allora accedere a content string
begin
if (Length(Content) > 1500) and (Length(Content) < 10000000) then
begin
Src := TEncoding.UTF8.GetBytes(Content);
SeaZlib.Compress(Src, Dst);
ZBuff := TMemoryStream.Create;
ZBuff.Write(Dst, Length(Dst));
ContentStream := ZBuff;
ContentStream.Seek(0, 0);
ContentEncoding := 'deflate';
ContentLength := ZBuff.Size;
(* Src := TEncoding.UTF8.GetBytes(Content);
ZBuffStr:=TMemoryStream.Create;
ZBuffStr.Write(Src, Length(Src));
ZBuffStr.Seek(0,0);
ZBuff := TMemoryStream.Create;
SeaZlib.CompressStream(ZBuffStr, ZBuff, Z_BEST_SPEED_AC);
ZBuffStr.Free;
ContentStream := ZBuff;
ContentStream.Seek(0, 0);
ContentEncoding := 'deflate';
ContentLength := ZBuff.Size; *)
end;
end
else if (ContentStream.Size > 1500) and (ContentStream.Size < 10000000) then
begin
ZBuff := TMemoryStream.Create;
ContentStream.Seek(0, 0);
{$IFDEF D64ZLIB}
SeaZlib.CompressStream(ContentStream, ZBuff, Z_BEST_SPEED_AC);
{$ELSE}
ZCompressStream(ContentStream, ZBuff, zcFastest);
{$ENDIF}
ContentStream.Free;
ContentStream := ZBuff;
ContentStream.Seek(0, 0);
ContentEncoding := 'deflate';
ContentLength := ZBuff.Size;
end;
end;
procedure TWBHelper.ZlibDeflate(const Src:TMemoryStream);
{$IFDEF D64ZLIB}
var
Helper:TMemoryStream;
begin
if (Src.Size > 1500) and (Src.Size < 10000000) then
begin
Src.Seek(0, 0);
Helper:=TMemoryStream.Create;
SeaZlib.CompressStream(Src, Helper, Z_BEST_SPEED_AC);
ContentStream := Helper;
ContentStream.Seek(0, 0);
ContentEncoding := 'deflate';
ContentLength := Helper.Size;
end;
{$ELSE}
var
HelperTZ:TZCompressionStream;
Helper:TMemoryStream;
begin
if (Src.Size > 1500) and (Src.Size < 10000000) then
begin
Src.Seek(0, 0);
Helper:=TMemoryStream.Create;
HelperTZ:=TZCompressionStream.Create(Helper, zcFastest, -15);
HelperTZ.CopyFrom(Src, Src.Size);
HelperTZ.Free;
ContentStream := Helper;
ContentStream.Seek(0, 0);
ContentEncoding := 'deflate';
ContentLength := Helper.Size;
end;
{$ENDIF}
end;
end.