Ruby快速增加文件大小以进行测试 [英] Ruby increase file size on the fly for testing
问题描述
这听起来可能很奇怪,或者您为什么要这么做。
This may sound weird, or why do you want to do that.
我正在尝试编写一个黄瓜功能来测试上传大图像文件(> 1600万),因此,我不想将大文件存储在github或我的项目。我正在使用imagemagick来创建图像,但据我所知,只是一百万。我可以增加红宝石中的文件大小吗?我不在乎文件中的内容,仅在乎文件大小。谢谢,无论如何,我可以欺骗黄瓜让我相信我正在上传大文件吗?
I'm trying to write a cucumber feature to test uploading large image file (>16M) So, I don't want to store the large file on github or in my project. I'm using imagemagick to create an image but as far as I can do is just 1M. Can I increase the file size in ruby? I don't care about the content inside the file, just the file size. Thanks, is there anyway I could trick cucumber to believe me that I'm uploading a large file size?
谢谢
推荐答案
dd( 1)
工具可以在占用很少磁盘空间的情况下使文件变得很大:
The dd(1)
tool can make a file quite large while using very little disk space:
$ dd if=/dev/zero of=huge bs=1024 count=1 seek=100000
1+0 records in
1+0 records out
1024 bytes (1.0 kB) copied, 4.9857e-05 s, 20.5 MB/s
$ ls -lh huge
-rw-r--r-- 1 user user 98M 2011-07-03 02:43 huge
$ du -h huge
12K huge
文件巨大的
看起来是102400000字节长。 (大约98M。)但是它只占用12 KB的磁盘空间,因为 dd(1)
的 seek
参数导致它开始将写入方式写入文件。如果读取了较早的字节,则操作系统将提供无限的零流。 (0x00类型的零,而不是ASCII类型的零: 0。)
The file huge
looks to be 102400000 bytes long. (Roughly 98M.) But it only takes 12 kilobytes on disk, because the seek
parameter to dd(1)
causes it to start writing way "into" the file. If the earlier bytes are read, the OS will supply an endless stream of zeros. (0x00 kind of zeros, not the ASCII kind of zeros: "0".)
如果您想在Ruby中复制它,则可以使用 File#seek
函数:
If you wanted to replicate this in Ruby, you'd use the File#seek
function:
irb> f=File.new("huge", "w")
=> #<File:huge>
irb> f.seek(100000 * 1024)
=> 0
irb> f.write("hello")
=> 5
irb> f.close()
=> nil
irb> ^D
$ ls -lh huge
-rw-r--r-- 1 sarnold sarnold 98M 2011-07-03 02:47 huge
只是红宝石代码:
f=File.new("100MB", "w")
f.seek(100000 * 1024)
f.write("hello")
f.close()
这篇关于Ruby快速增加文件大小以进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!