I use a method to create xml files using XMLOutputter, FileWriter, and BufferedWriter. It works fine but the xml files can be rather larger (more than 10 megs) and I work with a lot of them.
I am trying to find a way to write the files directly to zip files, keeping the files architecture. The method I use will add different xml files to a main folder one after the other.
I want to change my method so it does not delete already archived files, just add them to it.
With all the time I spend researching a way to do this, I understand that I need to get the files from the existing archive, append new files to it, and zip it again. I would like to do this using zipInputStream and zipOutputStream.
The accepted answer to this question Java appending files into a zip is the best example yet of what I want to do, but the first time I will use the method, the zip file does not exist and I will not add more than one file at a time to the archive. I guess I can work around this by myself with an if statment that will check if the zip file exist, and use file instead of file[] in the method parameters.
The main problem I face is finding a way to create an xml file without writing it to disk and zip it to the zip file.
My guess would be to create a temp file and deleting it after the zipping is done.
Any idea on how I could achieve that?
Here is an example of my code:
XMLOutputter objOut = new XMLOutputter();
objOut.setFormat(Format.getPrettyFormat());
File objBaseDirectory = new File(m_strFolder); // folder where the xml file will be written but not main xml folder
if(!objBaseDirectory.exists())
{objBaseDirectory.mkdirs();}
FileWriter objFileWriter = new FileWriter(m_strFile); // xml file name
BufferedWriter objBuffer = new BufferedWriter(objFileWriter);
objOut.output(m_objectToWrite.toXml(0), objBuffer); // source file to convert to xml
This works fine and writes xml files to the right folder.
Anyone can help me sort this out?