如何将此文件连接函数从 Delphi 移植到 Ruby

发布于 2024-11-01 22:42:22 字数 1909 浏览 2 评论 0原文

我得到了这个 delphi 函数来连接文件:

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;

并且需要将其移植到 ruby​​ 中。 创建的文件应该与这个delphi split函数兼容: (仅存在于delphi中,不需要ruby端口)

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;

delphi函数的来源来自这里: http://www.delphi-treff.de/tipps/ dateienverzeichnisse/dateioperationen/dateien-miteinander-verschmelzen/

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/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍暮颜 2024-11-08 22:42:22

这是 Ruby 方式 - 简短、可读且简单:

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

没有检查它的拆分工作,没有 delphi

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文