views:

374

answers:

1

I'm trying to generate a blank docx file using Java, add some text, then write it to a BLOB that I can return to our document processing engine (a custom mess of PL/SQL and Java). I have to use the 1.4 JVM inside Oracle 10g, so no Java 1.5 stuff. I don't have a problem writing the docx to a file on my local machine, but when I try to write to BLOB, I'm getting garbage. Am I doing something dumb? Any help is appreciated. Note in the code below, all the get[name]Xml() methods return an org.w3c.dom.Document.

public void save(String fileName) throws Exception {
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(fileName));
    addEntry(zos, getDocumentXml(), "word/document.xml");
    addEntry(zos, getContentTypesXml(), "[Content_Types].xml");
    addEntry(zos, getRelsXml(), "_rels/.rels");
    zos.flush();
    zos.close();
}

public java.sql.BLOB save() throws Exception {
    java.sql.Connection conn = DbUtilities.openConnection();
    BLOB outBlob = BLOB.createTemporary(conn, true, BLOB.DURATION_SESSION);
    outBlob.open(BLOB.MODE_READWRITE);
    ZipOutputStream zos = new ZipOutputStream(outBlob.setBinaryStream(0L));
    addEntry(zos, getDocumentXml(), "word/document.xml");
    addEntry(zos, getContentTypesXml(), "[Content_Types].xml");
    addEntry(zos, getRelsXml(), "_rels/.rels");
    zos.flush();
    zos.close();
    return outBlob;
}

private void addEntry(ZipOutputStream zos, Document doc, String fileName) throws Exception {
    Transformer t = TransformerFactory.newInstance().newTransformer();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    t.transform(new DOMSource(doc), new StreamResult(baos));
    ZipEntry ze = new ZipEntry(fileName);
    byte[] data = baos.toByteArray();
    ze.setSize(data.length);
    zos.putNextEntry(ze);
    zos.write(data);
    zos.flush();
    zos.closeEntry();
}
A: 

It looks like the problem was in our document processing engine. It was expecting a zipped docx. Glad we document our code well.

Anyway, thanks to everyone who looked at my problem.

Jon Renaut