Recent

Author Topic: How to compress Blob Fields  (Read 1952 times)

DeBritto

  • Jr. Member
  • **
  • Posts: 68
How to compress Blob Fields
« on: May 27, 2016, 08:33:08 pm »
Hi folks,

I would like to know if it's possible to compress (using Zlib functions or something else) a string before stores the value in a Firebird Blob field? I'm asking because I'm developing an remote server-cliente application  that needs to share lots of information through internet and this strategy could save some time.

Best regards

Cyr

  • Newbie
  • Posts: 6
Re: How to compress Blob Fields
« Reply #1 on: May 27, 2016, 09:00:22 pm »
This is my code (not the best decision may be) to save some compressed data in to the BLOB field:

uses ...., ZStream

procedure TForm1.CargoGrid3DButtonClick(Sender: TObject; aCol, aRow: Integer);
var
 OutVertexStream,OutIndexStream: TMemoryStream;
 GzStream1,GzStream2 : Tcompressionstream;
 i:longint;
 s: string;
 d: Char;
begin
  if loadfromstl=false then exit;
  OutVertexStream := TMemoryStream.Create;
  OutIndexStream := TMemoryStream.Create;
   try
      GzStream1 := Tcompressionstream.create(clmax,OutVertexStream);
      GzStream2 := Tcompressionstream.create(clmax,OutIndexStream);
      try
        GzStream1.WriteBuffer(vertex[0],length(vertex)*12);
        GzStream2.WriteBuffer(vertexindex[0],length(vertexindex)*sizeof(vertexindex));
      finally
         GzStream1.free;
         GzStream2.free;
      end;

      OutVertexStream.Position:=0;
      OutIndexStream.Position:=0;


      SQLQuery1.Close;
      SQLQuery1.SQL.Clear;
      s:='select * from CargoTypeSizes where CargoTypeSizeID='+CargoGrid3D.Cells[0,CargoGrid3D.Row];
      SQLQuery1.SQL.Text :=s ;
      SQLQuery1.Open;
      SQLQuery1.Edit;
      s:='';
      for i := 1 to OutVertexStream.Size do
       begin
         OutVertexStream.Read(d, 1);
         s := s + d;
       end;
      SQLQuery1.FieldByName('vertexes').AsString:=s;
      s:='';
      for i := 1 to OutIndexStream.Size do
       begin
         OutIndexStream.Read(d, 1);
         s := s + d;
       end;
      SQLQuery1.FieldByName('indexes').AsString:=s;
      SQLQuery1.Post;
      SQLQuery1.UpdateMode := upWhereAll;
      SQLQuery1.ApplyUpdates;
      SQLTransaction1.Commit;
   finally
      CargoGrid3D.Cells[2,CargoGrid3D.Row]:='loaded';
      CargoGrid3D.Cells[3,CargoGrid3D.Row]:=inttostr(OutVertexStream.Size+OutIndexStream.Size);
      OutVertexStream.free;
      OutIndexStream.Free;
   end;

 
end;
« Last Edit: May 27, 2016, 09:29:20 pm by Cyr »

DeBritto

  • Jr. Member
  • **
  • Posts: 68
Re: How to compress Blob Fields
« Reply #2 on: May 28, 2016, 05:02:45 am »
Hi Cyr,
Thank you so much to share your code! I'll try it. But, let me ask you one more thing, you said that maybe this is not the best decision (to compress data into the Blob field). Why? Can you share some thoughts?

Best regards

 

TinyPortal © 2005-2018