My goal is to write an object to zip file in json format. The simplest way of doing it is:
ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
String json = gson.toJson(object);
zip.write(json.getBytes());
But I want to avoid to load the whole object to a single string. So I wrapped a zip stream into a writer object:
Writer writer = new OutputStreamWriter(zip);
And after that I write the entry in the following way:
zip.putNextEntry(entry);
gson.toJson(content, writer);
writer.flush();
zip.closeEntry();
zip.flush();
It works fine, but it seems very messy using writer and zip objects at the same time. Is there any better solution for this problem?