开发者

Ruby increase file size on the fly for testing

This m开发者_运维问答ay sound weird, or why do you want to do that.

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?

Thanks


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

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".)

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

Just ruby code:

f=File.new("100MB", "w")
f.seek(100000 * 1024)
f.write("hello")
f.close()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜