我得到了这个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拆分函数:(只存在于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/
以下是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))
}
}
没有检查split works,没有delphi