6

我有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

我在这里没有做什么?

4

1 回答 1

13

您需要确保致电:

gzipOutputStream.flush();

最终

gzipOutputStream.close();
于 2012-08-23T21:37:19.673 回答