Difference between revisions of "LMD VCL - ZLib support"

From LMD
Jump to: navigation, search
(Units)
Line 39: Line 39:
 
     LDataStream := TLMDCompressionStream.Create(clDefault, LFileStream);  
 
     LDataStream := TLMDCompressionStream.Create(clDefault, LFileStream);  
 
     try
 
     try
       LDataStream(FSomeData, FSizeOfSomeData);
+
       LDataStream.Write(FSomeData, FSizeOfSomeData);
 
     finally
 
     finally
 
       LDestStream.Free;
 
       LDestStream.Free;
Line 49: Line 49:
 
...
 
...
 
</delphi>
 
</delphi>
 +
 +
TLMDDecompressionStream can be used as decompression layer for stream with compressed data. Example:
 +
 +
<delphi>
 +
uses
 +
  ..., LMDZLib, ...;
 +
 +
// old code
 +
...
 +
var
 +
  LDataStream: TFileStream;
 +
begin
 +
  LDataStream := TFileStream.Create(FileName, fmOpenRead);
 +
  try
 +
    LDataStream.Read(FSomeData, FSizeOfSomeData);
 +
  finally
 +
    LDataStream.Free;
 +
  end;
 +
end;
 +
...
 +
 +
// code with TLMDDecompressionStream
 +
...
 +
var
 +
  LDataStream: TLMDDecompressionStream;
 +
  LFileStream: TFileStream;
 +
begin
 +
  LFileStream := TFileStream.Create(FileName, fmOpenRead);
 +
  try
 +
    LDataStream := TLMDDecompressionStream.Create(LFileStream);
 +
    try
 +
      LDataStream.Read(FSomeData, FSizeOfSomeData);
 +
    finally
 +
      LDestStream.Free;
 +
    end;
 +
  finally
 +
    LFileStream.Free;
 +
  end;
 +
end;
 +
...
 +
</delphi>
 +
 +
As you can see changes in code are minimal.

Revision as of 14:10, 17 July 2007

Introduction

LMD 2k7 has built in ZLib library to compress/decompress any data. This is native pascal solution it doesn't require any external obj or dll files.

Units

  • LMDZCommonUnit.pas - Common code and types for compress/decompress routines (raw ZLib api)
  • LMDZCompressUnit.pas - Unit that contain compression code (raw ZLib api)
  • LMDZUncompressUnit.pas - Unit with uncompression code (raw ZLib api)
  • LMDZLib.pas - TStream wrappers that provide interface to compress/decompress data directly into any TStream descendants.

LMDZLib interface

TLMDCompressionStream wrapper class that compress data directly to any TStream descendant, and has transparent interface that compatible with TStream, so you can easily add compression for exists application. Example:

<delphi> uses

 ..., LMDZLib, ...;

// old code ... var

 LDataStream: TFileStream;

begin

 LDataStream := TFileStream.Create(FileName, fmCreate);
 try
   LDataStream(FSomeData, FSizeOfSomeData);
 finally
   LDataStream.Free;
 end;

end; ...

// code with TLMDCompressionStream ... var

 LDataStream: TLMDCompressionStream;
 LFileStream: TFileStream;

begin

 LFileStream := TFileStream.Create(FileName, fmCreate);
 try 
   LDataStream := TLMDCompressionStream.Create(clDefault, LFileStream); 
   try
     LDataStream.Write(FSomeData, FSizeOfSomeData);
   finally
     LDestStream.Free;
   end;
 finally
   LFileStream.Free;
 end;

end; ... </delphi>

TLMDDecompressionStream can be used as decompression layer for stream with compressed data. Example:

<delphi> uses

 ..., LMDZLib, ...;

// old code ... var

 LDataStream: TFileStream;

begin

 LDataStream := TFileStream.Create(FileName, fmOpenRead);
 try
   LDataStream.Read(FSomeData, FSizeOfSomeData);
 finally
   LDataStream.Free;
 end;

end; ...

// code with TLMDDecompressionStream ... var

 LDataStream: TLMDDecompressionStream;
 LFileStream: TFileStream;

begin

 LFileStream := TFileStream.Create(FileName, fmOpenRead);
 try 
   LDataStream := TLMDDecompressionStream.Create(LFileStream); 
   try
     LDataStream.Read(FSomeData, FSizeOfSomeData);
   finally
     LDestStream.Free;
   end;
 finally
   LFileStream.Free;
 end;

end; ... </delphi>

As you can see changes in code are minimal.