aboutsummaryrefslogtreecommitdiffstats
path: root/poi-ooxml
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2023-07-12 12:25:01 +0000
committerPJ Fanning <fanningpj@apache.org>2023-07-12 12:25:01 +0000
commite1bb2a73c83893db399addf28544800310432afc (patch)
treed9cf6fbc846a3b8a322f65e2fb67e8178aa5edc5 /poi-ooxml
parenta9a0acc232deee5dc3a256e68264f23795120805 (diff)
downloadpoi-e1bb2a73c83893db399addf28544800310432afc.tar.gz
poi-e1bb2a73c83893db399addf28544800310432afc.zip
[bug-66675] fix issue with writing xlsx (core properties being added twice)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1910949 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi-ooxml')
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java6
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java4
-rw-r--r--poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java17
3 files changed, 25 insertions, 2 deletions
diff --git a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java
index 1146711705..35f3c10c01 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java
@@ -940,7 +940,7 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
throw new IllegalArgumentException("part");
}
- if (partList.containsKey(part._partName)) {
+ if (hasPackagePart(part)) {
if (!partList.get(part._partName).isDeleted()) {
throw new InvalidOperationException(
"A part with the name '"
@@ -958,6 +958,10 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
return part;
}
+ protected boolean hasPackagePart(PackagePart part) {
+ return partList.containsKey(part._partName);
+ }
+
/**
* Remove the specified part in this package. If this part is relationship
* part, then delete all relationships in the source part.
diff --git a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java
index 3567e66074..dc176b441b 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java
@@ -542,7 +542,9 @@ public final class ZipPackage extends OPCPackage {
// Ensure that core properties are added if missing
getPackageProperties();
// Add core properties to part list ...
- addPackagePart(this.packageProperties);
+ if (!hasPackagePart(this.packageProperties)) {
+ addPackagePart(this.packageProperties);
+ }
// ... and to add its relationship ...
this.relationships.addRelationship(this.packageProperties
.getPartName().getURI(), TargetMode.INTERNAL,
diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
index d8d36726d0..fe3ac973cd 100644
--- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
+++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
@@ -3864,6 +3864,23 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
}
}
+ @Test
+ void testBug66675() throws Exception {
+ try (XSSFWorkbook wb = openSampleWorkbook("bug66675.xlsx")) {
+ POIXMLProperties.CoreProperties coreProperties = wb.getProperties().getCoreProperties();
+ assertNotNull(coreProperties);
+ wb.removeSheetAt(0);
+ try (UnsynchronizedByteArrayOutputStream bos = UnsynchronizedByteArrayOutputStream.builder().get()) {
+ wb.write(bos);
+ try (XSSFWorkbook wb2 = new XSSFWorkbook(bos.toInputStream())) {
+ XSSFSheet sheet = wb2.getSheetAt(0);
+ assertNotNull(sheet);
+ assertNotNull(wb2.getProperties().getCoreProperties());
+ }
+ }
+ }
+ }
+
private static void readByCommonsCompress(File temp_excel_poi) throws IOException {
/* read by commons-compress*/
try (ZipFile zipFile = new ZipFile(temp_excel_poi)) {