JR *_*lia 4 java unzip archive
使用的环境是Google App Engine.该zip文件已在BlobStore中上传.
我有以下代码:
ZipInputStream zis = ...
ZipEntry ze = zis.getNextEntry();
while( ze != null){
System.out.println(ze.getName());
ze = zis.getNextEntry();
}
Run Code Online (Sandbox Code Playgroud)
如何确定zip存档中每个文件的内容类型?ze.getName方法显示文件的名称.文件类型怎么样?
谢谢
dan*_*dan 10
您可以使用mime type而不是尝试通过文件扩展名来猜测,在某些情况下可能会丢失.以下是建立mime type文件的选项:
使用javax.activation.MimetypesFileTypeMap,像:
System.out.println("Mime Type of " + f.getName() + " is " +
new MimetypesFileTypeMap().getContentType(f));
Run Code Online (Sandbox Code Playgroud)运用 java.net.URL
URL u = new URL(fileUrl);
URLConnection uc = u.openConnection();
type = uc.getContentType();
Run Code Online (Sandbox Code Playgroud)运用 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());
Run Code Online (Sandbox Code Playgroud)运用 JMimeMagic
MagicMatch match = parser.getMagicMatch(f);
System.out.println(match.getMimeType()) ;
Run Code Online (Sandbox Code Playgroud)运用 mime-util
Collection<?> mimeTypes = MimeUtil.getMimeTypes(f);
Run Code Online (Sandbox Code Playgroud)使用DROID
Droid (Digital Record Object Identification) is a software tool to
perform automated batch identification of file formats.
Run Code Online (Sandbox Code Playgroud)孔径框架
Aperture is an open source library and framework for crawling and indexing
information sources such as file systems, websites and mail boxes.
Run Code Online (Sandbox Code Playgroud)有关上述每个选项的更多详细信息,请参阅从文件中获取Mime类型.
在这种情况下,最简单的方法是使用第一个解决方案 javax.activation.MimetypesFileTypeMap,如:
MimetypesFileTypeMap mtft = new MimetypesFileTypeMap();
String mimeType = mtft.getContentType(ze.getName());
System.out.println(ze.getName()+" type: "+ mimeType);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5530 次 |
| 最近记录: |