was ist neu ¦  programmier tips ¦  indy artikel ¦  intraweb artikel ¦  informationen ¦  links ¦  interviews
 sonstiges ¦  tutorials ¦  Add&Win Gewinnspiel

Tips (1541)

Dateien (137)
Datenbanken (90)
Drucken (35)
Grafik (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Mathematik (76)
Multimedia (45)
Oberfläche (107)
Objekte/
ActiveX (51)

OpenTools API (3)
Sonstiges (126)
Strings (83)
System (266)
VCL (242)

Tips sortiert nach
Komponente


Tip suchen

Tip hinzufügen

Add&Win Gewinnspiel

Werbung

56 Visitors Online


 
...einen Stream komprimieren, dekomprimieren?
Autor: Mike Orriss
Homepage: http://www.teamb.com
[ Tip ausdrucken ]  

Tip Bewertung (35):  
     


uses
  
ZLib;

{ Compress a stream }

procedure CompressStream(inpStream, outStream: TStream);
var
  
InpBuf, OutBuf: Pointer;
  InpBytes, OutBytes: Integer;
begin
  
InpBuf := nil;
  OutBuf := nil;
  try
    
GetMem(InpBuf, inpStream.Size);
    inpStream.Position := 0;
    InpBytes := inpStream.Read(InpBuf^, inpStream.Size);
    CompressBuf(InpBuf, InpBytes, OutBuf, OutBytes);
    outStream.Write(OutBuf^, OutBytes);
  finally
    if 
InpBuf <> nil then FreeMem(InpBuf);
    if OutBuf <> nil then FreeMem(OutBuf);
  end;
end;


{ Decompress a stream }
procedure DecompressStream(inpStream, outStream: TStream);
var
  
InpBuf, OutBuf: Pointer;
  OutBytes, sz: Integer;
begin
  
InpBuf := nil;
  OutBuf := nil;
  sz     := inpStream.Size - inpStream.Position;
  if sz > 0 then 
    try
      
GetMem(InpBuf, sz);
      inpStream.Read(InpBuf^, sz);
      DecompressBuf(InpBuf, sz, 0, OutBuf, OutBytes);
      outStream.Write(OutBuf^, OutBytes);
    finally
      if 
InpBuf <> nil then FreeMem(InpBuf);
      if OutBuf <> nil then FreeMem(OutBuf);
    end;
  outStream.Position := 0;
end;


{
  Example:
   Compress the contents of RichEdit1 and
   calculate the compression rate.
   Then save the stream to a file (ms2.dat)

  Beispiel:
   Komprimiert den Inhalt von RichEdit1 und
   berechnet die Kompressionsrate.
   Dann wird der Stream in eine Datei (ms2.dat) gespeichert.
}

procedure TForm1.Button1Click(Sender: TObject);
var
  
ms1, ms2: TMemoryStream;
begin
  
ms1 := TMemoryStream.Create;
  try
    
ms2 := TMemoryStream.Create;
    try
      
RichEdit1.Lines.SaveToStream(ms1);
      CompressStream(ms1, ms2);
      ShowMessage(Format('Stream Compression Rate: %d %%',
        [round(100 / ms1.Size * ms2.Size)]));
      ms2.SaveToFile('C:\ms2.dat');
    finally
      
ms1.Free;
    end;
  finally
    
ms2.Free;
  end;
end;

{
  Loads the stream from a file (ms2.dat)
  and decompresses it.
  Then loads the Stream to RichEdit1.

  Lädt den komprimierten Stream von einer Datei (ms2.dat)
  und dekomprimiert ihn.
  Dann wird der Stream wieder in RichEdit1 geladen.
}

procedure TForm1.Button2Click(Sender: TObject);
var
  
ms1, ms2: TMemoryStream;
begin
  
ms1 := TMemoryStream.Create;
  try
    
ms2 := TMemoryStream.Create;
    try
      
ms1.LoadFromFile('C:\ms2.dat');
      DecompressStream(ms1, ms2);
      RichEdit1.Lines.LoadFromStream(ms2);
    finally
      
ms1.Free;
    end;
  finally
    
ms2.Free;
  end;
end;


 

Bewerten Sie diesen Tipp:

dürftig
ausgezeichnet


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners