文件导出,失败 :Stream closed

项目场景:

利用 java.util.zip 的相关的类进行zip 包导出的操作,结果出现 zip 包只导出了一个文件

问题描述:

报错 stream closeed
错误日志:

java.io.IOException: Stream closed

	at java.util.zip.ZipOutputStream.ensureOpen(ZipOutputStream.java:97)
	at java.util.zip.ZipOutputStream.putNextEntry(ZipOutputStream.java:190)
	at com.cyl.base.util.ZipFileUtil.writeFile2(ZipFileUtil.java:79)
	at com.cyl.base.util.ZipFileUtil.writeFile(ZipFileUtil.java:53)
	at com.cyl.base.util.ZipFileUtil.writeZipFile(ZipFileUtil.java:31)
	at com.base.test.ZipFileUtilTest.testWrite(ZipFileUtilTest.java:28)

原因分析:

在这个方法中 传入 的 zos没有被关闭,关闭了 zos 的封装流 Writer fos, 导致了 zos也被标记为已关闭。所以报错

 private void copyFile(File file, ZipOutputStream zos) throws IOException {
        FileInputStream is = null;
        Writer fos = new OutputStreamWriter(zos);
        try {
            is = new FileInputStream(file);
            byte[] bytes = new byte[1024];
            char[] chars = new char[1024];
            int length;
            while ((length = is.read(bytes)) > 0) {
                for (int i = 0; i < length; i++) {
                    chars[i] = (char) bytes[i];
                }
                fos.write(chars, 0, length);
            }
        } catch (IOException e) {
            throw e;
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                }
            }
            try {
                fos.close();
            } catch (Exception e) {
            }
        }
    }

解决方案:

Writer fos 由外层调用方法创建,由外层方法关闭

public void writeFile (List<File> files, File zipFile) throws IOException {
        ZipOutputStream zos = null;
        FileOutputStream fos = null;
        Writer writer = null;
        try {
            fos = new FileOutputStream(zipFile);
            zos = new ZipOutputStream(fos);
            writer = new OutputStreamWriter(zos);

            // 2. 循环创建目录,写入文件
            for (File file: files) {
                copyFile(file, writer);
            }
        } catch (IOException e) {
            throw e;
        } finally {
            if (zos != null) {
                try {
                    zos.close();
                } catch (Exception e) {
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (Exception e) {
                }
            }
            if (writer != null) {
                try {
                    writer.close();
                } catch (Exception e) {
                }
            }
        }
    }
    private void copyFile(File file, Writer fos) throws IOException {
        FileInputStream is = null;
        try {
            is = new FileInputStream(file);
            byte[] bytes = new byte[1024];
            char[] chars = new char[1024];
            int length;
            while ((length = is.read(bytes)) > 0) {
                for (int i = 0; i < length; i++) {
                    chars[i] = (char) bytes[i];
                }
                fos.write(chars, 0, length);
            }
        } catch (IOException e) {
            throw e;
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                }
            }
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值