Avatar of hidrau
hidrau
Flag 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
Delphi

Avatar of undefined
Last Comment
hidrau

8/22/2022 - Mon
mwochnick

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
hidrau

ASKER
I have heard something about zlib, do you know?
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
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
mwochnick

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
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
developmentguru

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
hidrau

ASKER
thanks
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.