确定Ruby中的文件类型 [英] Determine file type in Ruby
本文介绍了确定Ruby中的文件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一个可靠如何确定文件的类型?文件扩展名分析是不可接受的.必须有一个类似于UNIX file(1)命令的rubyesque工具吗?
How does one reliably determine a file's type? File extension analysis is not acceptable. There must be a rubyesque tool similar to the UNIX file(1) command?
这与MIME或内容类型有关,与文件系统分类(例如目录,文件或套接字)无关.
This is regarding MIME or content type, not file system classifications, such as directory, file, or socket.
推荐答案
与libmagic
绑定的红宝石绑定可以满足您的需求.它可以作为名为
There is a ruby binding to libmagic
that does what you need. It is available as a gem named ruby-filemagic:
gem install ruby-filemagic
需要libmagic-dev
.
文档似乎有些薄,但这应该可以帮助您入门:
The documentation seems a little thin, but this should get you started:
$ irb
irb(main):001:0> require 'filemagic'
=> true
irb(main):002:0> fm = FileMagic.new
=> #<FileMagic:0x7fd4afb0>
irb(main):003:0> fm.file('foo.zip')
=> "Zip archive data, at least v2.0 to extract"
irb(main):004:0>
这篇关于确定Ruby中的文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文