开发者

How to port this file join function from Delphi to Ruby

I got this delphi function to join files:

procedure Join(Main_FileName, Hidden_Filename : string);
var
  MainFile    : TFileStream;
  HiddenFile  : TFileStream;
  SizeOfFile  : Cardinal;
  SearchWord  : string[10];
begin
  MainFile       := TFileStream.Create(Main_FileName, fmOpenReadWrite or fmShareDenyWrite);
  try
    SizeOfFile     := MainFile.Size;
    HiddenFile     := TFileStream.Create(Hidden_Filename, fmOpenRead or fmShareDenyNone);
    try
      MainFile.Seek(0, soFromEnd);
     开发者_开发问答 MainFile.CopyFrom(HiddenFile, 0);
      MainFile.Seek(0, soFromEnd);
    finally
     HiddenFile.Free;
    end;
    SearchWord := IntToStr(SizeOfFile) + #0;
    MainFile.Seek(0, soFromEnd);
    MainFile.WriteBuffer(SearchWord, SizeOf(SearchWord));
  finally
   MainFile.Free;
  end;
end;

And need to port this in ruby. The created file should compatible with this delphi split function: (Exists in delphi only no ruby port needed)

procedure Split(Main_FileName, NewFromMain_Filename : string);
var
  MainFile  : TFileStream;
  SplitFile : TFileStream;
  HelpStr   : string[10];
  GetSize   : integer;
begin
  MainFile    := TFileStream.create(Main_FileName, fmOpenReadWrite or fmShareDenyWrite);
  try
    SplitFile := TFileStream.Create(NewFromMain_Filename, fmCreate or fmShareDenyNone);
    try
      MainFile.Position   := MainFile.Size - 11;
      MainFile.Read(HelpStr, 10);
      GetSize := StrToInt(HelpStr);
      MainFile.Position := GetSize;
      SplitFile.CopyFrom(MainFile, MainFile.Size-GetSize);
      SplitFile.Size := SplitFile.Size - 11;
      MainFile.Size :=  GetSize;
    finally
     SplitFile.Free;
    end;
  finally
   MainFile.Free;
  end;
end;

Source of the delphi functions in from here: http://www.delphi-treff.de/tipps/dateienverzeichnisse/dateioperationen/dateien-miteinander-verschmelzen/


Here is the Ruby way - short, readable and easy:

size = File.size("mainfile")
File.open("mainfile", "ab") {|mf|
  File.open("hiddenfile", "rb") { |hf|
     mf.write(hf.read)
     mf.write("#{size}\x0".ljust(10))
  }
}

didn't check it split works, have no delphi

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜