First of all, all the zip compression is embedded in an unique Delphi unit, named SynZip which is meant to replace the default zlib unit of Delphi, and enhance it. It works from Delphi 7 to Delphi 2010, from the same source code, which is released under a GPL/LGPL/MPL tri-license.

Do you want to compress some data in memory? Use CompressString() and UnCompressString() methods. Both work with RawByteString encoded data:

function test: boolean;
var tmp: RawByteString;
begin
tmp := 'some data';
result := (UnCompressString(CompressString(tmp,False,comp))=tmp);
end;

Do you want to create a zip archive file? Use our TZipWrite class.

procedure TestCompression;
var FN: TFileName;
begin
FN := ChangeFileExt(paramstr(0),'.zip');
with TZipWrite.Create(FN) do
try
AddDeflated('one.exe',pointer(Data),length(Data));
assert(Count=1);
AddDeflated('ident.txt',M.Memory,M.Position);
assert(Count=2);
finally
Free;
end;
end;

Do you want to read a zip archive file? Use our TZipRead class.

procedure TestUnCompression;
var FN: TFileName;
i: integer;
begin
FN := ChangeFileExt(paramstr(0),'.zip');
with TZipRead.Create(FN) do
try
i := NameToIndex('ONE.exe');
assert(i>=0);
assert(UnZip(i)=Data);
i := NameToIndex('ident.TXT');
assert(i>=0);
assert(Entry[i].info^.zcrc32=crc32(0,M.Memory,M.Position));
finally
Free;
end;
DeleteFile(FN);
end;

And if you want the file to be embedded to your executable, there is a dedicated Create constructor in the TZipRead just for handling that:

  1. create your exe file with a TZipRead instance created with it;
  2. append your zip content to your exe (by using TZipWrite or by using copy /b on command line);
  3. that's all!
Couldn't it be easier?

Code above was extracted and adapted from our SynSelfTests test unit.
Comments and questions are welcome on our forum.