Accessing compressed data
Easy Compression Library streams (TECLFileStream, TECLMemoryStream, TECLStream) compress and decompress data on the fly, so when you get TECLFileStream.Size, you see a size of decompressed file, because the file is compressed/decompressed transparently.
This technology significantly simplifies dealing with the points specific for compression, and after specifying CompressionLevel you may work with ECL streams as with usual streams and don't care of compression.
But sometimes, the direct access to the compressed data is required. For example if you want to compress data in memory using TECLMemoryStream and then write this compressed data to a special place in a file or to transmit this data by packets over the internet or a network.
Easy Compression Library provides an easy way to get the direct access to compressed (encrypted) data in ECL streams through its CompressedDataStream property.
Use this property as a stream to access compressed data directly.
The following example illustrates how to compress test.txt in memory, save the compressed data to a file with special offset, load again and then access the transparently decompressed data.
Example:
var
CompMS: TECLMemoryStream;
FS: TFileStream;
begin
CompMS := TECLMemoryStream.Create('', ppmMax);
CompMS.LoadFromFile('c:\test.txt');
FS := TFileStream.Create('custom.dat', fmCreate);
FS.Position := 10;
CompMS.CompressedDataStream.SaveToStream(FS);
FS.Free;
CompMS.Free;
CompMS := TECLMemoryStream.Create;
FS := TFileStream.Create('custom.dat', fmOpenReadWrite or fmShareDenyNone);
FS.Position := 10;
CompMS.CompressedDataStream.CopyFrom(FS, FS.Size-10);
ShowMessage('The size of decompressed data is: ' + IntToStr(CompMS.Size));
FS.Free;
CompMS.Free;
end;
|