Files Delphi

Title: Compress files
Question: How to compress files
Answer:
unit unComp;
interface
uses Zlib, Classes, SysUtils;
function CompressFile(vFileName, vDest: String): Boolean;
function DecompressFile(vComFile, vDest: String): Boolean;
implementation
function CompressFile(vFileName, vDest: String): Boolean;
var
CompFileStream: TStream;
CompressionStream: TCompressionStream;
FileStream: TStream;
begin
try
FileStream := TFileStream.Create(vFileName, fmOpenRead or fmShareDenyWrite);
CompFileStream := TFileStream.Create(vDest,fmCreate);
CompressionStream := TCompressionStream.Create(clMax, CompFileStream);
CompressionStream.CopyFrom(FileStream, 0);
CompressionStream.Free;
CompFileStream.Free;
FileStream.Free;
Result := True;
except
Result := False;
end;
end;
function DecompressFile(vComFile, vDest: String): Boolean;
var
CompFileStream: TStream;
FileStream: TStream;
DecompressionStream: TDecompressionStream;
Buffer: Array[0..4095] of Char;
BufLen: Integer;
begin
try
CompFileStream := TFileStream.Create(vComFile, fmOpenRead or fmShareDenyWrite);
FileStream := TFileStream.Create(vDest, fmCreate);
DecompressionStream := TDecompressionStream.Create(CompFileStream);
BufLen := DecompressionStream.Read(Buffer,SizeOf(Buffer));
while BufLen 0 do
begin
FileStream.Write(Buffer, BufLen);
BufLen := DecompressionStream.Read(Buffer,SizeOf(Buffer));
end;
DecompressionStream.Free;
FileStream.Free;
CompFileStream.Free;
Result := True;
except
Result := False;
end;
end;
end.