无法读取xlsx文件- java.io.IOException:无法读取zip条目源

ndh0cuux  于 2023-05-12  发布在  Java
关注(0)|答案(2)|浏览(1008)

我得到下面的错误,而试图读取Excel文件。

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

我的代码如下:

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();

有人能帮帮忙吗?我尝试了一切,但没有任何工作。P.S我尝试了下面的代码,以及这有助于我阅读Excel,但后来我不能写在文件中。

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

最奇怪的是,如果我将SHE_267.xlsx的内容复制到一个新的xlsx并使用它,那么上面的代码就可以正常工作。即使我在excel SHE_267.xlsx中创建了一个新工作表,并将sheet1的内容复制到新工作表中,并尝试访问新工作表,相同的代码也可以工作。这怎么可能?原始sheet1的问题是什么?我错过了什么?

bcs8qyzn

bcs8qyzn1#

尝试添加ZipSecureFile.setMinInflateRatio(-1.0d)来禁用安全检查,但要确保您正在控制输入,这样您就不会有满内存

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();
zkure5ic

zkure5ic2#

尝试像这样配置资源插件:

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

相关问题