4

I have two binary files (suppose, this is a ZIP-file previously sliced into 2 parts). How do I can combine them into a single file? More precisely, add the second file to the first one.

UPDATE: Guys, thanks to everyone who responded me, but it's not exactly what I need. Basically, I need an analogue of the shell command: "copy /b file.000+file.001+file.002 file.bin"

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Red October
  • 689
  • 2
  • 12
  • 31
  • Thank you for editing to clarify your intention. Please note that this type of critical information should have been included in your initial post, so that people don't end up doing unneeded work for you. – Jerry Dodge Apr 22 '13 at 23:43
  • @Jerry the update changes nothing. The `copy` command does exactly the same as the code in my answer and is exactly what we all understood the question to be all along. This is an archive that spans multiple files that needs to be re-constructed. – David Heffernan Apr 23 '13 at 07:05

1 Answers1

11

Like this:

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;

Or more generally to concatenate multiple files:

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;

Call it like this:

ConcatenateFiles(['Part1', 'Part2'], 'Outfile.zip')
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • +1 but while this does answer the question, suppose user wants to extract the two files later. How is that addressed? – Jerry Dodge Apr 22 '13 at 19:26
  • 3
    @JerryDodge The combined ZIP file is not two files. The two files here are presumably an archive that has been split in two for transmission purposes, and needs to be concatenated back together before the next step. That next step is going to involve an implementation of a ZIP archive reader. – David Heffernan Apr 22 '13 at 19:28
  • 3
    Q: Who sez the OP wants to break Humpty apart again? It sounds like he just cut the original .zip file in half (or something very similar). – paulsm4 Apr 22 '13 at 19:30
  • No one said that, I was just pointing out that you would presumably want to know that. Kinda like encryption/decryption, what's the point of encrypting if it cannot be decrypted? – Jerry Dodge Apr 22 '13 at 19:58
  • 1
    @Jerry: Most zip utilities still support disk spanning (splitting a large zip file into one or more files that will fit on multiple disks of some kind). In order to unzip at the destination, those files typically have to be reassembled (although utilities like WinZip will often do it at runtime as the archive is decompressed). – Ken White Apr 22 '13 at 20:24
  • OHH I suddenly understand that the two files *used* to be the same file but were split, and now they're being combined back together. I thought the whole question was about taking two unrelated files and combining them into one. – Jerry Dodge Apr 22 '13 at 20:28
  • @Jerry Dodge, come on, try to look at the zip so called "spanned volumes" and you will find out what they are just raw binary segments of the `whole.zip` (`cat` them together). – OnTheFly Apr 22 '13 at 21:20
  • 1
    That wasn't clear at the beginning. Now it is. "Come on" is not going to get you very far here though. – Jerry Dodge Apr 22 '13 at 21:32
  • @David Heffernan: yes, I need something like yours ConcatenateFiles, but unfortunately it doesn't work to me: no errors, but it does nothing for some reason. – Red October Apr 22 '13 at 22:27
  • @David also note OP edited the question to ask something different. – Jerry Dodge Apr 22 '13 at 23:44
  • @RedOctober The code in the question works perfectly and does exactly what you want. – David Heffernan Apr 23 '13 at 06:55
  • @RedOctober I guess the accept means that you found out what was going wrong. Is it all good now? – David Heffernan Apr 23 '13 at 09:08
  • @David, yes, thank you! But I just used `TStringList` instead of `array of string`. – Red October Apr 23 '13 at 09:22