Title: How to save many streams in a (compressed, encrypted) file
uses
HKStreamCol; { from Demo-Download }
private
{ Private declarations }
function SaveHKFile(FileName: TFileName): boolean;
function LoadHKFile(FileName: TFileName): boolean;
public
{ Public declarations }
end;
implementation
{$R *.dfm}
//--------------------------------------------------------
// Save our File
function TfrmMain.SaveHKFile(FileName: TFileName): Boolean;
//--------------------------------------------------------
var
ms: TMemoryStream;
i: integer;
s: string;
StrList: TStringList;
begin
ms := TMemoryStream.Create;
try
with HKStreams1 do
begin
// Version Info
//--------------------------------
i := 1;
ms.Write(I, SizeOf(I));
AddStream('Version-Info', ms);
ms.Clear;
// Author Info
//--------------------------------
s := 'Your Name';
ms.WriteBuffer(s[1], Length(s));
AddStream('Author-Info', ms);
ms.Clear;
// Memo1
//--------------------------------
Memo1.Lines.SaveToStream(ms);
AddStream('Memo1', ms);
ms.Clear;
// ListBox1
//-------------------------------
ListBox1.Items.SaveToStream(ms);
AddStream('ListBox1', ms);
ms.Clear;
// Different Controls on Groupbox1
//-------------------------------
// Look at the Demo for GetSettings implementation
StrList := TStringList.Create;
try
GetSettings(StrList, GroupBox1);
StrList.SaveToStream(ms);
finally
StrList.Free;
end;
AddStream('Settings', ms);
ms.Clear;
// Save everything
// -----------------------------
SaveToFile(FileName);
ClearStreams;
end; {with}
finally
ms.Free;
end;
end;
//--------------------------------------------------------
// Load our File
function TfrmMain.LoadHKFile(FileName: TFileName): Boolean;
//--------------------------------------------------------
var
ms: TMemoryStream;
i: Integer;
s: string;
StrList: TStringList;
begin
if not FileExists(FileName) then
begin
ShowMessage('File not found');
// Customize Error handling...
Exit;
end;
try
HKStreams1.LoadFromFile(FileName);
except
on ECorruptFile do Exit;
on EStreamError do Exit;
end;
ms := TMemoryStream.Create;
try
with HKStreams1 do
begin
// Version Info
//--------------------------------
GetStream('Version-Info', ms);
ms.read(i, SizeOf(i));
label1.Caption := 'Version: ' + IntToStr(i);
ms.Clear;
// Author Info
//--------------------------------
GetStream('Author-Info', ms);
SetLength(S, ms.Size);
ms.read(S[1], ms.Size);
Label2.Caption := 'Author: ' + s;
ms.Clear;
// Memo1
//--------------------------------
GetStream('Memo1', ms);
Memo1.Lines.LoadFromStream(ms);
ms.Clear;
// ListBox1
//-------------------------------
GetStream('ListBox1', ms);
ListBox1.Items.LoadFromStream(ms);
ms.Clear;
// Different Controls on Groupbox1
//-------------------------------
// Look at the Demo for SetSettings implementation
GetStream('Settings', ms);
StrList := TStringList.Create;
try
StrList.LoadFromStream(ms);
SetSettings(StrList);
finally
StrList.Free;
end;
{ ----------------------------- }
ClearStreams;
end; {with}
finally
ms.Free;
end;
end;
//--------------------------------------------------------
// Save Dialog
procedure TfrmMain.btnSaveClick(Sender: TObject);
//--------------------------------------------------------
begin
if SaveDialog1.Execute then
begin
SaveHKFile(SaveDialog1.FileName);
end;
// Clear controls
ListBox1.Clear;
Memo1.Clear;
Edit1.Clear;
Edit2.Clear;
Edit3.Clear;
Edit4.Clear;
CheckBox1.Checked := not CheckBox1.Checked;
CheckBox2.Checked := not CheckBox2.Checked;
end;
//--------------------------------------------------------
// Open Dialog
procedure TfrmMain.btnLoadClick(Sender: TObject);
//--------------------------------------------------------
begin
if OpenDialog1.Execute then
begin
LoadHKFile(OpenDialog1.FileName);
end;
end;