从Zip文件中的文件中读取内容
我正在尝试创建一个简单的java程序,它从zip文件中的文件中读取和提取内容。Zip文件包含3个文件(txt,pdf,docx)。我需要阅读所有这些文件的内容,我正在使用Apache Tika。
有人可以帮我在这里实现功能。到目前为止我尝试过这个但没有成功
代码片段
public class SampleZipExtract { public static void main(String[] args) { List<String> tempString = new ArrayList<String>(); StringBuffer sbf = new StringBuffer(); File file = new File("C:\\Users\\xxx\\Desktop\\abc.zip"); InputStream input; try { input = new FileInputStream(file); ZipInputStream zip = new ZipInputStream(input); ZipEntry entry = zip.getNextEntry(); BodyContentHandler textHandler = new BodyContentHandler(); Metadata metadata = new Metadata(); Parser parser = new AutoDetectParser(); while (entry!= null){ if(entry.getName().endsWith(".txt") || entry.getName().endsWith(".pdf")|| entry.getName().endsWith(".docx")){ System.out.println("entry=" + entry.getName() + " " + entry.getSize()); parser.parse(input, textHandler, metadata, new ParseContext()); tempString.add(textHandler.toString()); } } zip.close(); input.close(); for (String text : tempString) { System.out.println("Apache Tika - Converted input string : " + text); sbf.append(text); System.out.println("Final text from all the three files " + sbf.toString()); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TikaException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
慕桂英546537
牧羊人nacy
相关分类