【问题标题】:Java zip file created but not opening up, says unexpected end of fileJava zip 文件已创建但未打开,表示文件意外结束
【发布时间】:2012-08-23 21:26:19
【问题描述】:

我有一个Object

private String name;
private int age;
private String country;
// getters and setters

函数是

protected void write(@Nonnull final Document document, @Nonnull final OutputStream stream) throws PersistenceException {
        try {
            jaxbContext.createMarshaller().marshal(document, stream);
        } catch (final JAXBException e) {
            LOGGER.error(e.getMessage(), e);
            throw new PersistenceException("Failed to marshall document " + docment.getUniqueId() + ": " + e.getMessage(), e);
        }
    }

我把它转换成zip文件

           ByteArrayOutputStream stream = new ByteArrayOutputStream();
           write(document, stream);
           GZIPOutputStream gzipOutputStream = new GZIPOutputStream(new FileOutputStream(new File(getOutputFilePath(document.getUniqueId()))));
           gzipOutputStream.write(stream.toByteArray());

这会创建 zip 文件,但是当我尝试打开它时,它会显示

gzip: document.xml.gz: unexpected end of file

我在这里没有做什么?

【问题讨论】:

  • 是否关闭打开的 gzip 输出流?
  • 就是这样,我做了flush()close() 并且成功了,感谢您指出这一点

标签: java gzip


【解决方案1】:

你需要确保调用:

gzipOutputStream.flush();

最终

gzipOutputStream.close();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-18
    • 2014-10-14
    • 2012-06-10
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    相关资源
    最近更新 更多