5
意外結束我有一個Object
作爲Java的zip文件創建,但不對外開放,說文件
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
這是什麼我不在這裏做?
你關閉打開的gzip輸出流? – gkuzmin
就是這樣,我做了'flush()'和'close()',它的工作,謝謝指出, – daydreamer