The following code gives me an "InvalidOperationException: Operation not
allowed, document open in read only mode!" at the line marked with
asterisks.
InputStream pptFileStream = new FileInputStream(pptxInput);
OPCPackage pkg = OPCPackage.open(pptFileStream);
XSLFSlideShow ss = new XSLFSlideShow(pkg);
pkg.close();
pptFileStream.close();
POIXMLProperties p = ss.getProperties();
List<CTProperty> properties =
p.getCustomProperties().getUnderlyingProperties().getPropertyList();
for (int i = 0; i < properties.size(); i++)
{
if (properties.get(i).getLinkTarget() == null)
{
if (properties.get(i).isSetLpwstr())
{
properties.get(i).setLpwstr(" ");
}
}
}
p.commit(); // **************** Exception Thrown
******************************
OutputStream out = new FileOutputStream(pptxInput);
ss.write(out);
out.close();
This exception appears to occur because the method "public static
OPCPackage open(InputStream in)" forces the access to READ ONLY. So I
tried to change my code to side step the OPCPackage open() method:
I replaced this line:
OPCPackage pkg = OPCPackage.open(pptFileStream);
with these lines
OPCPackage pkg = new ZipPackage(pptFileStream,
PackageAccess.READ_WRITE);
pkg.getParts();
But then I discovered that most of the ZipPackage constructors are NOT
PUBLIC. The ZipPackage.java source file is missing the PUBLIC keyword on
two constructors.
So I tried a different OPEN method.
I replaced this line:
OPCPackage pkg = OPCPackage.open(pptFileStream);
with this line
OPCPackage pkg =
OPCPackage.open(pptxInput.getAbsolutePath(), PackageAccess.READ_WRITE);
Leaving the rest of the code unchanged, this new line causes a
NullPointerException at
POIXMLProperties p = ss.getProperties();
So I removed the two close() calls, and it runs to completion, but it
corrupts the PPTX file (a 51k file becomes a 3k file).
According to the JavaDoc,
OPCPackage open(java.io.InputStream in)
loads the whole ZIP file in memory, and
OPCPackage open(java.lang.String path, PackageAccess access)
does not. So I presume my 3k file is the dump of an empty data structure.
Any suggestions about what I should try next?
Is there a way to force the ZIP file to be loaded into memory?