Lazarus Pascal Requires Chilkat v11.0.0+
Lazarus Pascal
Extract a Single ZIP Entry While Preserving Its Stored Path
See more Zip Examples
This example demonstrates how to use the ZipEntry.Extract method to extract a single entry from a ZIP archive.
The example first creates a ZIP archive containing a text file. It then re-opens the ZIP, finds the entry, and extracts it to a base directory.
The Extract method uses the relative path stored within the ZIP entry. Any required subdirectories are created automatically beneath the specified base directory.
Chilkat Lazarus Pascal Downloads
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;
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.
success := zip.AddString('myApp/docs/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;
// Find the entry by its stored ZIP path.
entry := TZipEntry.Create;
success := zip2.EntryOf('myApp/docs/hello.txt',entry);
if (success = False) then
begin
WriteLn('Entry not found.');
Exit;
end;
// Extract this single entry to the base directory:
//
// c:/temp/extracted
//
// Because the entry's stored path is:
//
// myApp/docs/hello.txt
//
// the file will be extracted to:
//
// qa_output/extracted/myApp/docs/hello.txt
//
// The required subdirectories are created automatically.
success := entry.Extract('c:/temp/extracted');
if (success = False) then
begin
WriteLn(entry.LastErrorText);
Exit;
end;
zip2.CloseZip();
WriteLn('Entry extracted successfully.');
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.