Skip to content
Advertisement

Issues when trying to open an xlsx restricted file: org.apache.poi.openxml4j.exceptions.InvalidOperationException

I have a big issue on a Java tool I work with. Every time I try to upload an xlsx file that is marked as “Restricted with protection”, I got the same exception

       **org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open the 
                                 specified file: 'C:/bea/test.xslx'**

       **at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:106)**

       **at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:221)**



       **Caused by: java.util.zip.ZipException: error in opening zip file**

      **at java.util.zip.ZipFile.open(Native Method)
        at java.util.zip.ZipFile.<init>(Unknown Source)
        at java.util.zip.ZipFile.<init>(Unknown Source)
        at java.util.zip.ZipFile.<init>(Unknown Source)
        at org.apache.poi.openxml4j.opc.internal.ZipHelper.openZipFile(ZipHelper.java:174)
        at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:104)
        ... 6 more**

There is no password on that excel file, it’s just an upgrade of MS office that apply this kind of restriction for every file that is downloaded from the Internet. If I change it into “Unlimited access” everything is fine.

The java tool first create a copy of the excel file I try to upload in a temporary path. The file is correctly created (as the original one) into excelPath. The exception is caught at this line

                      opcPkg = OPCPackage.open(excelPath, PackageAccess.READ); 

I tried to change the PackageAccess into READ_WRITE, but nothing has changed. The tool I’m working with is Java 8 based, it’s built with Ant and runs on Jboss EAP 7.2.

–EDIT– Thanks to @Markus and @Gagravarr I found there is a sort of encryption on that file that allows only selected people to open the document (ie. trying to open it in my personal laptop I’m asked to insert email and password to open it).
In order to repeat the exception it’s suffice to open an xlsx, then go to File–>Info–>Protect Workbook –>Restrict Access and change into “Restricted Access” as shown below

Restriction

Is anyone familiar with this kind of issue? Is it possible to bypass this kind of restriction and open the document in java? Any hint would be really appreciated! Thanks a lot in advance.

Advertisement

Answer

There is a difference between ZipFile and ZipInputStream implementation. Please try:

File file = new File(excelPath);
FileInputStream input = new FileInputStream(file);
opcPkg = OPCPackage.open(input);

Update:

IRM restricted documents / AD RMS of course can not be opened by Apache POI. This is a microsoft specific feature that needs access to licensing server / Active Directory. I never heard of any implementation that integrates this on linux.

User contributions licensed under: CC BY-SA
12 People found this is helpful
Advertisement