如何在 Java 中使用 GroupDocs.Parser 提取 zip 檔案

如果您需要在 Java 應用程式中extract text from zip 壓縮檔,GroupDocs.Parser 提供乾淨、統一的 API,為您處理繁重的工作。無論您是處理電子郵件附件、大量文件上傳,或是備份套件,本教學將逐步示範how to extract zip內容,並說明為何此函式庫是 Java 開發者的首選。

快速解答

  • 我應該使用哪個函式庫? GroupDocs.Parser for Java.
  • 我可以從 ZIP 中的每個檔案提取文字嗎? Yes, for all formats supported by the parser.
  • 我需要授權嗎? A free trial works for evaluation; a permanent license is required for production.
  • 記憶體使用是否需要關注? Use try‑with‑resources and process items iteratively to keep the footprint low.
  • 需要哪個 Java 版本? JDK 8 or higher.

您將學習

  • 如何使用 GroupDocs.Parser 在 Java 中extract text from zip 檔案。
  • 使用 Maven 或直接下載方式設定函式庫。
  • 實作程式碼以讀取 zip 附件 Java 並檢查容器支援情況。
  • 真實情境、效能技巧與除錯建議。

為何使用 GroupDocs.Parser 進行 ZIP 提取?

只需載入一次 ZIP,讓 parser 處理繁重工作。GroupDocs.Parser 支援超過 60 種文件格式,因此您可以從 PDF、Word 檔、Excel 工作表等提取文字——全部位於同一壓縮檔內。API 會自動偵測 ZIP 是否受支援,串流每個項目,並即時釋放資源,即使是 500 頁的檔案,記憶體使用也保持在 20 MB 以下。

前置條件

  • JDK 8+ 已安裝並設定。
  • IntelliJ IDEA 或 Eclipse 等 IDE(任何相容 Java 的編輯器皆可)。
  • 基本的 Maven 使用經驗(或手動加入 JAR 的能力)。

必要的函式庫、版本與相依性

您需要最新的 GroupDocs.Parser for Java。以下顯示 Maven 坐標。

Maven 設定

<repositories>
   <repository>
      <id>repository.groupdocs.com</id>
      <name>GroupDocs Repository</name>
      <url>https://releases.groupdocs.com/parser/java/</url>
   </repository>
</repositories>

<dependencies>
   <dependency>
      <groupId>com.groupdocs</groupId>
      <artifactId>groupdocs-parser</artifactId>
      <version>25.5</version>
   </dependency>
</dependencies>

直接下載
Alternatively, you can download the latest version from GroupDocs.Parser for Java releases.

取得授權

  • Free trial: Start with a trial to explore the capabilities.
  • Temporary license: Use a temporary key for unrestricted testing.
  • Purchase: For production, obtain a full license to remove evaluation limits.

如何在 Java 中提取 zip 文字

以下我們將實作分為兩個實用功能:

  1. Extract zip attachments – 從壓縮檔內的每個檔案提取文字。
  2. Check container extraction support – Verify that the ZIP can be processed and list its contents.

功能 1 – extract zip attachments

步驟 1:初始化 parser

Parser 類別是讀取 ZIP 等容器格式的入口點。

try (Parser parser = new Parser("YOUR_DOCUMENT_DIRECTORY/SampleZip.zip")) {
    // Proceed with extraction logic...
}

步驟 2:迭代附件並提取文字

ContainerItem 代表 ZIP 內的單一項目。item.openParser() 為該項目建立專屬的 parser,attachmentParser.getText() 回傳提取的文字內容。

Iterable<ContainerItem> attachments = parser.getContainer();
if (attachments == null) {
    System.out.println("Container extraction isn't supported");
} else {
    for (ContainerItem item : attachments) {
        try (Parser attachmentParser = item.openParser()) {
            // Attempt to extract text from each zip entity
            try (TextReader reader = attachmentParser.getText()) {
                String extractedText = reader == null ? "No text" : reader.readToEnd();
                System.out.println(extractedText);
            }
        } catch (UnsupportedDocumentFormatException ex) {
            System.out.println("The format of the contained document isn't supported.");
        }
    }
}

發生了什麼?

  • parser.getContainer() returns an iterable of every entry inside the ZIP.
  • For each ContainerItem, we open a dedicated Parser instance (item.openParser()).
  • attachmentParser.getText() tries to read the textual content; if the format isn’t supported, we catch the exception and move on.

功能 2 – verify container extraction support

步驟 1:初始化 parser(同前)

相同的 Parser 實例可重複使用,以查詢容器的中繼資料。

try (Parser parser = new Parser("YOUR_DOCUMENT_DIRECTORY/SampleZip.zip")) {
    // Check supported operations...
}

步驟 2:列出 ZIP 內的檔案路徑

container.getItems() provides a collection of ContainerItem objects, each exposing getFilePath() for the internal path.

Iterable<ContainerItem> attachments = parser.getContainer();
if (attachments == null) {
    System.out.println("Container extraction isn't supported");
} else {
    for (ContainerItem item : attachments) {
        System.out.println(item.getFilePath()); // Output the file path of each item
    }
}

為何重要:
了解內部結構可協助您決定是否處理該壓縮檔、跳過不支援的檔案,或向使用者提供預覽。

實務應用

  1. Email attachment processing – Automatically extract and index text from archived email attachments.
  2. Document management systems – Ingest bulk uploads where users zip many files together; you can still search the content.
  3. Backup & restore validation – Verify that archived documents contain the expected text before restoring, reducing the risk of corrupted backups.

效能考量

  • Iterative processing: The examples read one file at a time, which prevents memory spikes when dealing with large archives.
  • Try‑with‑resources: Guarantees that each Parser and TextReader is closed promptly, avoiding resource leaks.
  • Threading (advanced): For massive ZIPs you can parallelize the loop, but be sure each thread uses its own Parser instance to stay thread‑safe.

常見問題與解決方案

問題原因解決方案
Container extraction isn't supportedZIP 包含 parser 無法處理的格式。Verify the file types inside the archive; only supported formats can be parsed.
UnsupportedDocumentFormatException嵌套文件的格式未被識別。Skip the file, or convert it to a supported type before zipping.
Memory spikes with large archives同時載入大量檔案。Process items one‑by‑one as shown; avoid storing all extracted text in a collection.

常見問答

Q: 什麼是 GroupDocs.Parser Java?
A: It is a library that extracts text, metadata, and images from a wide range of document formats, including PDFs, Office files, and many more.

Q: 我可以使用此函式庫從 zip 中提取非文字檔案(例如圖片)嗎?
A: The primary focus is text extraction, but you can also retrieve images and other binary content through additional API calls.

Q: 如何有效處理非常大的 ZIP 檔案?
A: Use the iterative approach demonstrated above, keep the parser inside a try‑with‑resources block, and avoid loading all content into memory at once.

Q: GroupDocs.Parser 可以用於商業應用嗎?
A: Yes, but a valid production license is required.

Q: 若遇到問題該向何處求助?
A: Visit the free support forum at GroupDocs Support Forum.

其他資源

Start integrating extract text from zip functionality today and give your Java applications the power to read every document hidden inside an archive!

最後更新: 2026-10-17
測試版本: GroupDocs.Parser 25.5
作者: GroupDocs

相關教學