如何将两个文件连接成一个?

时间:2013-04-22 19:20:51

标签: file delphi

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

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

1 个答案:

答案 0 :(得分:10)

像这样:

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')