Splitting up and Reassembling A File

Hi,

I was wondering if you possibly help?

I am looking to split up a file, say an image file, and then at a later
date reassemble all of the pieces of this split file to recreate the
image.

I have implemented this so far;

file = File.open(“image.jpg”,“rb”){|f|f.read}
i = 1
file.bytes.each_slice(10000) do |slice|
new_file = File.new(“file_#{i}.txt”, “w+”)
new_file.puts(slice.pack(‘C*’)))
i = i + 1
end

I think this is the right logic to split up a file, storing the bytes
into a text file name file_1.txt, file_2.txt etc based on the size of
the file.

To recreate the file I am thinking of doing something along the lines
of;

new_file = File.new(“image_recreated.jpg”, “wb+”)

#for each split up file do |n|
file = File.open(“file_#{n}.txt”,“rb”){|f|f.read}
new_file.puts(file)
end

When testing it, the new image_recreated file gets created however is
not accessible to view.

I hope someone could possibly help.

Thanks a lot

On Mon, Mar 14, 2011 at 8:11 PM, Rob Pa [email protected] wrote:

file = File.open(“image.jpg”,“rb”){|f|f.read}
i = 1
file.bytes.each_slice(10000) do |slice|
new_file = File.new(“file_#{i}.txt”, “w+”)

Wild guess: have you tried creating these files with the binary flag
too?

Jesus.

Thanks for the reply

Unfortunately that doesn’t fix the issue.

The image file is getting recreated and bits of the image are visible
but its obvious the file is missions bits of data.

Hope you can help,
Thanks!

Rob P. wrote in post #987385:

Hi,

I was wondering if you possibly help?

I am looking to split up a file, say an image file, and then at a later
date reassemble all of the pieces of this split file to recreate the
image.

I have implemented this so far;

file = File.open(“image.jpg”,“rb”){|f|f.read}
i = 1
file.bytes.each_slice(10000) do |slice|
new_file = File.new(“file_#{i}.txt”, “w+”)
new_file.puts(slice.pack(‘C*’)))
i = i + 1
end

  • Try “wb” not “w+”
  • Use “print” or “write”, not “puts” (which adds a newline)
  • Messing about with bytes and pack might work, but you may as well just
    read into a string
  • Close each file after using it - the block form of File.open is the
    easiest way to make sure of that

Something like this (untested):

File.open(“image.jpg”,“rb”) do |src|
i = 1
while chunk = src.read(10000)
File.open(“file_#{i}.bin”,“wb”) do |new_file|
new_file << chunk
end
i += 1
end
end

And of course, you can test this part separately using ‘cat’ at the
command line to try to reassemble the chunks into the original file, and
‘cmp’ to check the results.

Thanks a lot for your help.

I think the:

new_file.write(slice.pack(“C*”))

instead of “puts” has done the trick. I’ll do some further testing.

Thanks again!

On Mon, Mar 14, 2011 at 3:11 PM, Rob Pa [email protected] wrote:

I am looking to split up a file, say an image file, and then at a later
date reassemble all of the pieces of this split file to recreate the
image.

When testing it, the new image_recreated file gets created however is
not accessible to view.

Test your script on a text file and then compare the results (e.g.
diff -u, vimdiff, etc).