首页 > 代码库 > tika获取压缩文件内容
tika获取压缩文件内容
package com.wangchao.tika.sax;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class MyHandler extends DefaultHandler {
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
String content = new String(ch,start,length);
System.out.println(content);
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
System.out.println("</"+qName+">");
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
System.out.println("<"+qName+">");
}
}
//主类
package com.wangchao.tika.demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
import org.apache.tika.Tika;
import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.xml.sax.SAXException;
import com.wangchao.tika.sax.MyHandler;
public class RarDemo {
public static void main(String[] args) throws FileNotFoundException, IOException, SAXException, TikaException {
Tika tika = new Tika();
Metadata m = new Metadata();
Parser parser = tika.getParser();
MyHandler mh = new MyHandler();
ParseContext p = new ParseContext();
p.set(Parser.class,parser);
//目前能实现读取压缩文件里面文件的标题和正文。
parser.parse(new FileInputStream(new File("/home/wangchao/下载/4.zip")), mh, m, p);
}
}
以下代码与上面没关系,可以获取压缩文件中的文件的元数据
package com.wangchao.tika.zip;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import org.apache.tika.Tika;
import org.apache.tika.metadata.Metadata;
public class ZipFileDemo {
/**
* @param args
* @throws IOException
* @throws ZipException
*/
public static void main(String[] args) throws ZipException, IOException {
// TODO 自动生成的方法存根
ZipFile zf = new ZipFile(new File("/home/wangchao/下载/4.zip"));
List<ZipEntry> list = (List<ZipEntry>) Collections.list(zf.entries());
Tika tika = new Tika();
Metadata m = new Metadata();
tika.parse(zf.getInputStream(list.get(0)), m);
System.out.println(m);
}
}