Search code examples
iphoneiosruby-on-railsphp-ziparchive

download from rails to iphone zipped file missing portions


I'm using send_file to download a zipped file consisting of multiple images, from my rails server to my iPhone. On Rails I am using Zip::ZipOutputStream.put_next_entry which I think comes from rubyzip. On the iPhone I am using ZipArchive to unzip the file back into multiple files into memory using UnzipFileToData. The problem I run into is that the bottom portions of the images are black by random amounts. Some images have no blacked out portions, others have up to half of the bottom part of the image blacked out. The images are small, around 20 KB in size.

The problem I have is that I cannot figure out what portion of the path from rails to iPhone is causing the images to have their bottom portions blacked out.

 1.  I've ftp'ed the zipped file from my Rails server to my Mac and unzipped them and the images look fine, which means the images are getting zipped on the Rails server correctly.
 2.  I've tried reversing the order that the images are added to the zip file, but the same amounts of the bottom of the images are blacked out.
 3.  Could it be that different compression levels are being used?  

Anyone have any idea why the unzipped images from a multi-file zipped file would be missing random parts of the bottom part of the images?


Solution

  • Found the solution here. My rails code originally looked like:

     Zip::ZipOutputStream.open(zipped_file.path) do |z|
        user_photo_array.each do |file|
            z.put_next_entry(File.basename(file))
            z.print IO.read(file)
        end
     end
    

    and as the link above noted, IO.read is problematic for binary files, so I followed the link's suggestion and replaced IO.read with File.open(file, "rb"){ |f| f.read } as

     Zip::ZipOutputStream.open(zipped_file.path) do |z|
        user_photo_array.each do |file|
            z.put_next_entry(File.basename(file))
            z.print File.open(file, "rb"){ |f| f.read }
        end
     end
    

    and that fixed the problem!