문제

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?

도움이 되었습니까?

해결책

The concept is not very much different. your XMLOutputter writes the xml data to target Writer or OutputStream. In your code, it is being written to one that ends up in a file on disk. you can supply another output stream that ends up in the zip.

if you look at the code in the example you gave about appending files to zip, you see the following:

ZipOutputStream out = new ZipOutputStream(new FileOutputStream(source));
for(int i = 0; i < files.length; i++){
    InputStream in = new FileInputStream(files[i]);
    out.putNextEntry(new ZipEntry(path + files[i].getName()));
    for(int read = in.read(buffer); read > -1; read = in.read(buffer)){
        out.write(buffer, 0, read);
    }
    out.closeEntry();
    in.close();
}

...

out.close();

what is happening ? a zip output stream is opened, named "out". then a loop is performed on the files. for each file, an input stream is obtained to read the file bytes, named "in". then an entry is added into the the zip output stream. adding it prepares the output stream to receive data for that entry. after that, the input stream is written into the output stream, the entry is closed (in prparation for the next one for the next file in the loop) and in the end the zip output stream is closed.

well, all you have to do is to loop over your documents (currently being in "m_objectToWrite") instead of the disk files. and then, instead of copying from an input stream to an output stream, you ask your xml outputter to write the document to the output stream. pretty much the same concept of "in a logical world, bytes go from input to output". But here, instead of manually doing this copy, some special object is doing it for you. the xml outputter.

I will leave to you to get everything to work correctly, like a good path and where files are stored in the zip. but supposing that m_objectToWrite is a "Document" object, a skeleton for a working code could be : (using code from the above and from your code in this post:

ZipOutputStream out = new ZipOutputStream(new FileOutputStream(source));
for( Document m_objectToWrite : MY_DOCUMENTS_LIST ){
    out.putNextEntry(new ZipEntry(path + GIVE_NAME_BASED_ON_CURRENT_DOCUMENT));
    objOut.output(m_objectToWrite.toXml(0), out);
    out.closeEntry();
}

...

out.close();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top