Như những người khác đã chỉ ra, bạn có thể sử dụng lớp ZipOutputStream
để tạo tệp ZIP. Bí quyết để đặt nhiều tệp trong một tệp ZIP là sử dụng các mô tả ZipEntry
trước khi viết (marshalling) dữ liệu XML JAXB trong ZipOutputStream
. Vì vậy, mã của bạn có thể trông giống như thế này:
JAXBElement jaxbElement1 = objectFactory.createRoot(rootType);
JAXBElement jaxbElement2 = objectFactory.createRoot(rootType);
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(new FileOutputStream("xml-file.zip"));
// add zip-entry descriptor
ZipEntry ze1 = new ZipEntry("xml-file-1.xml");
zos.putNextEntry(ze1);
// add zip-entry data
marshaller.marshal(jaxbElement1, zos);
ZipEntry ze2 = new ZipEntry("xml-file-2.xml");
zos.putNextEntry(ze2);
marshaller.marshal(jaxbElement2, zos);
zos.flush();
} finally {
if (zos != null) {
zos.close();
}
}
Nguồn
2011-08-20 12:05:27
Tại sao bạn sẽ viết một tệp XML bằng JAXB? Tại sao bạn không sao chép nó vào zip trực tiếp –
Zip là nhiều hơn để lưu trữ tất cả mọi thứ thay vì nén nó. Mặc dù trình soạn thảo của nó thực sự xử lý các tệp i cần một giải pháp cho phép tôi thay đổi các tệp bên ngoài trình chỉnh sửa để thay đổi nhanh chóng. – kdgwill