ZLibCompressDecompress (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example shows the usage of TZCompressionStream and TZDecompressionStream. Supply the two filenames in the Edit controls of the main form and then press either the Compress or the Decompress button to do the actions. When the Compress button is pressed, the '.zip' extension is automatically added to the output file. When the Decompress button is pressed, '.zip' is automatically removed.

Code

procedure TForm1.btnCompressClick(Sender: TObject);
var
  LInput, LOutput: TFileStream;
  LZip: TZCompressionStream;

begin
  { Create the Input, Output, and Compressed streams. }
  LInput := TFileStream.Create(Edit1.Text, fmOpenRead);
  LOutput := TFileStream.Create(Edit2.Text + '.zip', fmCreate);
  LZip := TZCompressionStream.Create(clDefault, LOutput);

  { Compress data. }
  LZip.CopyFrom(LInput, LInput.Size);

  { Free the streams. }
  LZip.Free;
  LInput.Free;
  LOutput.Free;
end;

procedure TForm1.btnDecompressClick(Sender: TObject);
var
  LInput, LOutput: TFileStream;
  LUnZip: TZDecompressionStream;

begin
  { Create the Input, Output, and Decompressed streams. }
  LInput := TFileStream.Create(Edit1.Text, fmOpenRead);
  LOutput := TFileStream.Create(ChangeFileExt(Edit1.Text, 'txt'), fmCreate);
  LUnZip := TZDecompressionStream.Create(LInput);

  { Decompress data. }
  LOutput.CopyFrom(LUnZip, 0);

  { Free the streams. }
  LUnZip.Free;
  LInput.Free;
  LOutput.Free;
end;

Uses

See Also