1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Donation with Paypal!!!

    Go to your paypal account and send directly donation to [email protected]

    1 month - 10 $ - Standart VIP

    6 months - 20 $- Standart VIP

    1 year - 30 $- Standart VIP

    2 years - 50 $- Standart VIP

    Gold member for life - 150 $- Standart VIP

    High Vip (Standart VIP include) group please send PM or email to [email protected] for info

    After Donation please send email to [email protected]

  3. Donation Ways 2020


    Paysend
  4. Telegram
Dismiss Notice

Donation with Paypal!!!

Go to your paypal account and send directly donation to [email protected]

1 month - 10 $ - Standart VIP

6 months - 20 $- Standart VIP

1 year - 30 $- Standart VIP

2 years - 50 $- Standart VIP

Gold member for life - 150 $- Standart VIP

High Vip (Standart VIP include) group please send PM or email to [email protected] for info

After Donation please send email to [email protected]

Dismiss Notice
For open hidden message no need write thanks, thank etc. Enough is click to like button on right side of thread.

Data in a File

Discussion in 'Delphi Programming' started by grgwjy, Apr 15, 2015.

  1. grgwjy
    Offline

    grgwjy DF Member DF Gold User

    Data in a File

    This code i made for an idea to HIDE-THANKS my data that i save in a file and i HIDE-THANKS that file into other file (usually i pick an image file), so if someone look that file he can still see the image, as long as he do not edit the image and save it, my data will safe in that image file.
    Sometime i call it "file in file"

    Code:
    unit GDataInFile2008;
    
    interface
    
    uses Classes, SysUtils, Windows;
    
    type
     TGDataInFileState = (gdinfNotExists,gdinfAttachFail,
                          gdinfLoadFail,gdinfDone,gdinfNoAttachment);
    
     TGDataInFile2008 = class(TComponent)
      private
    
      public
       function AttachToFile(pFileNameTarget,
                             pFileNameAttachment,
                             pFileNameDestination : string) : TGDataInFileState;
       function StreamAttachToFile(pFileNameTarget : string;
                                   MemoryStream : TMemoryStream;
                                   pFileNameDestination, pType : string) : TGDataInFileState;
       function LoadDataFromFile(var pType : string;
                                 pFileNameSource,
                                 pFileNameDestination : string) : TGDataInFileState;
       function StreamLoadDataFromFile(var pType : string; pFileNameSource : string;
                                   MemoryStream : TMemoryStream;
                                   pFileNameDestination : string) : TGDataInFileState;
       function FindAttachment(pFileNameSource : string) : string;
       function DeleteAttachment(pFileNameSource,
                                 pFileNameDestination : string) : boolean;
    
      published
     end;
    
    procedure Register;
    
    implementation
    
    function TGDataInfile2008.StreamAttachToFile(pFileNameTarget : string;
                                   MemoryStream : TMemoryStream;
                                   pFileNameDestination, pType : string) : TGDataInFileState;
    var
     acStream : TFileStream;
     acM : TMemoryStream;
     acSL : TStringList;
     iSize : integer;
    begin
     if not FileExists(pFileNameTarget) then
     begin
      Result := gdinfNotExists;
      Exit;
     end;
     CopyFile(PChar(pFileNameTarget),PChar(pFileNameDestination),false);
     try
      acStream := TFileStream.Create(pFileNameDestination, fmOpenWrite or fmShareDenyWrite);
     except
      Result := gdinfAttachFail;
      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 := gdinfDone;
      except
       Result := gdinfAttachFail;
      end;
     finally
      acStream.Free;
      acSL.Free;
      acM.Free;
     end;
    end;
    
    function TGDataInFile2008.AttachToFile(pFileNameTarget,
                                           pFileNameAttachment,
                                           pFileNameDestination : string) : TGDataInFileState;
    var
     acStream : TFileStream;
     MemoryStream : TMemoryStream;
     acM : TMemoryStream;
     acSL : TStringList;
     iSize : integer;
    begin
     if not FileExists(pFileNameTarget) then
     begin
      Result := gdinfNotExists;
      Exit;
     end;
     CopyFile(PChar(pFileNameTarget),PChar(pFileNameDestination),false);
     try
      acStream := TFileStream.Create(pFileNameDestination, fmOpenWrite or fmShareDenyWrite);
     except
      Result := gdinfAttachFail;
      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 := gdinfDone;
      except
       Result := gdinfAttachFail;
      end;
     finally
      acStream.Free;
      acSL.Free;
      acM.Free;
      MemoryStream.Free;
     end;
    end;
    
    function TGDataInFile2008.StreamLoadDataFromFile(var pType : string; pFileNameSource : string;
                                   MemoryStream : TMemoryStream;
                                   pFileNameDestination : string) : TGDataInFileState;
    var
     acStream : TFileStream;
     iSize : integer;
     TypeStream : TMemoryStream;
     acSL : TStringList;
    begin
     if not FileExists(pFileNameSource) then
     begin
      Result := gdinfNotExists;
      Exit;
     end;
     try
      acStream := TFileStream.Create(pFileNameSource,fmOpenRead or fmShareDenyWrite);
     except
      Result := gdinfLoadFail;
      Exit;
     end;
     if acStream.Size = 0 then
     begin
      Result := gdinfLoadFail;
      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 := gdinfLoadFail;
        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 := gdinfLoadFail;
         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 := gdinfDone;
       end
       else
        Result := gdinfNoAttachment;
      except
       Result := gdinfLoadFail;
      end;
     finally
      acStream.Free;
      acSL.Free;
      TypeStream.Free;
     end;
    end;
    
    function TGDataInFile2008.LoadDataFromFile(var pType : string;
                                           pFileNameSource,
                                           pFileNameDestination : string) : TGDataInFileState;
    var
     acStream : TFileStream;
     iSize : integer;
     MemoryStream, TypeStream : TMemoryStream;
     acSL : TStringList;
    begin
     if not FileExists(pFileNameSource) then
     begin
      Result := gdinfNotExists;
      Exit;
     end;
     try
      acStream := TFileStream.Create(pFileNameSource,fmOpenRead or fmShareDenyWrite);
     except
      Result := gdinfLoadFail;
      Exit;
     end;
     if acStream.Size = 0 then
     begin
      Result := gdinfLoadFail;
      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 := gdinfLoadFail;
        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 := gdinfLoadFail;
         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 := gdinfDone;
       end
       else
        Result := gdinfNoAttachment;
      except
       Result := gdinfLoadFail;
      end;
     finally
      acStream.Free;
      acSL.Free;
      MemoryStream.Free;
      TypeStream.Free;
     end;
    end;
    
    function TGDataInFile2008.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 TGDataInFile2008.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('GSoft2008',[TGDataInFile2008]);
    end;
    
    end.
     
  2. monique
    Offline

    monique DF Member

    Thanks
     
  3. Prof_Ho2
    Offline

    Prof_Ho2 DF Member

    nice pece of code
     

Share This Page