Delphi World - это проект, являющийся сборником статей и малодокументированных возможностей  по программированию в среде Delphi. Здесь вы найдёте работы по следующим категориям: delphi, delfi, borland, bds, дельфи, делфи, дэльфи, дэлфи, programming, example, программирование, исходные коды, code, исходники, source, sources, сорцы, сорсы, soft, programs, программы, and, how, delphiworld, базы данных, графика, игры, интернет, сети, компоненты, классы, мультимедиа, ос, железо, программа, интерфейс, рабочий стол, синтаксис, технологии, файловая система...
Сохранить несколько потоков в сжатый зашифрованный файл

Оформил: DeeCo
Автор: http://www.swissdelphicenter.ch

{ 
 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;
Проект Delphi World © Выпуск 2002 - 2024
Автор проекта: USU Software
Вы можете выкупить этот проект.