Splitting up and Reassembling A File

R

Rob Pa

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
 
J

Jesús Gabriel y Galán

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 =3D File.open("image.jpg","rb"){|f|f.read}
i =3D 1
file.bytes.each_slice(10000) do |slice|
=A0new_file =3D File.new("file_#{i}.txt", "w+")

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

Jesus.
 
R

Rob P.

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!
 
B

Brian Candler

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.
 
B

brabuhr

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).
 
R

Rob P.

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top