]> source.dussan.org Git - poi.git/commitdiff
[bug-65706] ignore trash parts in OOXML files
authorPJ Fanning <fanningpj@apache.org>
Sat, 27 Nov 2021 10:15:39 +0000 (10:15 +0000)
committerPJ Fanning <fanningpj@apache.org>
Sat, 27 Nov 2021 10:15:39 +0000 (10:15 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1895370 13f79535-47bb-0310-9956-ffa450edef68

poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java
poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java
test-data/spreadsheet/Excel_file_with_trash_item.xlsx [new file with mode: 0644]

index 915dd7793cded125ff35d3479693d0dcf452a92a..45c1f282e6c95677bc7c7f71afe8e06f6dacd81b 100644 (file)
@@ -330,14 +330,17 @@ public final class ZipPackage extends OPCPackage {
 
             final String entryName = zipArchiveEntry.getName();
             PackagePartName ppn = null;
-            try {
-                // We get an error when we parse [Content_Types].xml
-                // because it's not a valid URI.
-                ppn = (CONTENT_TYPES_PART_NAME.equalsIgnoreCase(entryName)) ? null
-                    : PackagingURIHelper.createPartName(ZipHelper.getOPCNameFromZipItemName(entryName));
-            } catch (Exception e) {
-                // We assume we can continue, even in degraded mode ...
-                LOG.atWarn().withThrowable(e).log("Entry {} is not valid, so this part won't be added to the package.", entryName);
+            // ignore trash parts
+            if (!entryName.startsWith("[trash]")) {
+                try {
+                    // We get an error when we parse [Content_Types].xml
+                    // because it's not a valid URI.
+                    ppn = (CONTENT_TYPES_PART_NAME.equalsIgnoreCase(entryName)) ? null
+                            : PackagingURIHelper.createPartName(ZipHelper.getOPCNameFromZipItemName(entryName));
+                } catch (Exception e) {
+                    // We assume we can continue, even in degraded mode ...
+                    LOG.atWarn().withThrowable(e).log("Entry {} is not valid, so this part won't be added to the package.", entryName);
+                }
             }
 
             this.partName = ppn;
index 503119594ff354914a32e90a51a977fb8a95cbf0..458c46d750a9d8b3a175d682e7ce6f528a6f8923 100644 (file)
@@ -1289,6 +1289,16 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook {
         }
     }
 
+    @Test
+    void testIgnoreTrashParts() throws Exception {
+        try (XSSFWorkbook workbook = openSampleWorkbook("Excel_file_with_trash_item.xlsx")) {
+            for (PackagePart packagePart : workbook.getPackage().getParts()) {
+                assertFalse(packagePart.getPartName().toString().contains("trash"),
+                        "should ignore part " + packagePart.getPartName());
+            }
+        }
+    }
+
     private static void expectFormattedContent(Cell cell, String value) {
         assertEquals(value, new DataFormatter().formatCellValue(cell),
                 "Cell " + ref(cell) + " has wrong formatted content.");
diff --git a/test-data/spreadsheet/Excel_file_with_trash_item.xlsx b/test-data/spreadsheet/Excel_file_with_trash_item.xlsx
new file mode 100644 (file)
index 0000000..ddd11f8
Binary files /dev/null and b/test-data/spreadsheet/Excel_file_with_trash_item.xlsx differ