Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1066
  • Last Modified:

ZLIB TCompressionstream

Does anyone know how to properly use the ZLIB compression streams that come with Delphi 4?

I think it's called TCompressionStream;

Sorry for the low point value, this question isn't a high priority problem and I have a temporary solution for now.
0
arcrotty
Asked:
arcrotty
  • 2
1 Solution
 
intheCommented:
you not have the d3 sample?

i post here i case not:

program Test;
 
{$APPTYPE CONSOLE}
 
uses Classes, SysUtils, ZLib;
 
const
  BufferSize = 4096;
 
type
  TTest = class
    InFile: TStream;
    OutFile: TStream;
    ZStream: TCustomZLibStream;
    Buffer: array[0..BufferSize-1] of Byte;
    procedure Expand;
    procedure Shrink;
    procedure Status(Sender: TObject);
  end;
 
procedure TTest.Shrink;
begin
  InFile := TFileStream.Create('test.dpr', fmOpenRead);
  try
 
    OutFile := TFileStream.Create('test.dat', fmCreate);
    try
 
      ZStream := TCompressionStream.Create(clFastest, OutFile);
      try
        TCompressionStream(ZStream).OnProgress := Status;
        ZStream.CopyFrom(InFile, 0);
      finally
        ZStream.Free;
      end;
 
    finally
      OutFile.Free;
    end;
  finally
    InFile.Free;
  end;
end;
 
 
procedure TTest.Expand;
var
  Count: Integer;
begin
  InFile := TFileStream.Create('test.dat', fmOpenRead);
  try
 
    OutFile := TFileStream.Create('test.new', fmCreate);
    try
 
      ZStream := TDecompressionStream.Create(InFile);
      try
        TDecompressionStream(ZStream).OnProgress := Status;
        while True do
          begin
            Count := ZStream.Read(Buffer, BufferSize);
            if Count <> 0 then OutFile.WriteBuffer(Buffer, Count) else Break;
          end;
      finally
        ZStream.Free;
      end;
 
    finally
      OutFile.Free;
    end;
  finally
    InFile.Free;
  end;
end;
 
procedure TTest.Status(Sender: TObject);
begin
  Writeln(TCustomZLibStream(Sender).Position);
end;
 
var
  T: TTest;
begin
  T := TTest.Create;
  try
    T.Shrink;
    T.Expand;
  finally
    T.Free;
  end;
  Readln;
end.
0
 
intheCommented:
or
an example of saving C:\the.bmp as C:\new.dat then loading C:\new.dat into a TImage
  (note it dont use tcompressionstream for this just thought you may be interested):


implementation
 
uses
  ZLib;
 
....
 
procedure TForm1.Button1Click(Sender:
TObject);
var
  Strm: TMemoryStream;
  PStrm: Pointer;
  i: Integer;
begin
  Strm := TMemoryStream.Create;
  Strm.LoadFromFile('c:\the.bmp');
  Screen.Cursor := crHourGlass;
  CompressBuf(Strm.Memory, Strm.Size,
PStrm, i);
  Screen.Cursor := crDefault;
  Strm.Clear;
  Strm.Write(PStrm^, i);
  Strm.SaveToFile('c:\new.dat');
  Strm.Clear;
  Strm.Destroy;
end;
 
procedure TForm1.Button2Click(Sender:
TObject);
var
  Strm: TMemoryStream;
  PStrm: Pointer;
  tmp, i: Integer;
begin
  Strm := TMemoryStream.Create;
  Strm.LoadFromFile('c:\new.dat');
  Screen.Cursor := crHourGlass;
  DecompressBuf(Strm.Memory, Strm.Size,
tmp, PStrm, i);
  Screen.Cursor := crDefault;
  Strm.Clear;
  Strm.Write(PStrm^, i);
  Strm.Position := 0;
 
Image1.Picture.BitMap.LoadFromStream(Strm);
end;


0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now