Not able to read xlsx file - java.io.IOException: Failed to read zip entry source

715 Views Asked by At

I am getting below error while trying to read excel file.

    java.io.IOException: Failed to read zip entry source
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:106)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:342)
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init> 
(XSSFWorkbook.java:285)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1123)
at org.testng.TestNG.run(TestNG.java:1031)
at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)
Caused by: java.util.zip.ZipException: invalid entry size (expected 0 but got 1052 bytes)
at java.util.zip.ZipInputStream.readEnd(ZipInputStream.java:384)
at java.util.zip.ZipInputStream.read(ZipInputStream.java:196)
at org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream.read(ZipSecureFile.java:213)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource$FakeZipEntry.<init>(ZipInputStreamZipEntrySource.java:132)
at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource.<init>(ZipInputStreamZipEntrySource.java:56)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:99)
... 28 more

My code is as below:

   path="C://Users//SHE_267.xlsx";

   xls = new ExcelReader(path);

   public ExcelReader(String path) {

        this.path = path;
        try {
            fis = new FileInputStream(new File(path));
            if (path.endsWith(".xls")) {
                System.out.println("file extension is xls");
                workbook = new HSSFWorkbook(fis);
            } else if (path.endsWith(".xlsx")) {
                System.out.println("file extension is xlsx");
                workbook = new XSSFWorkbook(fis);
            }
            sheet = workbook.getSheetAt(0);
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        

Can someone please help on this? I tried everything but nothing works. P.S I tried with below code as well which helped me to read the excel but then I am not able to write in the file.

    workbook = WorkbookFactory.create(new File(path));

The most weird part is that if I copy the contents from this SHE_267.xlsx to a new xlsx and use that then this same above code works fine. Even if I create a new sheet within the excel SHE_267.xlsx and copy the contents of sheet1 to the new sheet and try to access new sheet, the same code works. How is it possible? What is the issue with the original sheet1? What am I missing?

2

There are 2 best solutions below

1
DEV On

Try adding ZipSecureFile.setMinInflateRatio(-1.0d) to disable security check, but make sure that you are controling the input so you wont have full memory

fis = new FileInputStream(new File(path));
        ZipSecureFile.setMinInflateRatio(-1.0d);
        if (path.endsWith(".xls")) {
            System.out.println("file extension is xls");
            workbook = new HSSFWorkbook(fis);
        } else if (path.endsWith(".xlsx")) {
            System.out.println("file extension is xlsx");
            workbook = new XSSFWorkbook(fis);
        }
        sheet = workbook.getSheetAt(0);
        fis.close();
0
Twave On

Try to configure the resources plugin like this:

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>${maven-resources-plugin.version}</version>
    <configuration>
        <nonFilteredFileExtensions>
            <nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
            <nonFilteredFileExtension>xls</nonFilteredFileExtension>
        </nonFilteredFileExtensions>
    </configuration>
</plugin>