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.
Is anyone familiar with this kind of issue? Any hint would be really appreciated! Thanks a lot in advance.
CodePudding user response:
Making a copy of the file and not being able to open the copy makes me suspect there is something wrong with the file permissions of the copied file.
Check this out to learn about file permissions in case your environment is Linux. Here's how to view file permissions in Windows
After viewing the file permissions for the copied file, and verifying whether they have Read access for your program, you can learn how to make the copy of the file in a way that gives the proper permissions to the copy.
CodePudding user response:
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);