This is an automated email from the ASF dual-hosted git repository.

pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git


The following commit(s) were added to refs/heads/trunk by this push:
     new eacd5f9bb6 Event based extractor: refactor password support  (#1026)
eacd5f9bb6 is described below

commit eacd5f9bb6169ac90a5b899e41062048d6b9bb03
Author: PJ Fanning <[email protected]>
AuthorDate: Wed Apr 22 12:23:03 2026 +0200

    Event based extractor: refactor password support  (#1026)
    
    * add password
    
    * more changes
    
    * Update MainExtractorFactory.java
    
    * Update MainExtractorFactory.java
---
 .../apache/poi/extractor/MainExtractorFactory.java | 55 +++++++++++++++++-----
 .../hssf/extractor/EventBasedExcelExtractor.java   | 20 +++++++-
 2 files changed, 60 insertions(+), 15 deletions(-)

diff --git 
a/poi/src/main/java/org/apache/poi/extractor/MainExtractorFactory.java 
b/poi/src/main/java/org/apache/poi/extractor/MainExtractorFactory.java
index eba21972d9..8a39bc360a 100644
--- a/poi/src/main/java/org/apache/poi/extractor/MainExtractorFactory.java
+++ b/poi/src/main/java/org/apache/poi/extractor/MainExtractorFactory.java
@@ -31,6 +31,7 @@ import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
 import org.apache.poi.poifs.filesystem.DirectoryNode;
 import org.apache.poi.poifs.filesystem.FileMagic;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+import org.apache.poi.util.ExceptionUtil;
 
 /**
  * ExtractorFactory for HSSF and Old Excel format
@@ -44,34 +45,62 @@ public class MainExtractorFactory implements 
ExtractorProvider {
     @SuppressWarnings({"java:S2095"})
     @Override
     public POITextExtractor create(File file, String password) throws 
IOException {
-        return create(new POIFSFileSystem(file, true).getRoot(), password);
+        POIFSFileSystem fs = new POIFSFileSystem(file, true);
+        POITextExtractor extractor = null;
+        try {
+            extractor = create(fs.getRoot(), password);
+        } catch (Throwable t) {
+            if (!ExceptionUtil.isFatal(t)) {
+                fs.close();
+                throw t;
+            }
+        }
+        if (extractor == null) {
+            fs.close();
+        }
+        return extractor;
     }
 
     @Override
     public POITextExtractor create(InputStream inputStream, String password) 
throws IOException {
-        return create(new POIFSFileSystem(inputStream).getRoot(), password);
+        POIFSFileSystem fs = new POIFSFileSystem(inputStream);
+        POITextExtractor extractor = null;
+        try {
+            extractor = create(fs.getRoot(), password);
+        } catch (Throwable t) {
+            if (!ExceptionUtil.isFatal(t)) {
+                fs.close();
+                throw t;
+            }
+        }
+        if (extractor == null) {
+            fs.close();
+        }
+        return extractor;
     }
 
     @SuppressWarnings("java:S2093")
     @Override
     public POITextExtractor create(DirectoryNode poifsDir, String password) 
throws IOException {
         final String oldPW = Biff8EncryptionKey.getCurrentUserPassword();
-        try {
-            Biff8EncryptionKey.setCurrentUserPassword(password);
 
-            if 
(poifsDir.hasEntry(InternalWorkbook.OLD_WORKBOOK_DIR_ENTRY_NAME)) {
+        if (poifsDir.hasEntry(InternalWorkbook.OLD_WORKBOOK_DIR_ENTRY_NAME)) {
+            Biff8EncryptionKey.setCurrentUserPassword(password);
+            try {
                 return new OldExcelExtractor(poifsDir);
+            } finally {
+                Biff8EncryptionKey.setCurrentUserPassword(oldPW);
             }
+        }
 
-            // Look for certain entries in the stream, to figure it out from
-            for (String workbookName : 
WORKBOOK_DIR_ENTRY_NAMES_CASE_INSENSITIVE) {
-                if (poifsDir.hasEntryCaseInsensitive(workbookName)) {
-                    return ExtractorFactory.getPreferEventExtractor() ? new 
EventBasedExcelExtractor(poifsDir) : new ExcelExtractor(poifsDir);
-                }
+        // Look for certain entries in the stream, to figure it out from
+        for (String workbookName : WORKBOOK_DIR_ENTRY_NAMES_CASE_INSENSITIVE) {
+            if (poifsDir.hasEntryCaseInsensitive(workbookName)) {
+                final char[] passArray = password == null ? null : 
password.toCharArray();
+                return ExtractorFactory.getPreferEventExtractor() ?
+                        new EventBasedExcelExtractor(poifsDir, passArray) :
+                        new ExcelExtractor(poifsDir, passArray);
             }
-
-        } finally {
-            Biff8EncryptionKey.setCurrentUserPassword(oldPW);
         }
 
         return null;
diff --git 
a/poi/src/main/java/org/apache/poi/hssf/extractor/EventBasedExcelExtractor.java 
b/poi/src/main/java/org/apache/poi/hssf/extractor/EventBasedExcelExtractor.java
index a3bb7fb633..3f5144a04a 100644
--- 
a/poi/src/main/java/org/apache/poi/hssf/extractor/EventBasedExcelExtractor.java
+++ 
b/poi/src/main/java/org/apache/poi/hssf/extractor/EventBasedExcelExtractor.java
@@ -63,18 +63,35 @@ import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 public class EventBasedExcelExtractor implements POIOLE2TextExtractor, 
org.apache.poi.ss.extractor.ExcelExtractor {
     private final POIFSFileSystem poifs;
     private final DirectoryNode _dir;
+    private final char[] _password;
     private boolean doCloseFilesystem = true;
     boolean _includeSheetNames = true;
     boolean _formulasNotResults;
 
     public EventBasedExcelExtractor(DirectoryNode dir) {
+        this(dir, null);
+    }
+
+    /**
+     * @since 6.0.0
+     */
+    public EventBasedExcelExtractor(DirectoryNode dir, char[] password) {
         poifs = null;
         _dir = dir;
+        _password = password;
     }
 
     public EventBasedExcelExtractor(POIFSFileSystem fs) {
+        this(fs, null);
+    }
+
+    /**
+     * @since 6.0.0
+     */
+    public EventBasedExcelExtractor(POIFSFileSystem fs, char[] password) {
         poifs = fs;
         _dir = fs.getRoot();
+        _password = password;
     }
 
     /**
@@ -95,7 +112,6 @@ public class EventBasedExcelExtractor implements 
POIOLE2TextExtractor, org.apach
         throw new IllegalStateException("Metadata extraction not supported in 
streaming mode, please use ExcelExtractor");
     }
 
-
     /**
      * Would control the inclusion of cell comments from the document,
      * if we supported it
@@ -158,7 +174,7 @@ public class EventBasedExcelExtractor implements 
POIOLE2TextExtractor, org.apach
         HSSFRequest request = new HSSFRequest();
         request.addListenerForAllRecords(ft);
 
-        factory.processWorkbookEvents(request, _dir);
+        factory.processWorkbookEvents(request, _dir, _password);
 
         return tl;
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to