Главная » Delphi » Основная » использование ZLib

использование ZLib

23:07
uses
ZLib;
 
///**************
 
procedure CompressFile(const Source, Dest : String);
var
SourceFile, DestFile : TFileStream;
compr : TCompressionStream;
bytecount : Integer;
begin
SourceFile := TFileStream.Create(Source, fmOpenRead);
DestFile := TFileStream.Create(Dest,fmCreate);
try
bytecount := SourceFile.Size;
//store the original size in bytes
DestFile.Write(bytecount, SizeOf(bytecount));
//create the compression stream (after storing the bytecount!)
compr := TCompressionStream.Create(clMax, DestFile);
try
//compress the content of the file
compr.CopyFrom(SourceFile,bytecount);
finally
compr.Free;
end;
finally
SourceFile.Free;
DestFile.Free;
end;
end;
 
procedure DecompressFile(const Source, Dest : String);
var
SourceFile, DestFile : TFileStream;
decompr : TDecompressionStream;
bytecount : Integer;
begin
SourceFile := TFileStream.Create(Source, fmOpenRead);
DestFile := TFileStream.Create(Dest,fmCreate);
try
//how big was the "original"
SourceFile.Read(bytecount,SizeOf(bytecount));
if bytecount > 0 then
begin
//create the decompression stream after reading the bytecount
decompr := TDecompressionStream.Create(SourceFile);
try
//decompress
DestFile.CopyFrom(decompr,bytecount);
finally
decompr.Free;
end;
end;
finally
SourceFile.Free;
DestFile.Free;
end;
end;
 
 
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then CompressFile(OD.FileName,'C:\test.zip');
end;