我有Object
private String name;
private int age;
private String country;
// getters and setters
我使用XML
将此对象转换为JAXB
,如下所示
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文件?
答案 0 :(得分:3)
Java有许多用于以各种方式处理压缩的类。这是一个有用的链接:Compressing and Decompressing Data Using Java APIs
答案 1 :(得分:0)
正如已经建议的那样,将ObjectStream
包裹在ZipOutStream
OutputStream stream = new ZipOutputStream(new ObjectOutputStream(new FileOutputStream(getOutputFilePath(document.getUniqueId()))));