Java zip文件已创建但未打开,但文件意外结束 [英] Java zip file created but not opening up, says unexpected end of file

查看:133
本文介绍了Java zip文件已创建但未打开,但文件意外结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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);
        }
    }

我将其转换为

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

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

This creates the zip file, but when I try to open it up, it says

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

我不在这里做什么?

推荐答案

您需要确保致电:

gzipOutputStream.flush();

最终

gzipOutputStream.close();

这篇关于Java zip文件已创建但未打开,但文件意外结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆