Java zip file created but not opening says unexpected end of file

I have Objecthow

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

and functions

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);
        }
    }

I convert this to a file zipas

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

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

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

What am I not doing here?

+5
source share
1 answer

You need to call:

gzipOutputStream.flush();

and ultimately

gzipOutputStream.close();
+11
source

All Articles