-1

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?

  • 1
    have you try adding this https://stackoverflow.com/questions/46796874/java-io-ioexception-failed-to-read-zip-entry-source – DEV Mar 29 '23 at 12:19
  • I did and it didn't work. – Sheena Tyagi Mar 29 '23 at 12:41
  • 1
    Your code works for me - I am unable to recreate your error (I am using a XLSX file that I created with Excel - so I know my Excel file is valid). Try with a different file. Otherwise, please provide a [mre] in your question. That would include more details about the specific libraries and versions you are using, the Java version, a _complete_ stack trace, etc... (We need the "R" in "MRE"). Also, there is no Selenium in your code - why is that tag used? Is it relevant? If so, show how. – andrewJames Mar 29 '23 at 13:26
  • Side note: Why are you using `//` as file path separators? You only need `/`. Perhaps you are thinking of the need to double up backslashes in Java, here (not forward slashes)? – andrewJames Mar 29 '23 at 13:26
  • 1
    Have you attempted to open the xlsx in, say, 7-zip, to see if it is indeed an XLSX file (i.e. is a zip container of other files)? I'd wager someone created an XLS file and renamed it as .xlsx so your code would then fail in that scenario. – Justin N Mar 29 '23 at 13:42

2 Answers2

0

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();
DEV
  • 1,607
  • 6
  • 19
0

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>
LinFelix
  • 1,026
  • 1
  • 13
  • 23
Twave
  • 1
  • 1