Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

gzip

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?

like image 906
daydreamer Avatar asked Aug 23 '12 21:08

daydreamer


1 Answers

You need to make sure to call:

gzipOutputStream.flush();

and eventually

gzipOutputStream.close();
like image 198
Jon Lin Avatar answered Sep 30 '22 21:09

Jon Lin