Советы по Delphi


Blob-поля, потоки, компрессия... - часть 2


/p>

Файл Main.pas:

unit Main;

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,Db, DBTables, StdCtrls, ComCtrls, ExtCtrls, DBCtrls, Buttons, swDBPanl,swRecPos;type
TfrmMain = class(TForm)T1: TTable;T1ID: TAutoIncField;T1BLOBData: TBlobField;OD: TOpenDialog;P1: TPanel;SB1: TSpeedButton;SB2: TSpeedButton;SB3: TSpeedButton;SB4: TSpeedButton;DS: TDataSource;DBN: TDBNavigator;procedure SB1Click(Sender: TObject);procedure SB2Click(Sender: TObject);procedure SB3Click(Sender: TObject);procedure SB4Click(Sender: TObject);procedure DSDataChange(Sender: TObject; Field: TField);procedure FormShow(Sender: TObject);privateEF:boolean;procedure SetButtons;procedure UpdateEditor;procedure StoreFromFile;procedure StoreFromEditor;public{ Public declarations }end;
var frmMain: TfrmMain;

implementation
uses
ZLib;

{$R *.DFM}

const LID:longint=0;
procedure TfrmMain.SetButtons;
var c1:boolean;
begin c1:=T1.RecordCount>0;
SB2.Enabled:=not EF and c1;SB3.Enabled:=not EF and c1;SB4.Enabled:=EF;end;

procedure TfrmMain.UpdateEditor;
var Buf:TStream;
ZStream:TCustomZLibStream;id:longint;begin
id:=T1ID.AsInteger;if (id=LID) and not EF then exit else LID:=id;Buf:=TMemoryStream.Create;T1BLOBData.SaveToStream(Buf);if Buf.Size>0 then beginZStream:=TDecompressionStream.Create(Buf);RE.Lines.LoadFromStream(ZStream);ZStream.Free;end else RE.Lines.Clear;Buf.Free;end;

procedure TfrmMain.StoreFromFile;
var InFile,Buf:TStream;
ZStream:TCustomZLibStream;begin
if not
OD.Execute then exit;T1.AppendRecord([NULL]);InFile:=TFileStream.Create(OD.FileName,fmOpenRead);Buf:=TMemoryStream.Create;ZStream:=TCompressionStream.Create(clMax,Buf);ZStream.CopyFrom(InFile,0);ZStream.Free;T1.Edit;T1BLOBData.LoadFromStream(Buf);T1.Post;Buf.Free;InFile.Free;LID:=0;UpdateEditor;end;

procedure TfrmMain.StoreFromEditor;
var InStream,Buf:TStream;
ZStream:TCustomZLibStream;begin
InStream:=TMemoryStream.Create;Buf:=TMemoryStream.Create;RE.Lines.SaveToStream(InStream);ZStream:=TCompressionStream.Create(clMax,Buf);ZStream.CopyFrom(InStream,0);ZStream.Free;T1.Edit;T1BLOBData.LoadFromStream(Buf);T1.Post;UpdateEditor;end;

procedure TfrmMain.SB1Click(Sender: TObject);
begin
if
EF then begin StoreFromEditor;RE.ReadOnly:=true;DBN.Enabled:=true;EF:=false;SB1.Hint:='Добавить';end else StoreFromFile;SetButtons;end;

procedure TfrmMain.SB2Click(Sender: TObject);
begin
if
MessageDlg('Удалять запись?',mtConfirmation,[mbYes,mbNo],0)=mrYes thenbegin T1.Delete;SetButtons;end;end;

procedure TfrmMain.SB3Click(Sender: TObject);
begin
DBN.Enabled:=false;EF:=true;SB1.Hint:='Внести изменения';RE.ReadOnly:=false;SetButtons;end;

procedure TfrmMain.SB4Click(Sender: TObject);
begin
UpdateEditor;DBN.Enabled:=true;EF:=false;SB1.Hint:='Добавить';RE.ReadOnly:=true;end;

procedure TfrmMain.DSDataChange(Sender: TObject; Field: TField);
begin
if
assigned(frmMain) and Visible and not EF then
begin
UpdateEditor;SetButtons;end;end;

procedure TfrmMain.FormShow(Sender: TObject);
begin
EF:=false;SetButtons;DSDataChange(nil,nil);end;

end.

Файл CompBLOB.dpr:

program CompBLOB;
uses
Forms,Main in 'Main.pas' {frmMain};
{$R *.RES}

begin
Application.Initialize;Application.CreateForm(TfrmMain, frmMain);Application.Run;end.

С наилучшими пожеланиями
. [000565]




Начало  Назад  Вперед



Книжный магазин