2

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

4

2 回答 2

3

Java 有许多用于以各种方式处理压缩的类。这是一个有用的链接:使用 Java API 压缩和解压缩数据

于 2012-08-23T20:48:13.793 回答
0

正如已经建议的那样,将你ObjectStreamZipOutStream

OutputStream stream = new ZipOutputStream(new ObjectOutputStream(new FileOutputStream(getOutputFilePath(document.getUniqueId()))));
于 2012-08-23T21:01:44.407 回答