Apache POI - File size getting reduced on adding custom properties

Viewed 55

I Have a Java service which is responsible for adding custom properties to the incoming office file stream.
Upon save, the file size is getting decreased for few set of xlsx, docx, that I've tested so far. However, when I try to unzip the file and then compare the size it comes as expected few bytes/kilobytes greater than the original size.

Below is the sample code for the same:

 public InputStream addCustomPropertyToPOIXMLDocument(POIXMLDocument document,
        List<CustomProperty> customProperties) throws Exception {
    addCustomPropertyToOOXMLDocument(document, customProperties);
    // Fill an empty output stream
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    document.write(baos);
    // Close the document
    document.close();
    // Create the input stream (do not forget to close the inputStream after use)
    return new ByteArrayInputStream(baos.toByteArray());
}

private void addCustomPropertyToOOXMLDocument(POIXMLDocument document, List<CustomProperty>  customPropertiesPayload) {
    CustomProperties documentCustomProperties = document.getProperties().getCustomProperties();
    customPropertiesPayload
            .forEach(customProperty -> {
                String key = customProperty.getKey();
                if (documentCustomProperties.contains(key)) {
                    int index = 0;
                    for (CTProperty prop : documentCustomProperties.getUnderlyingProperties().getPropertyList()) {
                        if (prop.getName().equals(key)) {
                            documentCustomProperties.getUnderlyingProperties().removeProperty(index);
                            break;
                        }
                        index++;
                    }
                }
                documentCustomProperties.addProperty(key, customProperty.getValue());
            });
}

This is not always the case.It only happens for few particular set of office files. I'm not able to figure out the exact RCA. I've added one such case here: https://1drv.ms/u/s!AhQUibsydNRZtTLRl7joGXPpq4c6?e=OZgsXq
Original file(Book2.xlsx) had size 8769 bytes, where as resuling file(Book2-updated.xlsx) has 6918 bytes.
I've tried to compare them using compare tools such as office apps and online tools such as aspose compare. comparision doesnot show any difference. Can anyone tell me the why size getting reduced.

I'm using java 17 for development, and apache POI - 5.2.3
I would appreciate any help. Thanks in advance.

========edit===========
Compare tools only check the content difference. But on saving the file in office format somehow decreases the size of the file. this happens maybe due to different compression level used by Apache POI, MS-office as suggested in the comments or something else entirely.
I didnot find anything conclusive on POI docs. It give the wrong impression that something is removed from document as result of size reduction or am I missing something here?

0 Answers
Related