Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Decompress Large Binary File in Blocks
See more Compression Examples
Decompresses a large binary file in blocks.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.Compression,
Chilkat.FileAccess;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
facSrc: TFileAccess;
facDest: TFileAccess;
blockSize: Integer;
numBlocks: Integer;
compress: TCompression;
decompressedBytes: TBytes;
compressedBytes: TBytes;
i: Integer;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
facSrc := TFileAccess.Create;
facDest := TFileAccess.Create;
// Open a previously compressed file for decompressing.
// See Compress Large File in Blocks
success := facSrc.OpenForRead('c:/temp/qa_output/compressedBmp.dat');
if (success = False) then
begin
WriteLn(facSrc.LastErrorText);
Exit;
end;
// If we compress in 32K chunks, find out how many blocks there will be.
blockSize := 32768;
numBlocks := facSrc.GetNumBlocks(blockSize);
// Open an output file for the decompressed data.
success := facDest.OpenForWrite('c:/temp/qa_output/decompressed.bmp');
if (success = False) then
begin
WriteLn(facDest.LastErrorText);
Exit;
end;
compress := TCompression.Create;
compress.Algorithm := 'deflate';
// Assuming numBlocks > 1
compress.FirstChunk := True;
compress.LastChunk := False;
i := 0;
while i < numBlocks do
begin
compressedBytes := facSrc.ReadBlock(i,blockSize);
decompressedBytes := compress.DecompressBytes(compressedBytes);
facDest.FileWrite(decompressedBytes);
i := i + 1;
compress.FirstChunk := False;
if (i = (numBlocks - 1)) then
begin
compress.LastChunk := True;
end;
end;
facSrc.FileClose();
facDest.FileClose();
WriteLn('Finished decompressing file.');
facSrc.Free;
facDest.Free;
compress.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.