[alibaba/easyexcel]csv读取时的autoCloseStream参数无效

2024-04-28 206 views
4
版本
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.2.1</version>
        </dependency>
示例代码
    public static void main(String[] args) {
        Charset gbk = Charset.forName("gbk");
        ZipInputStream zIn = new ZipInputStream(IoUtil.toStream(new File("e://fund_bill_20160405.zip")), gbk);//输入源zip路径
        ZipEntry entry;
        while ((entry = zIn.getNextEntry()) != null && !entry.isDirectory()) {
            EasyExcel.read(zIn, new PageReadListener<Map<Integer, String>>(dataList -> {
                System.out.println(dataList);
            })).excelType(ExcelTypeEnum.CSV).autoCloseStream(false).charset(Charset.forName("gbk")).sheet().doRead();
            System.out.println(entry.getName());
        }
    }

把e://fund_bill_20160405.zip替换成随便一个多个csv压缩成的zip文件,就可以直接运行

报错截图

image

原因分析

由于ZipInputStream的特殊性,该流是可以通过getNextEntry进行多次读取的,但是autoCloseStream参数没起作用,在红框处的代码就会直接把流关闭,黄框执不执行不影响结果. image

临时解决方案

临时的结局方案是通过中转流来做,不直接操作原始流,但是还是希望尽快修复

引入
       <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.6</version>
        </dependency>
示例代码
    public static void main(String[] args) {
        Charset gbk = Charset.forName("gbk");
        ZipInputStream zIn = new ZipInputStream(IoUtil.toStream(new File("e://fund_bill_20160405.zip")), gbk);//输入源zip路径
        ZipEntry entry;

        while ((entry = zIn.getNextEntry()) != null && !entry.isDirectory()) {
            ByteArrayOutputStream tempOutSteam = new ByteArrayOutputStream();
            IoUtil.copy(zIn, tempOutSteam);
            InputStream tempInputStream = new ByteArrayInputStream(tempOutSteam.toByteArray());
            EasyExcel.read(tempInputStream, new PageReadListener<Map<Integer, String>>(dataList -> {
                System.out.println(dataList);
            })).excelType(ExcelTypeEnum.CSV).autoCloseStream(false).charset(Charset.forName("gbk")).sheet().doRead();
            System.out.println(entry.getName());
        }
    }

回答

9

不支持用zip stream作为参数

9

我竟无言以对。这个问题不应该是这个参数无效吗? 和用什么类型inputstream有什么关系 ? 你用什么类型的inputstream都是无效的。

9

抱歉之前没有理解你的问题,get

0

fix