forked from sirodcar/commonx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompressionHelpers.pas
More file actions
61 lines (49 loc) · 1.35 KB
/
CompressionHelpers.pas
File metadata and controls
61 lines (49 loc) · 1.35 KB
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
unit CompressionHelpers;
interface
uses
zip, typex;
type
//!!! NOTE... I don't really do anything with this. Just for interface compatibilitiy
TZCompressionLevel = (
zcNone,
zcFastest,
zcDefault,
zcMax,
zcLevel1,
zcLevel2,
zcLevel3,
zcLevel4,
zcLevel5,
zcLevel6,
zcLevel7,
zcLevel8,
zcLevel9
);
function CompressStr(s: ansistring; level: TZCompressionLevel = zcDefault): RawByteString;
function DecompressStr(bs: RawByteString): ansistring;
implementation
function CompressStr(s: ansistring; level: TZCompressionLevel = zcDefault): RawByteString;
var
sz: nativeint;
begin
setlength(result, length(s)*4);
sz := zip.ZipRam(@s[low(s)], @result[low(result)], length(s), length(result), nil);
setlength(result, sz);
end;
function DecompressStr(bs: RawByteString): ansistring;
var
sz: nativeint;
begin
setlength(result, length(bs)*3);
sz := zip.UnZipRam(@bs[low(bs)], @result[low(result)], length(bs), length(result), nil);
//if unzipram returns <0 then there wasn't enough memory... try again with 0-result memory allocated
if sz < 0 then begin
setlength(result, 0-sz);
sz := zip.UnZipRam(@bs[low(bs)], @result[low(result)], length(bs), length(result), nil);
if sz < 0 then
raise ECritical.create('failed to unzip ram');
end;
if sz > 0 then
setlength(result, sz);
end;
end.