Сохранить несколько потоков в сжатый зашифрованный файл
{
This example shows you how to save many streams into a single file and load it again.
You need to install the HKStream component from the Demo-Download.
The HKStreams component enables you to store streams with LHA compression if wanted,
and can also be encrypted with blowfish.
THKStreams is also smart, if you load afterwards an encrypted or compressed (or both) file,
it will now how to read it, and can also call your event that asks the user
for a password if needed.
In the following example we will store several components
(TMemo, TListbox, some other controls)
and a Version-Information in a single file and then read it back.
THKStreams enables you to write your own file format and easily access streams
in a file, as they are stored in different sections.
}
{
Dieses Beispiel zeigt, wie man mehrere Stream in eine Datei speichern kann.
Dafur wird die THKStreams Komponente verwendet, welche es unter anderem erlaubt,
eine Datei mit LHA zu komprimieren und mit Blowfish zu verschlussseln.
Auch kann einer Datei ein Passwort hinzugefugt werden, welches dann beim Offnen
der Datei vom Benutzer eingegeben werden muss.
Im Beispiel werden einige Komponenten wie TMemo, TListbox und diverse Controls einer
Groupbox und Versions-Informationen in einer einzigen Datei gespeichert.
Mit der THKStreams Komponente kann man leicht sein eigenes Dateiformat schreiben und
sehr einfach auf Streams zugreifen, da diese in einzelne Sektionen unterteilt sind.
}
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;
Опубликовал Kest
November 13 2008 21:46:53 ·
0 Комментариев ·
7420 Прочтений ·
• Не нашли ответ на свой вопрос? Тогда задайте вопрос в комментариях или на форуме! •
Комментарии
Нет комментариев.
Добавить комментарий
Рейтинги
Рейтинг доступен только для пользователей.
Пожалуйста, залогиньтесь или зарегистрируйтесь для голосования.
Нет данных для оценки.
Гость
Вы не зарегистрированны? Нажмите здесь для регистрации.