2017-05-12 63 views

回答

4

如果你知道你将会是一个理智的Unix系统,那么你只需要致电file即可。

如果您需要独立实施,there are several available on CPAN。可能最接近file的是File::MMagic。这是它自己的实现,所以它可以在任何系统上工作,但可能不像file那样。

$ head test.pl 
#!/usr/bin/env perl 

use strict; 
use warnings; 

$ file test.pl 
test.pl: a /usr/bin/env perl script text executable, ASCII text 

$ perl -wlE 'use File::MMagic; $mm = File::MMagic->new; say $mm->checktype_filename(shift)' test.pl 
x-system/x-unix; executable /usr/bin/env script text 
+0

将'docx'文件报告为zip,但'file'命令也是如此。 – CJ7

+0

@ CJ7如果docx使用zlib压缩,我不会感到惊讶。您可能必须将其与文件扩展名的数据库结合使用。 – Schwern

+0

我只使用这个由于某些原因没有扩展名的文件。 – CJ7

0

File :: MMagic的built-in magic太短了,没用。避免。

改为使用File::LibMagic,这是最好的。

$ perl -mFile::LibMagic -MDDS -E \ 
    'say Dump(File::LibMagic->new 
     ->info_from_filename("Startopia EULA English.docx"))' 
$HASH1 = { 
    description => 'Microsoft Word 2007+', 
    encoding  => 'binary', 
    mime_type => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 
    mime_with_encoding => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document; charset=binary' 
};