# Joins all .piece files (eg. abc.ext.piece1, abc.ext.piece2 etc) in the current directory and creates a single file (eg. abc.ext) # Does not do any error handling! # Usage: ruby file_joiner.rb class FileJoiner def initialize # sort the .piece filenames in the correct order i.e. if there are 10 .piece files in the current directory # we need to ensure that abc.ext.piece2 (and not abc.ext.piece10) follows abc.ext.piece1 @pieces = Dir["*.piece*"].sort do |f1, f2| f1.split("piece").pop.to_i <=> f2.split("piece").pop.to_i end joined_filename = @pieces[0].split("\.")[0..1].join("\.") @joined_file = File.open(joined_filename, "wb") end def join @pieces.each { |file| @joined_file << File.open(file, "rb").read } @joined_file.close end end FileJoiner.new.join