Sample code for 30+ languages & platforms
Lazarus Pascal

Unzip a ZIP Entry Directly into a BinData Object Using ZipEntry.UnzipToBd

See more Zip Examples

This example demonstrates how to use the ZipEntry.UnzipToBd method to inflate a ZIP entry directly into a BinData object.

The entry contents are uncompressed entirely in memory without creating a file on disk.

This is useful when:

  • Processing ZIP entry data entirely in memory
  • Avoiding temporary filesystem files
  • Working with binary files such as images, PDFs, or certificates
  • Passing uncompressed ZIP data directly to other APIs

The example opens a ZIP archive, locates a PDF entry, inflates it into a BinData object, and then writes the uncompressed bytes to a new file.

Suppose the ZIP archive contains:

docs/report.pdf

The entry is uncompressed directly into memory before optionally being saved to:

qa_output/report.pdf

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.BinData,
  Chilkat.Zip;

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

procedure RunDemo;
var
  success: Boolean;
  zip: TZip;
  entry: TZipEntry;
  pdfData: TBinData;

begin
  success := False;

  zip := TZip.Create;

  //  Open an existing ZIP archive.
  success := zip.OpenZip('qa_data/zips/documents.zip');
  if (success = False) then
    begin
      WriteLn(zip.LastErrorText);
      Exit;
    end;

  //  Locate the PDF entry within the ZIP archive.
  entry := TZipEntry.Create;

  success := zip.EntryOf('docs/report.pdf',entry);
  if (success = False) then
    begin
      WriteLn('ZIP entry not found.');
      zip.CloseZip();
      Exit;
    end;

  //  ------------------------------------------------------------
  //  Inflate the ZIP entry directly into a BinData object.
  //  
  //  The uncompressed bytes are stored entirely in memory.
  //  
  pdfData := TBinData.Create;

  success := entry.UnzipToBd(pdfData);
  if (success = False) then
    begin
      WriteLn(entry.LastErrorText);
      Exit;
    end;

  WriteLn('Uncompressed size = ' + pdfData.NumBytes);
  WriteLn('');

  //  ------------------------------------------------------------
  //  Optionally save the uncompressed bytes to a file.
  //  
  success := pdfData.WriteFile('qa_output/report.pdf');
  if (success = False) then
    begin
      WriteLn(pdfData.LastErrorText);
      Exit;
    end;

  zip.CloseZip();

  WriteLn('PDF extracted successfully.');


  zip.Free;
  entry.Free;
  pdfData.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.