是否有一种使用Apache Commons Compress API从文件内容(即,不是文件扩展名)可靠地检测文件压缩类型的快速方法?
使用Apache Tika,可以做到
Tika tika = new Tika();
String path = <the full path to the file examined, including the filename>;
FileInputStream fis = new FileInputStream(new File(path));
String type = tika.detect(fis);
并且类型变量被检测到的文件内容的 MIME 类型(例如,text/plain、application/zip 等)填充。
理想情况下,出于多种原因,我想避免让 Tika 参与此过程,包括 Tika 似乎将 AR 存档格式误检测为“文本/纯文本”这一事实,这是 Commons Compress 可生产的格式之一。