如何在Java中压缩对象流?

时间:2012-08-23 20:44:55

标签: java jaxb

我有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文件?

2 个答案:

答案 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()))));