Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Unzip One File to a Stream
See more Zip Examples
Demonstrates how to unzip a particular file contained within a .zip archive to a Chilkat stream.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.Task,
Chilkat.StringBuilder,
Chilkat.Zip,
Chilkat.Stream;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
zip: TZip;
entry: TZipEntry;
streamA: TStream;
streamB: TStream;
unzipTask: TTask;
sb: TStringBuilder;
streamC: TStream;
s: string;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
zip := TZip.Create;
// First open a .zip.
success := zip.OpenZip('qa_data/hamlet.zip');
if (success = False) then
begin
WriteLn(zip.LastErrorText);
Exit;
end;
// Find the entry to be unzipped to a stream..
entry := TZipEntry.Create;
success := zip.EntryMatching('*hamlet.xml',entry);
if (success = False) then
begin
WriteLn(zip.LastErrorText);
Exit;
end;
// There are three ways we can unzip (inflate) to a stream.
//
// 1) Set a sink file on the stream object and call UnzipToStream synchronously.
// This unzips to the stream's sink (which is to simply unzip to a file in the filesystem).
//
// 2) Create a stream object and call UnzipToStreamAsync. This starts the unzip in a background
// thread (after Task.Run is called). Your application can then read the unzipped (inflated) file
// directly from the stream.
//
// 3) (not shown in this example) Unzip to a stream object that is the source of something else.
//
// ----------------------------------------------------------------
// Case 1: Unzip to a file by setting the sink of streamA to a file.
streamA := TStream.Create;
streamA.SinkFile := 'qa_output/hamletA.xml';
success := entry.UnzipToStream(streamA);
if (success <> True) then
begin
WriteLn(entry.LastErrorText);
Exit;
end;
// Close the stream to close the output file.
success := streamA.WriteClose();
// ----------------------------------------------------------------
// Case 2a: Uzip to a stream asynchronously.
streamB := TStream.Create;
unzipTask := entry.UnzipToStreamAsync(streamB);
if (entry.LastMethodSuccess <> True) then
begin
WriteLn(entry.LastErrorText);
Exit;
end;
// Start the background unzip thread.
unzipTask.Run();
// Read the stream. (this is reading the unzipped file data)
sb := TStringBuilder.Create;
while streamB.EndOfStream <> True do
begin
// Each call to ReadSb appends to the contents of sb.
streamB.ReadSb(sb);
end;
sb.WriteFile('qa_output/hamletB.xml','utf-8',False);
unzipTask.Free;
WriteLn('Success.');
// ----------------------------------------------------------------
// Case 2b: Uzip to a stream asynchronously and emit output while reading.
streamC := TStream.Create;
unzipTask := entry.UnzipToStreamAsync(streamC);
if (entry.LastMethodSuccess <> True) then
begin
WriteLn(entry.LastErrorText);
Exit;
end;
unzipTask.Run();
while streamC.EndOfStream <> True do
begin
s := streamC.ReadString();
WriteLn(s);
end;
unzipTask.Free;
WriteLn('Success.');
zip.Free;
entry.Free;
streamA.Free;
streamB.Free;
sb.Free;
streamC.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.