Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.0.0+

Get Compressed ZIP Entry Data as Base64 Using ZipEntry.CopyToBase64

See more Zip Examples

This example creates a small ZIP archive containing a text file added from memory, writes the ZIP to disk, re-opens it, and then uses ZipEntry.CopyToBase64 to retrieve the compressed data for the text file as a Base64-encoded string.

The Base64 string returned by CopyToBase64 represents the compressed bytes stored for the entry inside the ZIP archive. It is not the original uncompressed text data.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.ZipEntry,
  Chilkat.Zip;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  zip: TZip;
  zip2: TZip;
  entry: TZipEntry;
  b64: string;

begin
  success := False;

  //  ------------------------------------------------------------
  //  First create a sample ZIP archive containing a small text file.

  zip := TZip.Create;

  success := zip.NewZip('c:/temp/sampleText.zip');
  if (success = False) then
    begin
      WriteLn(zip.LastErrorText);
      Exit;
    end;

  //  Add a small text file from memory.
  //  The text is stored in the ZIP as "hello.txt".
  success := zip.AddString('hello.txt','Hello from a ZIP entry!','utf-8');
  if (success = False) then
    begin
      WriteLn(zip.LastErrorText);
      Exit;
    end;

  //  Write the ZIP archive to disk and close it.
  success := zip.WriteZipAndClose();
  if (success = False) then
    begin
      WriteLn(zip.LastErrorText);
      Exit;
    end;

  //  ------------------------------------------------------------
  //  Now open the ZIP archive we just created.

  zip2 := TZip.Create;

  success := zip2.OpenZip('c:/temp/sampleText.zip');
  if (success = False) then
    begin
      WriteLn(zip2.LastErrorText);
      Exit;
    end;

  //  Get the text file entry.
  entry := TZipEntry.Create;

  success := zip2.EntryOf('hello.txt',entry);

  if (success = False) then
    begin
      WriteLn('Entry not found.');
      Exit;
    end;

  //  Get the compressed ZIP entry data as Base64.
  //  
  //  This is the compressed data stored inside the ZIP file,
  //  not the uncompressed text "Hello from a ZIP entry!".
  b64 := entry.CopyToBase64();

  WriteLn('Base64 compressed ZIP entry data:');
  WriteLn(b64);

  zip2.CloseZip();

  WriteLn('Done.');


  zip.Free;
  zip2.Free;
  entry.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.