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

gzip, java

Solution

You need to make sure to call:

gzipOutputStream.flush();

and eventually

gzipOutputStream.close();

Problem

I have a `Object` as ``` private String name; private int age; private String country; // getters and setters ``` and functions are ``` 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 into `zip` file as ``` ByteArrayOutputStream stream = new ByteArrayOutputStream(); write(document, stream); GZIPOutputStream gzipOutputStream = new GZIPOutputStream(new FileOutputStream(new File(getOutputFilePath(document.getUniqueId())))); gzipOutputStream.write(stream.toByteArray()); ``` This creates the zip file, but when I try to open it up, it says ``` gzip: document.xml.gz: unexpected end of file ``` What is that I am not doing right here?

Original source