Is there a way to open a docx file, base64-encoded? As you know docx files are compressed files that need to be extracted first before generating a base64-encoded string of the main document. Is there a way to extract the docx prior to loading the docsx using wordMLPackage = Docx4J.load(is);?
In addition, how would I convert back "wordMLPackage" to base64-encode string?
The error I get is
- Code: Select all
org.docx4j.openpackaging.io3.stores.ZipPartStore .<init> line 127 - Unexpected end of ZLIB input stream
the code is
- Code: Select all
FileInputStream in = new FileInputStream(new File(inFilename));
int BUFFER_SIZE = 4096;
byte[] buffer = new byte[BUFFER_SIZE];
int len;
StringBuffer sb = new StringBuffer();
//Read through the buffer
while ((len = in.read(buffer)) > 0)
{
byte [] enc = Base64.encode(Arrays.copyOf(buffer, len));
sb.append(new String(enc));
}
in.close();
ByteArrayOutputStream collect = new ByteArrayOutputStream();
Base64OutputStream b64os = new Base64OutputStream(collect, false);
String wordContent = sb.toString();
b64os.write(wordContent.getBytes());
byte[] ba = collect.toByteArray();
//ba[ba.length] = 0;
ByteArrayInputStream is = new ByteArrayInputStream(ba);
b64os.close();
//Now load the docx
wordMLPackage = Docx4J.load(is);