Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Download a Zip from a URL and OpenBd. (No .zip file is created)
See more Zip Examples
Demonstrates how to download a .zip from a URL, opens the Zip, and gets the contents of a file. No file is ever written.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.Http,
Chilkat.BinData,
Chilkat.StringBuilder,
Chilkat.ZipEntry,
Chilkat.Zip;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
http: THttp;
bd: TBinData;
zip: TZip;
entry: TZipEntry;
lineEndingBehavior: Integer;
xmlStr: string;
sb: TStringBuilder;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http := THttp.Create;
bd := TBinData.Create;
// This URL is valid and can be tested...
success := http.QuickGetBd('https://chilkatdownload.com/example_data/hamlet.zip',bd);
if (http.LastMethodSuccess = False) then
begin
WriteLn(http.LastErrorText);
Exit;
end;
zip := TZip.Create;
// Open the zip from the bytes contained in bd.
success := zip.OpenBd(bd);
if (success = False) then
begin
WriteLn(zip.LastErrorText);
Exit;
end;
// Get the entry for the file we want..
entry := TZipEntry.Create;
success := zip.EntryOf('hamlet.xml',entry);
if (success = False) then
begin
WriteLn(zip.LastErrorText);
Exit;
end;
// Convert all line endings to CRLF (if needed)
lineEndingBehavior := 2;
xmlStr := entry.UnzipToString(lineEndingBehavior,'utf-8');
if (entry.LastMethodSuccess = False) then
begin
WriteLn(entry.LastErrorText);
Exit;
end;
// The XML in this case is about 274K, so let's just examine the last 20 lines...
sb := TStringBuilder.Create;
sb.Append(xmlStr);
WriteLn(sb.LastNLines(20,True));
http.Free;
bd.Free;
zip.Free;
entry.Free;
sb.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.