search
HomeJavajavaTutorialHow Do I Read Content from Multiple File Types Within a Zip Archive Using Apache Tika?

How Do I Read Content from Multiple File Types Within a Zip Archive Using Apache Tika?

Reading Content from Files Within Zip Achieved with Apache Tika

Challenge:

You aspire to write a Java program that extracts and reads the content of multiple files within a zip archive using Apache Tika. Specifically, your zip file contains a mix of text, PDF, and docx files.

Solution:

public class ZipContentExtractor {

    public static void main(String[] args) throws IOException, SAXException, TikaException {
        File zipFile = new File("C:\Users\xxx\Desktop\abc.zip");

        try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFile))) {
            ZipEntry entry;
            while ((entry = zipInputStream.getNextEntry()) != null) {
                // Checking file types
                if (entry.getName().endsWith(".txt") || entry.getName().endsWith(".pdf") || entry.getName().endsWith(".docx")) {
                    // Handling text files
                    if (entry.getName().endsWith(".txt")) {
                        BodyContentHandler textHandler = new BodyContentHandler();
                        Parser parser = new AutoDetectParser();
                        parser.parse(zipInputStream, textHandler, new Metadata(), new ParseContext());
                        System.out.println("TXT file content: " + textHandler.toString());
                    }
                    // Handling PDF files
                    else if (entry.getName().endsWith(".pdf")) {
                        Metadata metadata = new Metadata();
                        Parser parser = new PDFParser();
                        parser.parse(zipInputStream, new StreamingContentHandler(), metadata, new ParseContext());
                        System.out.println("PDF file content: " + metadata.get("xmpDM:documentID"));
                    }
                    // Handling DOCX files
                    else {
                        BodyContentHandler textHandler = new BodyContentHandler();
                        Parser parser = new OOXMLParser();
                        parser.parse(zipInputStream, textHandler, new Metadata(), new ParseContext());
                        System.out.println("DOCX file content: " + textHandler.toString());
                    }
                }
            }
        }
    }
}

Explanation:

  • The code iterates through the entries in the zip file.
  • For each entry, it checks the file type and handles it appropriately based on the file extension.
  • For text files, Apache Tika's AutoDetectParser is used to parse the content into a String.
  • For PDF files, the PDFParser is used to extract metadata, such as the document ID.
  • For DOCX files, the OOXMLParser is used to parse the content into a String.

The above is the detailed content of How Do I Read Content from Multiple File Types Within a Zip Archive Using Apache Tika?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor