2012-08-23 85 views
2

我有一個Object作爲如何在Java中壓縮Object流?

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

我轉換這個對象爲使用JAXBXML如下

OutputStream stream = new ObjectOutputStream(new FileOutputStream(getOutputFilePath(document.getUniqueId()))); 
write(proposal, stream); 

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

我如何轉換這種stream到磁盤Zip文件?

+3

[ZipOutputStream(http://docs.oracle.com/javase/6/docs/api/的java/UTIL /拉鍊/ ZipOutputStream.html) –

回答

0

正如已經暗示,包裝你ObjectStream圍繞ZipOutStream

OutputStream stream = new ZipOutputStream(new ObjectOutputStream(new FileOutputStream(getOutputFilePath(document.getUniqueId()))));