Home » Tipps & Tricks » Dateien/Verzeichnisse » Dateioperationen » Dateien komprimieren
Dateien komprimieren
Die Unit ZLib, die Delphi beiliegt, ermöglicht die ZIP-Komprimierung von Dateien.
uses ZLib, SysUtils;
Packen:
procedure Compress(InputFileName, OutputFileName: string); var InputStream, OutputStream: TFileStream; CompressionStream: ZLib.TCompressionStream; begin InputStream:=TFileStream.Create(InputFileName, fmOpenRead); try OutputStream:=TFileStream.Create(OutputFileName, fmCreate); try CompressionStream:=TCompressionStream.Create(clMax, OutputStream); try CompressionStream.CopyFrom(InputStream, InputStream.Size); finally CompressionStream.Free; end; finally OutputStream.Free; end; finally InputStream.Free; end; end;
Entpacken:
procedure Decompress(InputFileName, OutputFileName: string); var InputStream, OutputStream: TFileStream; DeCompressionStream: ZLib.TDeCompressionStream; Buf: array[0..4095] of Byte; Count: Integer; begin InputStream:=TFileStream.Create(InputFileName, fmOpenRead); try OutputStream:=TFileStream.Create(OutputFileName, fmCreate); try DecompressionStream := TDecompressionStream.Create(InputStream); try while true do begin Count := DecompressionStream.Read(Buf[0], SizeOf(Buf)); if Count = 0 then break else OutputStream.Write(Buf[0], Count); end; finally DecompressionStream.Free; end; finally OutputStream.Free; end; finally InputStream.Free; end; end;