Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Unzip Files to Byte Array
See more Zip Examples
Demonstrates how to unzip each file contained in a .zip to an in-memory byte array.Chilkat Pascal (Lazarus/Delphi) 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;
numEntries: Integer;
entry: TZipEntry;
i: Integer;
fileData: TBytes;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
zip := TZip.Create;
success := zip.OpenZip('qa_data/zips/test.zip');
if (success = False) then
begin
WriteLn(zip.LastErrorText);
Exit;
end;
// Iterate of each entry in the zip.
// An entry can be a file or directory entry. For each file, unzip to a byte array.
numEntries := zip.NumEntries;
WriteLn('NumEntries = ' + numEntries);
entry := TZipEntry.Create;
i := 0;
while i < numEntries do
begin
zip.EntryAt(i,entry);
if (entry.IsDirectory = False) then
begin
fileData := entry.Inflate();
// Do whatever you wish with the file data...
end;
i := i + 1;
end;
zip.CloseZip();
WriteLn('Finished.');
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.