-
JDOM and GZIP?
I'm trying to save a JDOM document as gzipped XML, but am getting a "JDOMParceException: Premature end of file" when I try to load the document back into my application. Any suggestions? I know that the document is well formed, because I can save and reload it successfully if I don't try to gzip it.
Here's a snippet showing how I'm currently creating the compressed file:
Code:
private Document _doc; // Note: Document creation code not included here
XMLOutputter xmlOutput = new XMLOutputter();
xmlOutput.setFormat(Format.getPrettyFormat()); // I've tried with and without this statement
FileOutputStream fos = new FileOutputStream(absPath);
GZIPOutputStream gzos = new GZIPOutputStream(fos);
xmlOutput.output(_doc, gzos);
and here's my attempt to load the file back into my application:
Code:
SAXBuilder parser = new SAXBuilder();
FileInputStream fis = new FileInputStream(absPath);
GZIPInputStream gzis = new GZIPInputStream(fis);
_doc = parser.build(gzis);
Thank you
-
Re: JDOM and GZIP?
I have never used JDOM so can't be sure what the problem is but the code shown does not close the output stream which means the stream may not be being flushed before you try to read it in again.
Whenever you use streams you should always wrap the code in a try-catch clause and close the stream in the finally section. This is the only way you can guarantee you will not leave a stream open no matter what happens in the code, even if an exception is thrown.
-
Re: JDOM and GZIP?
Thanks! That solved it ...and compressed my test document from 9.9 MB to 376.4 KB
-
Re: JDOM and GZIP?
Glad it hear it's working and it looks like it was certainly worth the effort involved in compressing it.