Browse Source

[bug-65706] ignore trash parts in OOXML files

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1895370 13f79535-47bb-0310-9956-ffa450edef68
tags/REL_5_2_0
PJ Fanning 2 years ago
parent
commit
7475b1f609

+ 11
- 8
poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java View 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;

+ 10
- 0
poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java View 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.");

BIN
test-data/spreadsheet/Excel_file_with_trash_item.xlsx View File


Loading…
Cancel
Save