Link to home
Start Free TrialLog in
Avatar of hidrau
hidrauFlag for Brazil

asked on

How can I compact a string ?

Hello Guys

I have a string very long that If I record it in a txt file, it reaches the 56 kb.

How could I compact (zip) it?

This string I will record in my MS SQL 2005 table and I don't want to record it this way.

I don't need to make any search inside of it. I just need to keep it in my table.

thanks
Alex
Avatar of mwochnick
mwochnick
Flag of United States of America image

Here's a link to an Delphi example on how to compress/decompress a string
http://www.example-code.com/delphi/string-compression-1.asp
Avatar of hidrau

ASKER

I have heard something about zlib, do you know?
Avatar of hidrau

ASKER

I found this but I couldn't make this work with D7

http://www.yanniel.info/2011/01/string-compress-decompress-delphi-zlib.html

Take a look at it
Right as the comments to article state that code doesn't work with Delphi 7 - you would need to use ZCompressStr and ZDecompressStr
example code can be found in this article  - just search for ZCompressStr and ZDecompressStr
http://www.delphigeist.com/2010/09/custom-client-server-application-with.html
Avatar of hidrau

ASKER

mwochnick,

I got another solution, take a look at this:

function CompressString(Inp: string): string;
var
  OutBuf: Pointer;
  OutBytes: Integer;
begin
  OutBuf:=nil;
  try
    CompressBuf(Pointer(Inp),Length(Inp),OutBuf,OutBytes);
    SetString(Result,PChar(OutBuf),OutBytes);
  finally
    if OutBuf <> nil then
       FreeMem(OutBuf);
  end;
end;

{ Decompress a string }
function DecompressString(Inp: string): string;
var
    OutBuf: Pointer;
    OutBytes: Integer;
begin
    OutBuf:=nil;
    try
        DecompressBuf(Pointer(Inp),Length(Inp),0,OutBuf,OutBytes);
        SetString(Result,PChar(OutBuf),OutBytes);
    finally
        if OutBuf <> nil then
            FreeMem(OutBuf);
    end;
end;  

Open in new window


it uses Zlib
ASKER CERTIFIED SOLUTION
Avatar of developmentguru
developmentguru
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hidrau

ASKER

thanks