Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Unzip an Email's Zip Attachments

See more Email Object Examples

Demonstrates the Chilkat Email.UnzipAttachments method, which unzips and replaces any Zip file attachments with their expanded contents. For example, if an email contains a single Zip holding three files, that Zip attachment is replaced by the three files as individual attachments. This example loads an email and expands its Zip attachments.

Background: Senders often bundle several files into one .zip to keep a message tidy or to compress large content. UnzipAttachments reverses that automatically, so downstream processing can work with the individual files directly rather than having to detect, extract, and re-inject archive contents. It is the inverse of ZipAttachments.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
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.Email;

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

procedure RunDemo;
var
  success: Boolean;
  email: TEmail;

begin
  success := False;

  //  Demonstrates the UnzipAttachments method, which unzips and replaces any Zip file
  //  attachments with their expanded contents.  For example, a single Zip containing 3 files
  //  is replaced by those 3 files as individual attachments.

  email := TEmail.Create;

  success := email.LoadEml('qa_data/eml/with_zip_attachment.eml');
  if (success = False) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

  WriteLn('NumAttachments before = ' + email.NumAttachments);

  //  Expand any Zip attachments in place.
  success := email.UnzipAttachments();
  if (success = False) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

  WriteLn('NumAttachments after = ' + email.NumAttachments);

  //  Note: The path "qa_data/..." is a relative local filesystem path,
  //  relative to the current working directory of the running application.


  email.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.