String compression/decompression routines using Delphi

I wrote the following two functions (in bold) with the purpose of compressing/decompressing string values within a Delphi application:

.........................
implementation

uses
  ZLib;

function ZCompressString(aText: string; aCompressionLevel: TZCompressionLevel): string;
var
  strInput,
  strOutput: TStringStream;
  Zipper: TZCompressionStream;
begin
  Result:= '';
  strInput:= TStringStream.Create(aText);
  strOutput:= TStringStream.Create;
  try
    Zipper:= TZCompressionStream.Create(strOutput, aCompressionLevel);
    try
      Zipper.CopyFrom(strInput, strInput.Size);
    finally
      Zipper.Free;
    end;
    Result:= strOutput.DataString;
  finally
    strInput.Free;
    strOutput.Free;
  end;
end;

function ZDecompressString(aText: string): string;
var
  strInput,
  strOutput: TStringStream;
  Unzipper: TZDecompressionStream;
begin
  Result:= '';
  strInput:= TStringStream.Create(aText);
  strOutput:= TStringStream.Create;
  try
    Unzipper:= TZDecompressionStream.Create(strInput);
    try
      strOutput.CopyFrom(Unzipper, Unzipper.Size);
    finally
      Unzipper.Free;
    end;
    Result:= strOutput.DataString;
  finally
    strInput.Free;
    strOutput.Free;
  end;
end;

.........................

The main advantage of the above functions over the ZCompressStr and ZDecompressStr routines shipped with ZLib.pas, is that you won’t have potential data lost when handling Unicode <->Ansi conversions. In other words, the above functions will work in both Ansi Delphi versions (previous to Delphi 2009) and Unicode Delphi versions (Delphi 2009, 2010, XE and so on).

Note that you need to include ZLib in the uses clause. ZLib.pas is a high level wrapper to the ZLib library created by Jean-Loup Gailly and Mark Adle for data compression.

In addition, using the TZCompressionStream and TZDecompressionStream classes you can also create (compress) and decompress ZIP files.

What did (do) I need this for? Well, the applicability for data compression is wide...

A real life example? I needed to store JSON strings in a MySQL database. As a way to optimize resources I compressed all JSON strings before the insertion into the database. After the retrieval, I was able to decompress each string to its original value. The compression rate was huge: I packed ~9000 chars in ~300 per JSON on average. This is a considerable saving: my table contains more than one million rows. Do the math yourself! :-)

Delphi Developers in Toronto

There are very little opportunities for Delphi developers in Toronto (Canada). The job market for software developers in this area is monopolized by .NET (C#, VB), Java and C++ in a huge percentage. Objective C, Python, PHP, Ruby are more popular than Delphi around here.

I don’t blame the Torontonian companies for deprecating Delphi. It‘s a reality that after the release of Delphi 7, Borland drove “the once most useful and popular IDE of the world” into a dark era.

Nonetheless, there is hope for Delphi. Lately, it has improved quite a bit, catching up to some degree for the time lost in the Borland's apocalypse. Some important milestones archived by Delphi lately:
  • Unicode support.
  • 64 Bits support.
  •  Mufti-platform support (with the introduction of FireMonkey)
Finally, I would like to list a few companies using Delphi these days in Toronto (Greater Toronto Area) :