FileUtils.mv 在 Windows 中添加换行符 [英] FileUtils.mv adding linebreaks in Windows

查看:27
本文介绍了FileUtils.mv 在 Windows 中添加换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 Flash 应用程序流式传输 wav 数据.如果我获取数据并执行以下操作:

I am streaming wav data from a flash application. If I get the data and do the following:

f = File.open('c:/test.wav', 'wb')
f << wav_data.pack('c'*wav_data.length)
f.close

wav 文件完美运行.如果我这样做:

The wav file works perfectly. If I do this:

f = Tempfile.new('test.wav')
f << wav_data.pack('c'*wav_data.length)
f.close
FileUtils.mv(f.path, 'c:/')

文件在那里,但听起来全是乱码.检查十六进制编辑器显示,工作文件到处都是 0A(或 \n),乱码版本有 0D0A(或 \r\n)

The file is there, but sounds all garbled. Checking in a hex editor shows that everywhere the working file had an 0A (or \n), the garbled version had 0D0A (or \r\n)

我将它与 rails+paperclip 结合使用,并且将在实时应用程序中使用 Heroku 和 S3 的组合,所以我希望这个问题会自行解决,但我想让它工作暂时在我的本地机器上.

I am using this in conjuction with rails+paperclip, and am going to be using a combination of Heroku and S3 for the live app, so I am hoping this problem will solve itself, but I'd like to get this working on my local machine for the time being.

是否有人知道 FileUtils.mv 会这样做的任何原因,以及是否有办法改变其行为?

Does anybody know of any reason FileUtils.mv would be doing this, and if there is a way to change its behaviour?

推荐答案

看起来像是二进制文件 vs 文本文件的问题.您应该使用 File#binmode 将文件设置为二进制,如下所示:

It looks like a binary file vs text file problem. You should set your file to binary with File#binmode as in:

f = Tempfile.new('test.wav')
f.binmode
f << data

在第一个示例中,您一定有第二个参数 open ,不是吗?像w+b"这样的东西,可能……b"代表二进制.

You must have had a second argument to open in your first example, no? Something like "w+b", probably... The "b" stands for binary.

这篇关于FileUtils.mv 在 Windows 中添加换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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