You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将org.w3c.dom生成的XML写入ZipOutputStream时遇ZLIB输入流异常

问题原因

你的代码生成的ZIP文件结构不完整,导致读取时抛出Unexpected end of ZLIB input stream异常。核心问题有两个:

  • 写完XML内容后,未调用zos.closeEntry()关闭当前ZIP条目,条目资源未正确释放,内容未完全刷新
  • 在ZipOutputStream未完成收尾(写入ZIP目录元数据)时,就获取了ByteArrayOutputStream的字节数组,生成的ZIP文件缺少必要结构信息
解决方法

修改getStream()方法,补充关键步骤并优化异常处理:

public InputStream getStream() {
    byte[] zipContent;
    try (final ByteArrayOutputStream bos = new ByteArrayOutputStream();
         final ZipOutputStream zos = new ZipOutputStream(bos)) {
        // 创建并写入ZIP条目
        var zipEntry = new ZipEntry("file1.xml");
        zos.putNextEntry(zipEntry);
        
        // 生成XML内容
        final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
        final Element root = document.createElement("root");
        document.appendChild(root);
        final DOMSource source = new DOMSource(document);
        final StreamResult result = new StreamResult(zos);
        final TransformerFactory transformerFactory = TransformerFactory.newInstance();
        final Transformer transformer = transformerFactory.newTransformer();
        transformer.transform(source, result);
        
        // 关键步骤1:关闭当前ZIP条目,确保内容完全写入
        zos.closeEntry();
        
        // 保存完整ZIP字节数组(try块结束后zos自动关闭,完成目录结构写入)
        zipContent = bos.toByteArray();
    } catch (Exception e) {
        // 避免吞掉异常,抛出运行时异常方便排查问题
        throw new RuntimeException("生成ZIP流失败", e);
    }
    return new ByteArrayInputStream(zipContent);
}
补充说明
  • zos.closeEntry():必须调用该方法结束当前条目的写入流程,确保条目内容被正确刷新到底层流中
  • try-with-resources会自动关闭ZipOutputStream,关闭时会触发finish()方法,自动写入ZIP的目录结构,保证ZIP文件完整性
  • 原代码的空catch块会隐藏所有异常,不利于问题排查,建议改为抛出运行时异常或记录日志

内容的提问来源于stack exchange,提问作者Thomas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 15:35:27