Why are binary files damaged when they are replaced?

I have a service that delivers zip files over the Internet. Zip contains executable files for the Windows platform.

I use the RubyZip library to compress the file, but this process damages the binary. On my local server, we use the zip command through a system call, and it works fine.

The zip command is not available in Heroku, and I'm just out of options.

I am using this class:

require 'zip/zip'

# This is a simple example which uses rubyzip to
# recursively generate a zip file from the contents of
# a specified directory. The directory itself is not
# included in the archive, rather just its contents.
#
# Usage:
#   directoryToZip = "/tmp/input"
#   outputFile = "/tmp/out.zip"   
#   zf = ZipFileGenerator.new(directoryToZip, outputFile)
#   zf.write()
class ZipFileGenerator

  # Initialize with the directory to zip and the location of the output archive.
  def initialize(inputDir, outputFile)
    @inputDir = inputDir
    @outputFile = outputFile
  end

  # Zip the input directory.
  def write()
    entries = Dir.entries(@inputDir); entries.delete("."); entries.delete("..") 
    io = Zip::ZipFile.open(@outputFile, Zip::ZipFile::CREATE); 

    writeEntries(entries, "", io)
    io.close();
  end

  # A helper method to make the recursion work.
  private
  def writeEntries(entries, path, io)

    entries.each { |e|
      zipFilePath = path == "" ? e : File.join(path, e)
      diskFilePath = File.join(@inputDir, zipFilePath)
      puts "Deflating " + diskFilePath
      if  File.directory?(diskFilePath)
        io.mkdir(zipFilePath)
        subdir =Dir.entries(diskFilePath); subdir.delete("."); subdir.delete("..") 
        writeEntries(subdir, zipFilePath, io)
      else
        io.get_output_stream(zipFilePath) { |f| f.puts(File.open(diskFilePath, "rb").read())}
      end
    }
  end

end
+3
source share
2 answers

theglauberThe answer is correct. As stated in the documentation of the classIO , which is a Filesuperclass:

. EOL โ†” CRLF Windows. ASCII-8BIT, .

. Windows (\r\n) (\n), , , , .

, IO#puts , (\r\n Windows), .

, File.open. , :

io.get_output_stream(zip_file_path) do |out|
  out.write File.binread(disk_file_path)
end
+3

Windows, .

: io = File.open('foo.zip','wb')

+3

Source: https://habr.com/ru/post/1546606/


All Articles