Files Delphi

Title: Add File Into Other File
Question: How to attach any file into any file ?
Answer:
unit GDataOnFile;
interface
uses Classes, SysUtils, Windows;
type
TGDataOnFileState = (gdonfNotExists,gdonfAttachFail,
gdonfLoadFail,gdonfDone,gdonfNoAttachment);
TGDataOnFile = class(TComponent)
private
public
function AttachFileToFile(pFileNameTarget,
pFileNameAttachment,
pFileNameDestination : string) : TGDataOnFileState;
function AttachStreamToFile(pFileNameTarget : string;
MemoryStream : TMemoryStream;
pFileNameDestination,
pType : string) : TGDataOnFileState;
function LoadFileFromFile(var pType : string;
pFileNameSource,
pFileNameDestination : string) : TGDataOnFileState;
function LoadStreamFromFile(var pType : string; pFileNameSource : string;
MemoryStream : TMemoryStream;
pFileNameDestination : string) : TGDataOnFileState;
function FindAttachment(pFileNameSource : string) : string;
function DeleteAttachment(pFileNameSource,
pFileNameDestination : string) : boolean;
published
end;
procedure Register;
implementation
function TGDataOnfile.AttachStreamToFile(pFileNameTarget : string;
MemoryStream : TMemoryStream;
pFileNameDestination, pType : string) : TGDataOnFileState;
var
acStream : TFileStream;
acM : TMemoryStream;
acSL : TStringList;
iSize : integer;
begin
if not FileExists(pFileNameTarget) then
begin
Result := gdonfNotExists;
Exit;
end;
CopyFile(PChar(pFileNameTarget),PChar(pFileNameDestination),false);
try
acStream := TFileStream.Create(pFileNameDestination, fmOpenWrite or fmShareDenyWrite);
except
Result := gdonfAttachFail;
Exit;
end;
acM := TMemoryStream.Create;
acSL := TStringList.Create;
try
try
MemoryStream.Seek(0,soFromBeginning);
acStream.Seek(0,soFromEnd);
acStream.CopyFrom(MemoryStream,0);
iSize := MemoryStream.Size + Sizeof(integer);
acStream.Write(iSize,SizeOf(iSize));
acSL.Add('GSoft' + pType);
acSL.SaveToStream(acM);
acM.Seek(0,soFromBeginning);
acStream.Seek(0,soFromEnd);
acStream.CopyFrom(acM,0);
iSize := acM.Size + Sizeof(integer);
acStream.Write(iSize,SizeOf(iSize));
Result := gdonfDone;
except
Result := gdonfAttachFail;
end;
finally
acStream.Free;
acSL.Free;
acM.Free;
end;
end;
function TGDataOnFile.AttachFileToFile(pFileNameTarget,
pFileNameAttachment,
pFileNameDestination : string) : TGDataOnFileState;
var
acStream : TFileStream;
MemoryStream : TMemoryStream;
acM : TMemoryStream;
acSL : TStringList;
iSize : integer;
begin
if not FileExists(pFileNameTarget) then
begin
Result := gdonfNotExists;
Exit;
end;
CopyFile(PChar(pFileNameTarget),PChar(pFileNameDestination),false);
try
acStream := TFileStream.Create(pFileNameDestination, fmOpenWrite or fmShareDenyWrite);
except
Result := gdonfAttachFail;
Exit;
end;
acM := TMemoryStream.Create;
MemoryStream := TMemoryStream.Create;
MemoryStream.LoadFromFile(pFileNameAttachment);
acSL := TStringList.Create;
try
try
MemoryStream.Seek(0,soFromBeginning);
acStream.Seek(0,soFromEnd);
acStream.CopyFrom(MemoryStream,0);
iSize := MemoryStream.Size + Sizeof(integer);
acStream.Write(iSize,SizeOf(iSize));
acSL.Add('GSoft' + ExtractFileExt(pFileNameAttachment));
acSL.SaveToStream(acM);
acM.Seek(0,soFromBeginning);
acStream.Seek(0,soFromEnd);
acStream.CopyFrom(acM,0);
iSize := acM.Size + Sizeof(integer);
acStream.Write(iSize,SizeOf(iSize));
Result := gdonfDone;
except
Result := gdonfAttachFail;
end;
finally
acStream.Free;
acSL.Free;
acM.Free;
MemoryStream.Free;
end;
end;
function TGDataOnFile.LoadStreamFromFile(var pType : string;
pFileNameSource : string;
MemoryStream : TMemoryStream;
pFileNameDestination : string) : TGDataOnFileState;
var
acStream : TFileStream;
iSize : integer;
TypeStream : TMemoryStream;
acSL : TStringList;
begin
if not FileExists(pFileNameSource) then
begin
Result := gdonfNotExists;
Exit;
end;
try
acStream := TFileStream.Create(pFileNameSource,fmOpenRead or fmShareDenyWrite);
except
Result := gdonfLoadFail;
Exit;
end;
if acStream.Size = 0 then
begin
Result := gdonfLoadFail;
acStream.Free;
Exit;
end;
acSL := TStringList.Create;
TypeStream := TMemoryStream.Create;
try
try
acStream.Seek(-SizeOf(integer),soFromEnd);
acStream.Read(iSize,SizeOf(iSize));
if iSize acStream.Size then
begin
acStream.Free;
acSL.Free;
TypeStream.Free;
Result := gdonfLoadFail;
Exit;
end;
acStream.Seek(-iSize,soFromEnd);
TypeStream.SetSize(iSize - SizeOf(integer));
TypeStream.CopyFrom(acStream,iSize - SizeOf(iSize));
TypeStream.Seek(0,soFromBeginning);
acSL.LoadFromStream(TypeStream);
if Pos('GSoft',acSL.Strings[0]) 0 then
begin
pType := ExtractFileExt(acSL.Strings[0]);
acStream.Seek(-SizeOf(integer) - SizeOf(integer) -
TypeStream.Size,soFromEnd);
acStream.Read(iSize,SizeOf(iSize));
if iSize acStream.Size then
begin
acStream.Free;
acSL.Free;
TypeStream.Free;
Result := gdonfLoadFail;
Exit;
end;
acStream.Seek(-iSize - Sizeof(integer) - TypeStream.Size,soFromEnd);
MemoryStream.SetSize(iSize - SizeOf(integer));
MemoryStream.CopyFrom(acStream,iSize - SizeOf(iSize));
MemoryStream.Seek(0,soFromBeginning);
if pFileNameDestination '' then
MemoryStream.SaveToFile(ExtractFileDir(pFileNameDestination) + '\' +
ChangeFileExt(ExtractFileName
(pFileNameDestination),pType));
Result := gdonfDone;
end
else
Result := gdonfNoAttachment;
except
Result := gdonfLoadFail;
end;
finally
acStream.Free;
acSL.Free;
TypeStream.Free;
end;
end;
function TGDataOnFile.LoadFileFromFile(var pType : string;
pFileNameSource,
pFileNameDestination : string) : TGDataOnFileState;
var
acStream : TFileStream;
iSize : integer;
MemoryStream, TypeStream : TMemoryStream;
acSL : TStringList;
begin
if not FileExists(pFileNameSource) then
begin
Result := gdonfNotExists;
Exit;
end;
try
acStream := TFileStream.Create(pFileNameSource,fmOpenRead or fmShareDenyWrite);
except
Result := gdonfLoadFail;
Exit;
end;
if acStream.Size = 0 then
begin
Result := gdonfLoadFail;
acStream.Free;
Exit;
end;
acSL := TStringList.Create;
MemoryStream := TMemoryStream.Create;
TypeStream := TMemoryStream.Create;
try
try
acStream.Seek(-SizeOf(integer),soFromEnd);
acStream.Read(iSize,SizeOf(iSize));
if iSize acStream.Size then
begin
acStream.Free;
acSL.Free;
MemoryStream.Free;
TypeStream.Free;
Result := gdonfLoadFail;
Exit;
end;
acStream.Seek(-iSize,soFromEnd);
TypeStream.SetSize(iSize - SizeOf(integer));
TypeStream.CopyFrom(acStream,iSize - SizeOf(iSize));
TypeStream.Seek(0,soFromBeginning);
acSL.LoadFromStream(TypeStream);
if Pos('GSoft',acSL.Strings[0]) 0 then
begin
pType := ExtractFileExt(acSL.Strings[0]);
acStream.Seek(-SizeOf(integer) - SizeOf(integer) -
TypeStream.Size,soFromEnd);
acStream.Read(iSize,SizeOf(iSize));
if iSize acStream.Size then
begin
acStream.Free;
acSL.Free;
MemoryStream.Free;
TypeStream.Free;
Result := gdonfLoadFail;
Exit;
end;
acStream.Seek(-iSize - Sizeof(integer) - TypeStream.Size,soFromEnd);
MemoryStream.SetSize(iSize - SizeOf(integer));
MemoryStream.CopyFrom(acStream,iSize - SizeOf(iSize));
MemoryStream.Seek(0,soFromBeginning);
MemoryStream.SaveToFile(ExtractFileDir(pFileNameDestination) + '\' +
ChangeFileExt(ExtractFileName(pFileNameDestination),
pType));
Result := gdonfDone;
end
else
Result := gdonfNoAttachment;
except
Result := gdonfLoadFail;
end;
finally
acStream.Free;
acSL.Free;
MemoryStream.Free;
TypeStream.Free;
end;
end;
function TGDataOnFile.FindAttachment(pFileNameSource : string) : string;
var
acStream : TFileStream;
iSize : integer;
TypeStream : TMemoryStream;
acSL : TStringList;
begin
if not FileExists(pFileNameSource) then
begin
Result := '';
Exit;
end;
try
acStream := TFileStream.Create(pFileNameSource,fmOpenRead or fmShareDenyWrite);
except
Result := '';
Exit;
end;
if acStream.Size = 0 then
begin
Result := '';
acStream.Free;
Exit;
end;
acSL := TStringList.Create;
TypeStream := TMemoryStream.Create;
try
try
acStream.Seek(-SizeOf(integer),soFromEnd);
acStream.Read(iSize,SizeOf(iSize));
if iSize acStream.Size then
begin
acStream.Free;
acSL.Free;
TypeStream.Free;
Result := '';
Exit;
end;
acStream.Seek(-iSize,soFromEnd);
TypeStream.SetSize(iSize - SizeOf(integer));
TypeStream.CopyFrom(acStream,iSize - SizeOf(iSize));
TypeStream.Seek(0,soFromBeginning);
acSL.LoadFromStream(TypeStream);
if Pos('GSoft',acSL.Strings[0]) 0 then
Result := ExtractFileExt(acSL.Strings[0])
else
Result := '';
except
Result := '';
end;
finally
acStream.Free;
acSL.Free;
TypeStream.Free;
end;
end;
function TGDataOnFile.DeleteAttachment(pFileNameSource,
pFileNameDestination : string) : boolean;
var
acStream, acStreamO : TFileStream;
iSize : integer;
MemoryStream, TypeStream, OriginStream : TMemoryStream;
acSL : TStringList;
begin
if not FileExists(pFileNameSource) then
begin
Result := false;
Exit;
end;
try
acStream := TFileStream.Create(pFileNameSource,fmOpenRead or fmShareDenyWrite);
except
Result := false;
Exit;
end;
try
acStreamO := TFileStream.Create(pFileNameDestination,fmCreate or fmShareDenyWrite);
except
acStream.Free;
Result := false;
Exit;
end;
if acStream.Size = 0 then
begin
Result := false;
acStream.Free;
acStreamO.Free;
Exit;
end;
acSL := TStringList.Create;
MemoryStream := TMemoryStream.Create;
TypeStream := TMemoryStream.Create;
OriginStream := TMemoryStream.Create;
try
try
acStream.Seek(-SizeOf(integer),soFromEnd);
acStream.Read(iSize,SizeOf(iSize));
if iSize acStream.Size then
begin
acStream.Free;
acStreamO.Free;
acSL.Free;
MemoryStream.Free;
TypeStream.Free;
OriginStream.Free;
Result := false;
Exit;
end;
acStream.Seek(-iSize,soFromEnd);
TypeStream.SetSize(iSize - SizeOf(integer));
TypeStream.CopyFrom(acStream,iSize - SizeOf(iSize));
TypeStream.Seek(0,soFromBeginning);
acSL.LoadFromStream(TypeStream);
if Pos('GSoft',acSL.Strings[0]) 0 then
begin
acStream.Seek(-SizeOf(integer) - SizeOf(integer) -
TypeStream.Size,soFromEnd);
acStream.Read(iSize,SizeOf(iSize));
if iSize acStream.Size then
begin
acStream.Free;
acStreamO.Free;
acSL.Free;
MemoryStream.Free;
TypeStream.Free;
OriginStream.Free;
Result := false;
Exit;
end;
acStream.Seek(-iSize - Sizeof(integer) - TypeStream.Size,soFromEnd);
MemoryStream.SetSize(iSize - SizeOf(integer));
MemoryStream.CopyFrom(acStream,iSize - SizeOf(iSize));
MemoryStream.Seek(0,soFromBeginning);
acStream.Seek(0,soFromBeginning);
acStreamO.Seek(0,soFromBeginning);
acStreamO.CopyFrom(acStream,acStream.Size - SizeOf(integer) - SizeOf(integer) -
TypeStream.Size - MemoryStream.Size);
iSize := acStream.Size - SizeOf(integer) - SizeOf(integer) -
TypeStream.Size - MemoryStream.Size;
acStream.Write(iSize,SizeOf(iSize));
Result := true;
end
else
Result := false;
except
Result := false;
end;
finally
acStream.Free;
acStreamO.Free;
acSL.Free;
MemoryStream.Free;
TypeStream.Free;
OriginStream.Free;
end;
end;
procedure Register;
begin
RegisterComponents('GSoft',[TGDataOnFile]);
end;
-----------------------------------------------------------------------
Example :
Code to attach File into File :
GDataOnFile1.AttachFileToFile('C:\Test01.exe','C:\Test02.exe','C:\Test3.exe');
The destination file can be the same file with the target :
GDataOnFile1.AttachFileToFile('C:\Test01.exe','C:\Test02.exe','C:\Test1.exe');
Code to attach Stream into File :
acMemoryStream := TMemoryStream.Create;
try
Memo1.Lines.SaveToStream(acMemoryStream);
GDataOnFile1.AttachStreamToFile('C:\Test01',acMemoryStream,'C:\Test02.exe',
'.txt');
finally
acMemoryStream.Free;
end;
Code to load File From File :
GDataOnFile1.LoadFileFromFile('','C:\Test01.exe','C:\Test02.exe);
Code to load Stream From File :
acMemoryStream := TMemoryStream.Create;
try
GDataOnFile1.LoadStreamFromFile('','C:\Test01.exe',acMemoryStream,'');
acMemoryStream.SaveToFile('C:\Test02.exe');
finally
acMemoryStream.Free;
end;
or
acMemoryStream := TMemoryStream.Create;
try
GDataOnFile1.LoadStreamFromFile('','C:\Test01.exe',acMemoryStream,
'C:\Test02.txt');
Memo1.Lines.LoadFromStream(acMemoryStream);
finally
acMemoryStream.Free;
end;
To check if a File have a attach file :
GDataOnFile1.FindAttachment('C:\Test01.exe')
The result true mean the file have it, false not have it
To delete the attach file and save the result into other file :
GDataOnFile1.DeleteAttachment('C:\Test01.exe','C:\Test02.exe');
The result of file Test02.exe is the file Test01.exe without attachment file.