Author: Jonas Bilinkevicius
Is is possible to somehow embed an external executable file in a Delphi program? Or
maybe another type of file for that matter like a zip file for instance. I want the
file to be extractable from my program later on so that I can use it on another
system.
Answer:
Here is a simple little component where you can save a file in the form file. You
can add compression/decompression/encryption or whatever you like, but this
demonstrates a possible storage process for data. Quick and simple to use.
1 unit Unit2;
2 3 interface4 5 uses6 Classes, Sysutils;
7 8 type9 TFileResourceComponent = class(TComponent)
10 private11 FStream: TMemoryStream;
12 FFileName: TFileName;
13 procedure SetFileName(const Value: TFileName);
14 procedure ReadData(Stream: TStream);
15 procedure WriteData(Stream: TStream);
16 public17 procedure DefineProperties(Filer: TFiler); override;
18 constructor Create(AOwner: TComponent); override;
19 destructor Destroy; override;
20 published21 property FileName: TFileName read FFileName write SetFileName;
22 end;
23 24 procedureregister;
25 26 implementation27 28 procedureregister;
29 begin30 RegisterComponents('REDSYS', [TFileResourceComponent]);
31 end;
32 33 { TFileResourceComponent }34 35 constructor TFileResourceComponent.Create(AOwner: TComponent);
36 begin37 inherited;
38 FStream := TMemoryStream.Create;
39 end;
40 41 procedure TFileResourceComponent.DefineProperties(Filer: TFiler);
42 begin43 inherited;
44 Filer.DefineBinaryProperty('Data', ReadData, WriteData, True);
45 end;
46 47 destructor TFileResourceComponent.Destroy;
48 begin49 inherited;
50 FStream.Free;
51 end;
52 53 procedure TFileResourceComponent.ReadData(Stream: TStream);
54 begin55 ifnot (csDesigning in ComponentState) then56 FStream.CopyFrom(Stream, Stream.Size);
57 end;
58 59 procedure TFileResourceComponent.SetFileName(const Value: TFileName);
60 begin61 FFileName := Value;
62 end;
63 64 procedure TFileResourceComponent.WriteData(Stream: TStream);
65 var66 FS:
67 TMemoryStream;
68 begin69 if FileExists(FFileName) then70 begin71 FS := TMemoryStream.Create;
72 try73 FS.LoadFromFile(FFileName);
74 Stream.CopyFrom(FS, FS.Size);
75 finally76 FS.Free;
77 end;
78 end;
79 end;
80 81 end.