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

Change a Filename before Unzipping

See more Zip Examples

How to open a zip and modify the filename of one or more files within the zip before unzipping.

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

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

procedure RunDemo;
var
  success: Boolean;
  zip: TZip;
  entry: TZipEntry;
  numFilesUnzipped: Integer;

begin
  success := False;

  //  This example requires the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  zip := TZip.Create;

  success := zip.OpenZip('test.zip');
  if (success = False) then
    begin
      WriteLn(zip.LastErrorText);
      Exit;
    end;

  entry := TZipEntry.Create;

  success := zip.EntryOf('hamlet.xml',entry);
  if (success = False) then
    begin
      WriteLn(zip.LastErrorText);
      Exit;
    end;
  entry.FileName := 'hamlet2.xml';

  success := zip.EntryOf('helloWorld.pl',entry);
  if (success = False) then
    begin
      WriteLn(zip.LastErrorText);
      Exit;
    end;
  entry.FileName := 'hw.pl';

  //  Now unzip to the "test" subdirectory, under our current
  //  working directory:
  numFilesUnzipped := zip.Unzip('test');
  if (numFilesUnzipped < 0) then
    begin
      WriteLn(zip.LastErrorText);
      Exit;
    end;

  //  The filenames within the .zip are unchanged, but it unzipped
  //  test/hw.pl and test/hamlet2.xml


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