ZLibCompressDecompress (Delphi)
Contents
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
- System.SysUtils ( fr | de | ja )
- System.Classes ( fr | de | ja )
- System.ZLib.TZCompressionStream.Create ( fr | de | ja )
- System.ZLib.TZDecompressionStream.Create ( fr | de | ja )
- System.Classes.TStream.CopyFrom ( fr | de | ja )
- System.Classes.TFileStream.Create ( fr | de | ja )
- System.SysUtils.ChangeFileExt ( fr | de | ja )
- System.ZLib.TZCompressionStream ( fr | de | ja )
- System.ZLib.TZDecompressionStream ( fr | de | ja )
- System.Classes.TFileStream ( fr | de | ja )