Uses ZLib;
Function CompressAnsiString(
const Input: AnsiString): RawByteString;
Var Buffer: Pointer;
BufSize: Integer;
Begin
Buffer :=
nil;
Try
CompressBuf(Input[1], Length(Input), Buffer, BufSize);
SetLength(Result, BufSize);
Move(Buffer^, Result[1], BufSize);
Finally
If Buffer <>
nil Then FreeMem(Buffer);
End;
End;
Function DeCompressAnsiString(
const Input: RawByteString): AnsiString;
Var Buffer: Pointer;
BufSize: Integer;
Begin
Buffer :=
nil;
Try
DeCompressBuf(Input[1], Length(Input), 0, Buffer, BufSize);
SetLength(Result, BufSize);
Move(Buffer^, Result[1], BufSize);
Finally
If Buffer <>
nil Then FreeMem(Buffer);
End;
End;
Function CompressWideString(
const Input: UnicodeString): RawByteString;
overload;
Var Buffer: Pointer;
BufSize: Integer;
Begin
Buffer :=
nil;
Try
CompressBuf(Input[1], Length(Input) * 2, Buffer, BufSize);
SetLength(Result, BufSize);
Move(Buffer^, Result[1], BufSize);
Finally
If Buffer <>
nil Then FreeMem(Buffer);
End;
End;
Function DeCompressWideString(
const Input: RawByteString): UnicodeString;
Var Buffer: Pointer;
BufSize: Integer;
Begin
Buffer :=
nil;
Try
DeCompressBuf(Input[1], Length(Input), 0, Buffer, BufSize);
SetLength(Result, BufSize
div 2);
Move(Buffer^, Result[1], BufSize
div 2);
Finally
If Buffer <>
nil Then FreeMem(Buffer);
End;
End;
Function CompressString(
const Input:
String): RawByteString;
Var Buffer: Pointer;
BufSize: Integer;
Begin
Buffer :=
nil;
Try
CompressBuf(Input[1], Length(Input) * SizeOf(Char), Buffer, BufSize);
SetLength(Result, BufSize);
Move(Buffer^, Result[1], BufSize);
Finally
If Buffer <>
nil Then FreeMem(Buffer);
End;
End;
Function DeCompressString(
const Input: RawByteString):
String;
Var Buffer: Pointer;
BufSize: Integer;
Begin
Buffer :=
nil;
Try
DeCompressBuf(Input[1], Length(Input), 0, Buffer, BufSize);
SetLength(Result, BufSize
div SizeOf(Char));
Move(Buffer^, Result[1], BufSize
div SizeOf(Char));
Finally
If Buffer <>
nil Then FreeMem(Buffer);
End;
End;