2012-08-23 74 views
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 

這是什麼我不在這裏做?

+3

你關閉打開的gzip輸出流? – gkuzmin

+0

就是這樣,我做了'flush()'和'close()',它的工作,謝謝指出, – daydreamer

回答

11

您需要確保調用:

gzipOutputStream.flush(); 

,並最終

gzipOutputStream.close(); 
+0

我補充說,它的工作謝謝你@Jon – daydreamer