Solved

ZLIB TCompressionstream

Posted on 2000-03-08
2
1,026 Views
Last Modified: 2012-05-04
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
Comment
Question by:arcrotty
  • 2
2 Comments
 
LVL 17

Expert Comment

by:inthe
ID: 2598421
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
 
LVL 17

Accepted Solution

by:
inthe earned 50 total points
ID: 2598458
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now