Writing a docx file to a BLOB using Java 1.4 inside Oracle 10g

Posted by Jon Renaut on Stack Overflow See other posts from Stack Overflow or by Jon Renaut
Published on 2010-03-10T15:44:31Z Indexed on 2010/03/12 1:37 UTC
Read the original article Hit count: 893

Filed under:
|
|
|

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();
}

© Stack Overflow or respective owner

Related posts about java

Related posts about Oracle