Sto cercando di creare un semplice programma java che legge ed estrae il contenuto dal file (s) all'interno del file zip. Il file zip contiene 3 file (txt, pdf, docx). Ho bisogno di leggere il contenuto di tutti questi file e sto usando Apache Tika per questo scopo.Leggi contenuto da file che si trovano all'interno del file zip
Qualcuno può aiutarmi qui per ottenere la funzionalità. Ho provato questo finora, ma senza successo
frammento di codice
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();
}
}
}
Perché non passare il file zip direttamente ad Apache Tika? Quindi chiamerà il parser ricorsivo che fornisci per ogni file nello zip, quindi non devi fare nulla di speciale! – Gagravarr
Questo è quello che mi stavo chiedendo ma non ho potuto ottenere abbastanza tutorial su come farlo. Sono anche un po 'preoccupato per questo - http://www.javamex.com/tutorials/compression/zip_problems.shtml, non sono sicuro che Tika risolva questo problema. –
Tika utilizza i comuni compressi per aggirare un sacco di questi problemi – Gagravarr