3

我有两个二进制文件(假设这是一个以前分成两部分的 ZIP 文件)。如何将它们组合成一个文件?更准确地说,将第二个文件添加到第一个文件中。

更新:伙计们,感谢所有回复我的人,但这并不是我所需要的。基本上,我需要一个 shell 命令的类似物:“copy /b file.000+file.001+file.002 file.bin”

4

1 回答 1

11

像这样:

var
  InStream, OutStream: TFileStream;
....
OutStream := TFileStream.Create(OutFileName, fmCreate);
try
  InStream := TFileStream.Create(InFileName1, fmOpenRead);
  try
    OutStream.CopyFrom(InStream, InStream.Size);
  finally
    InStream.Free;
  end;
  InStream := TFileStream.Create(InFileName2, fmOpenRead);
  try
    OutStream.CopyFrom(InStream, InStream.Size);
  finally
    InStream.Free;
  end;
finally
  OutStream.Free;
end;

或者更一般地连接多个文件:

procedure ConcatenateFiles(const InFileNames: array of string;
  const OutFileName: string);
var
  i: Integer;
  InStream, OutStream: TFileStream;
begin
  OutStream := TFileStream.Create(OutFileName, fmCreate);
  try
    for i := 0 to high(InFileNames) do
    begin
      InStream := TFileStream.Create(InFileNames[i], fmOpenRead);
      try
        OutStream.CopyFrom(InStream, InStream.Size);
      finally
        InStream.Free;
      end;
    end;
  finally
    OutStream.Free;
  end;
end;

像这样称呼它:

ConcatenateFiles(['Part1', 'Part2'], 'Outfile.zip')
于 2013-04-22T19:23:49.573 回答