Error reading PNG files using Ruby on Windows

I am creating a zip file containing both text files and image files. The code works as expected when working on MacOS, but it does not work when working on Windows because the contents of the image file are not read correctly.

Below, the image below reads PNG image files as "PNG", adding 5 bytes to the Zip file for each PNG image.

Is this a problem in a Windows environment?

zip_fs.file.open(destination, 'w') do |f|
  f.write File.read(file_name)
end
0
source share
3 answers

from Why are binaries damaged when they are replaced?

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

You need to tell Ruby to read / write files in binary mode. Here are a few variations of the theme:

zip_fs.file.open(destination, 'wb') do |f|
  File.open(file_name, 'rb') do |fi|
    f.write fi.read
  end
end

zip_fs.file.open(destination, 'wb') do |f|
  f.write File.read(file_name, 'mode' => 'rb')
end

zip_fs.file.open(destination, 'wb') do |f|
  f.write File.readbin(file_name)
end

, , , , , . . , :

BLOCK_SIZE = 1024 * 1024
zip_fs.file.open(destination, 'wb') do |f|
  File.open(file_name, 'rb') do |fi|
    while (block_in = fi.read(BLOCK_SIZE)) do
      f.write block_in
    end
  end
end

, , . File.binread(_)

, , open, " ", . , .

Ruby, , script, . close. OP RubyZip, , , open. read readbin EOF . , , , .

+1

, Lib. :

 File.open(path + '\Wall.Lib')

javascript, .

0

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


All Articles