在 Windows 上使用 Ruby 读取 PNG 文件失败 [英] Failure reading PNG files with Ruby on Windows

查看:55
本文介绍了在 Windows 上使用 Ruby 读取 PNG 文件失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个包含文本文件和图像文件的 Zip 文件.该代码在 MacOS 上运行时按预期工作,但在 Windows 上运行时失败,因为无法正确读取图像文件内容.

下面的代码片段总是将 PNG 图像文件读取为‰PNG",在 Zip 中为每个 PNG 图像添加一个 5 字节的文件.

是否是 Windows 环境的问题?

zip_fs.file.open(destination, 'w') do |f|f.write File.read(file_name)结尾

解决方案

您需要告诉 Ruby 以二进制模式读/写文件.以下是主题的一些变体:

zip_fs.file.open(destination, 'wb') do |f|File.open(file_name, 'rb') 做 |fi|f.write fi.read结尾结尾zip_fs.file.open(destination, 'wb') 做 |f|f.write File.read(file_name, 'mode' => 'rb')结尾zip_fs.file.open(destination, 'wb') 做 |f|f.write File.readbin(file_name)结尾

代码的一个潜在问题是输入文件正在被吞食,如果它大于可用空间,那将是一件坏事.最好以块为单位读取输入文件.这是未经测试的,但应该可以工作:

BLOCK_SIZE = 1024 * 1024zip_fs.file.open(destination, 'wb') 做 |f|File.open(file_name, 'rb') 做 |fi|而 (block_in = fi.read(BLOCK_SIZE)) 做f. 写入 block_in结尾结尾结尾

<小时><块引用>

打开的文件永远不会关闭.使用 File.binread(file_name)

我最初编写的代码是为了表明需要使用二进制模式,并使用 open 因为它更传统",但忘记使用块模式.我修改了我的示例代码来解决这个问题.

然而,该文件会被 Ruby 隐式关闭,因为解释器会在脚本结束时关闭,这是发生的内务处理的一部分.但是,最好显式close 文件.如果 OP 使用 RubyZip 就像我想的那样,如果将块传递给 open.否则,readreadbin 都会读取到 EOF 并关闭文件.如果输入文件的大小未知或大于可用缓冲区空间,则使用这些方法的代码需要对读取块的需求敏感.

I am creating a Zip file containing both text files and image files. The code works as expected when running on MacOS, but it fails when running on Windows because the image file contents are not read correctly.

The snippet below always reads PNG image files as '‰PNG', adding a 5 bytes file in the Zip for each PNG image.

Is it an issue regarding Windows environment?

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

解决方案

You need to tell Ruby to read/write the files in binary mode. Here are some variations on a 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

A potential problem with the code is the input file is being slurped, which, if it's larger than the available space, would be a bad thing. It'd be better to read the input file in blocks. This is untested but should work:

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


The file that was opened will never be closed. Use File.binread(file_name)

My initial code was written to show that binary mode needed to be used, and used open because it's "more traditional", but forgot to use the block mode. I modified my sample code to fix that problem.

However, the file would be closed implicitly by Ruby as the interpreter shuts down when the script ends, as part of housekeeping that occurs. However, it's better to explicitly close the file. If the OP is using RubyZip like I think, that will automatically happen if a block is passed to open. Otherwise, read and readbin will both read to EOF and close the file. Code using those methods needs to be sensitive to the need to read blocks if the input file is an unknown size or larger than available buffer space.

这篇关于在 Windows 上使用 Ruby 读取 PNG 文件失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆