[英]Determine Content Type of Files in Zip Archive in Java
Environment used is Google App Engine. 使用的环境是Google App Engine。 The zip file was uploaded in BlobStore. 该zip文件已在BlobStore中上传。
I have the following code: 我有以下代码:
ZipInputStream zis = ...
ZipEntry ze = zis.getNextEntry();
while( ze != null){
System.out.println(ze.getName());
ze = zis.getNextEntry();
}
How to determine the content type of each file in zip archive? 如何确定zip存档中每个文件的内容类型? ze.getName
method display the name of the file. ze.getName
方法显示文件的名称。 How about the file type? 文件类型怎么样?
Thanks 谢谢
You can use the mime type
instead of trying to guess by the file extensions, that may be missing in some cases. 您可以使用mime type
而不是尝试通过文件扩展名进行猜测,在某些情况下可能会丢失。 Here are the options to establish the mime type
of a file: 以下是建立文件的mime type
的选项:
Using javax.activation.MimetypesFileTypeMap
, like: 使用javax.activation.MimetypesFileTypeMap
,如:
System.out.println("Mime Type of " + f.getName() + " is " + new MimetypesFileTypeMap().getContentType(f));
Using java.net.URL
使用java.net.URL
URL u = new URL(fileUrl); URLConnection uc = u.openConnection(); type = uc.getContentType();
Using Apache Tika
使用Apache Tika
ContentHandler contenthandler = new BodyContentHandler(); Metadata metadata = new Metadata(); metadata.set(Metadata.RESOURCE_NAME_KEY, f.getName()); Parser parser = new AutoDetectParser(); // OOXMLParser parser = new OOXMLParser(); parser.parse(is, contenthandler, metadata); System.out.println("Mime: " + metadata.get(Metadata.CONTENT_TYPE)); System.out.println("Title: " + metadata.get(Metadata.TITLE)); System.out.println("Author: " + metadata.get(Metadata.AUTHOR)); System.out.println("content: " + contenthandler.toString());
Using JMimeMagic
使用JMimeMagic
MagicMatch match = parser.getMagicMatch(f); System.out.println(match.getMimeType()) ;
Using mime-util
使用mime-util
Collection<?> mimeTypes = MimeUtil.getMimeTypes(f);
Droid (Digital Record Object Identification) is a software tool to perform automated batch identification of file formats.
Aperture is an open source library and framework for crawling and indexing information sources such as file systems, websites and mail boxes.
See Get the Mime Type from a File for more details for each of the above options. 有关上述每个选项的更多详细信息,请参阅从文件中获取Mime类型 。
In this case the easiest way is to use the first solution, javax.activation.MimetypesFileTypeMap
, like: 在这种情况下,最简单的方法是使用第一个解决方案javax.activation.MimetypesFileTypeMap
,如:
MimetypesFileTypeMap mtft = new MimetypesFileTypeMap();
String mimeType = mtft.getContentType(ze.getName());
System.out.println(ze.getName()+" type: "+ mimeType);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.