반응형
uses ZLib;
(*
* CompressString() usage:
*
* CompressedString := CompressString(DataString);
*)
function CompressString(const S: string) : string;
var
InputStream: TCompressionStream;
OutputStream: TMemoryStream;
Size: Integer;
begin
OutputStream := TMemoryStream.Create;
try
InputStream := TCompressionStream.Create(clMax, OutputStream);
try
Size := Length(S);
InputStream.Write(Size, SizeOf(Size));
InputStream.Write(S[1], Size)
finally
InputStream.Free
end;
SetLength(Result, OutputStream.Size);
OutputStream.Seek(0, soFromBeginning);
OutputStream.Read(Result[1], Length(Result))
finally
OutputStream.Free
end
end;
(*
* DecompressString() usage:
*
* DataString := DecompressString(CompressedString);
*)
function DecompressString(const S: string) : string;
var
InputStream: TMemoryStream;
OutputStream: TDecompressionStream;
Size: Integer;
begin
InputStream := TMemoryStream.Create;
try
InputStream.Write(S[1], Length(S));
InputStream.Seek(0, soFromBeginning);
OutputStream := TDecompressionStream.Create(InputStream);
try
OutputStream.Read(Size, SizeOf(Size));
SetLength(Result, Size);
OutputStream.Read(Result[1], Size)
finally
OutputStream.Free
end
finally
InputStream.Free
end
end;
(*
* CompressString() usage:
*
* CompressedString := CompressString(DataString);
*)
function CompressString(const S: string) : string;
var
InputStream: TCompressionStream;
OutputStream: TMemoryStream;
Size: Integer;
begin
OutputStream := TMemoryStream.Create;
try
InputStream := TCompressionStream.Create(clMax, OutputStream);
try
Size := Length(S);
InputStream.Write(Size, SizeOf(Size));
InputStream.Write(S[1], Size)
finally
InputStream.Free
end;
SetLength(Result, OutputStream.Size);
OutputStream.Seek(0, soFromBeginning);
OutputStream.Read(Result[1], Length(Result))
finally
OutputStream.Free
end
end;
(*
* DecompressString() usage:
*
* DataString := DecompressString(CompressedString);
*)
function DecompressString(const S: string) : string;
var
InputStream: TMemoryStream;
OutputStream: TDecompressionStream;
Size: Integer;
begin
InputStream := TMemoryStream.Create;
try
InputStream.Write(S[1], Length(S));
InputStream.Seek(0, soFromBeginning);
OutputStream := TDecompressionStream.Create(InputStream);
try
OutputStream.Read(Size, SizeOf(Size));
SetLength(Result, Size);
OutputStream.Read(Result[1], Size)
finally
OutputStream.Free
end
finally
InputStream.Free
end
end;
반응형