Q:

Ruby file copy produces different file

I'm not very familiar with file handling in ruby.  A problem I've come accross is that reading and writing a binary file doesn't produce exactly the same file.
clone = Tempfile.new(tempfile.original_filename)
FileUtils.copy_stream(tempfile, clone)
clone.flush
From the image below it is clear that it is not an exact file copy, when I try to open the newly created file in an image viewer it reports that the file is corrupt.  I have tried copying the file in different ways such as clone.write(tempfile.read), etc. without success.

*The file viewer also indicates the original is ANSI Dos/Windows and the clone is ANSI Macintosh.  The file size also differs by about 200 bytes.

What I'm trying to accomplish is actually simply using a Tempfile twice.  A file is uploaded via rails and given to me as a Tempfile.  I want to submit it to two different restful services and RestClient.post closes the file automatically.  Another option would be to submit some sort of in memory stream clone to RestClient so that it can not close my file.  If I submit File.open(tempfile.path) to RestClient it produces the same broken file, this indicates that the reading is the problem and not the writing.  If I submit the original Tempfile object to RestClient it works perfectly but then it is closed and deleted and I cannot send it again.
Please help!
Regards,
Pierre

A:

It would be much more helpful to see a hex view of these files instead of a text editor's intepretation. My guess is that at least one of the files is not opened in binary mode. In Ruby 1.9, try
open(filename, 'rb')
open(filename, 'wb')
Tempfile.new(filename, :binmode => true)

for opening a file for reading / writing and to create a binary temporary file, respectively.

